config.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // pending forever is not allowed
  16. // never set it to 0, if zero, the underlying will set to 2s automatically
  17. Timeout int64 `json:",default=2000"`
  18. CpuThreshold int64 `json:",default=900,range=[0:1000]"`
  19. }
  20. RpcClientConf struct {
  21. Etcd discov.EtcdConf `json:",optional"`
  22. Endpoints []string `json:",optional=!Etcd"`
  23. App string `json:",optional"`
  24. Token string `json:",optional"`
  25. Timeout int64 `json:",optional"`
  26. }
  27. )
  28. func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
  29. return RpcClientConf{
  30. Endpoints: endpoints,
  31. App: app,
  32. Token: token,
  33. }
  34. }
  35. func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
  36. return RpcClientConf{
  37. Etcd: discov.EtcdConf{
  38. Hosts: hosts,
  39. Key: key,
  40. },
  41. App: app,
  42. Token: token,
  43. }
  44. }
  45. func (sc RpcServerConf) HasEtcd() bool {
  46. return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
  47. }
  48. func (sc RpcServerConf) Validate() error {
  49. if sc.Auth {
  50. if err := sc.Redis.Validate(); err != nil {
  51. return err
  52. }
  53. }
  54. return nil
  55. }
  56. func (cc RpcClientConf) HasCredential() bool {
  57. return len(cc.App) > 0 && len(cc.Token) > 0
  58. }