config.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. RpcServerConf struct {
  9. service.ServiceConf
  10. ListenOn string
  11. Etcd discov.EtcdConf `json:",optional"`
  12. Auth bool `json:",optional"`
  13. Redis redis.RedisKeyConf `json:",optional"`
  14. StrictControl bool `json:",optional"`
  15. // setting 0 means no timeout
  16. Timeout int64 `json:",default=2000"`
  17. CpuThreshold int64 `json:",default=900,range=[0:1000]"`
  18. }
  19. RpcClientConf struct {
  20. Etcd discov.EtcdConf `json:",optional"`
  21. Endpoints []string `json:",optional=!Etcd"`
  22. App string `json:",optional"`
  23. Token string `json:",optional"`
  24. Timeout int64 `json:",default=2000"`
  25. }
  26. )
  27. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  28. return RpcClientConf{
  29. Endpoints: endpoints,
  30. App: app,
  31. Token: token,
  32. }
  33. }
  34. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  35. return RpcClientConf{
  36. Etcd: discov.EtcdConf{
  37. Hosts: hosts,
  38. Key: key,
  39. },
  40. App: app,
  41. Token: token,
  42. }
  43. }
  44. func (sc RpcServerConf) HasEtcd() bool {
  45. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  46. }
  47. func (sc RpcServerConf) Validate() error {
  48. if sc.Auth {
  49. if err := sc.Redis.Validate(); err != nil {
  50. return err
  51. }
  52. }
  53. return nil
  54. }
  55. func (cc RpcClientConf) HasCredential() bool {
  56. return len(cc.App) > 0 && len(cc.Token) > 0
  57. }