config.go 2.0 KB

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