config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package zrpc
  2. import (
  3. "git.i2edu.net/i2/go-zero/core/discov"
  4. "git.i2edu.net/i2/go-zero/core/service"
  5. "git.i2edu.net/i2/go-zero/core/stores/redis"
  6. )
  7. type (
  8. // A RpcServerConf is a rpc server config.
  9. RpcServerConf struct {
  10. service.ServiceConf
  11. ListenOn string
  12. Etcd discov.EtcdConf `json:",optional"`
  13. Auth bool `json:",optional"`
  14. Redis redis.RedisKeyConf `json:",optional"`
  15. StrictControl bool `json:",optional"`
  16. // setting 0 means no timeout
  17. Timeout int64 `json:",default=2000"`
  18. CpuThreshold int64 `json:",default=900,range=[0:1000]"`
  19. }
  20. // A RpcClientConf is a rpc client config.
  21. RpcClientConf struct {
  22. Etcd discov.EtcdConf `json:",optional"`
  23. Endpoints []string `json:",optional=!Etcd"`
  24. App string `json:",optional"`
  25. Token string `json:",optional"`
  26. Timeout int64 `json:",default=2000"`
  27. }
  28. )
  29. // NewDirectClientConf returns a RpcClientConf.
  30. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  31. return RpcClientConf{
  32. Endpoints: endpoints,
  33. App: app,
  34. Token: token,
  35. }
  36. }
  37. // NewEtcdClientConf returns a RpcClientConf.
  38. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  39. return RpcClientConf{
  40. Etcd: discov.EtcdConf{
  41. Hosts: hosts,
  42. Key: key,
  43. },
  44. App: app,
  45. Token: token,
  46. }
  47. }
  48. // HasEtcd checks if there is etcd settings in config.
  49. func (sc RpcServerConf) HasEtcd() bool {
  50. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  51. }
  52. // Validate validates the config.
  53. func (sc RpcServerConf) Validate() error {
  54. if !sc.Auth {
  55. return nil
  56. }
  57. return sc.Redis.Validate()
  58. }
  59. // HasCredential checks if there is a credential in config.
  60. func (cc RpcClientConf) HasCredential() bool {
  61. return len(cc.App) > 0 && len(cc.Token) > 0
  62. }