config.go 1.9 KB

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