config.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package zrpc
  2. import (
  3. "github.com/tal-tech/go-zero/core/discov"
  4. "github.com/tal-tech/go-zero/core/service"
  5. "github.com/tal-tech/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. // TODO: enable it in v1.2.4
  20. // MaxRetries int `json:",default=0,range=[0:]"`
  21. }
  22. // A RpcClientConf is a rpc client config.
  23. RpcClientConf struct {
  24. Etcd discov.EtcdConf `json:",optional"`
  25. Endpoints []string `json:",optional"`
  26. Target string `json:",optional"`
  27. App string `json:",optional"`
  28. Token string `json:",optional"`
  29. NonBlock bool `json:",optional"`
  30. // TODO: enable it in v1.2.4
  31. // Retry bool `json:",optional"` // grpc auto retry
  32. Timeout int64 `json:",default=2000"`
  33. }
  34. )
  35. // NewDirectClientConf returns a RpcClientConf.
  36. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  37. return RpcClientConf{
  38. Endpoints: endpoints,
  39. App: app,
  40. Token: token,
  41. }
  42. }
  43. // NewEtcdClientConf returns a RpcClientConf.
  44. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  45. return RpcClientConf{
  46. Etcd: discov.EtcdConf{
  47. Hosts: hosts,
  48. Key: key,
  49. },
  50. App: app,
  51. Token: token,
  52. }
  53. }
  54. // HasEtcd checks if there is etcd settings in config.
  55. func (sc RpcServerConf) HasEtcd() bool {
  56. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  57. }
  58. // Validate validates the config.
  59. func (sc RpcServerConf) Validate() error {
  60. if !sc.Auth {
  61. return nil
  62. }
  63. return sc.Redis.Validate()
  64. }
  65. // HasCredential checks if there is a credential in config.
  66. func (cc RpcClientConf) HasCredential() bool {
  67. return len(cc.App) > 0 && len(cc.Token) > 0
  68. }