config.go 2.0 KB

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