config.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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=!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. if err := sc.Redis.Validate(); err != nil {
  56. return err
  57. }
  58. }
  59. return nil
  60. }
  61. // HasCredential checks if there is a credential in config.
  62. func (cc RpcClientConf) HasCredential() bool {
  63. return len(cc.App) > 0 && len(cc.Token) > 0
  64. }