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