go-zero/zrpc/config.go

74 lines
1.9 KiB
Go
Raw Normal View History

2020-09-18 11:41:52 +08:00
package zrpc
2020-07-26 17:09:05 +08:00
import (
2020-08-08 16:40:10 +08:00
"github.com/tal-tech/go-zero/core/discov"
"github.com/tal-tech/go-zero/core/service"
"github.com/tal-tech/go-zero/core/stores/redis"
2020-07-26 17:09:05 +08:00
)
type (
2021-03-01 23:52:44 +08:00
// A RpcServerConf is a rpc server config.
2020-07-26 17:09:05 +08:00
RpcServerConf struct {
service.ServiceConf
ListenOn string
Etcd discov.EtcdConf `json:",optional"`
Auth bool `json:",optional"`
Redis redis.RedisKeyConf `json:",optional"`
StrictControl bool `json:",optional"`
// setting 0 means no timeout
2020-07-26 17:09:05 +08:00
Timeout int64 `json:",default=2000"`
CpuThreshold int64 `json:",default=900,range=[0:1000]"`
}
2021-03-01 23:52:44 +08:00
// A RpcClientConf is a rpc client config.
2020-07-26 17:09:05 +08:00
RpcClientConf struct {
2020-08-18 18:36:44 +08:00
Etcd discov.EtcdConf `json:",optional"`
Endpoints []string `json:",optional=!Etcd"`
App string `json:",optional"`
Token string `json:",optional"`
2021-02-19 10:24:03 +08:00
Timeout int64 `json:",default=2000"`
2020-07-26 17:09:05 +08:00
}
)
2021-03-01 23:52:44 +08:00
// NewDirectClientConf returns a RpcClientConf.
2020-08-18 18:36:44 +08:00
func NewDirectClientConf(endpoints []string, app, token string) RpcClientConf {
2020-07-26 17:09:05 +08:00
return RpcClientConf{
2020-08-18 18:36:44 +08:00
Endpoints: endpoints,
App: app,
Token: token,
2020-07-26 17:09:05 +08:00
}
}
2021-03-01 23:52:44 +08:00
// NewEtcdClientConf returns a RpcClientConf.
2020-07-26 17:09:05 +08:00
func NewEtcdClientConf(hosts []string, key, app, token string) RpcClientConf {
return RpcClientConf{
Etcd: discov.EtcdConf{
Hosts: hosts,
Key: key,
},
App: app,
Token: token,
}
}
2021-03-01 23:52:44 +08:00
// HasEtcd checks if there is etcd settings in config.
2020-07-26 17:09:05 +08:00
func (sc RpcServerConf) HasEtcd() bool {
return len(sc.Etcd.Hosts) > 0 && len(sc.Etcd.Key) > 0
}
2021-03-01 23:52:44 +08:00
// Validate validates the config.
2020-07-26 17:09:05 +08:00
func (sc RpcServerConf) Validate() error {
if sc.Auth {
if err := sc.Redis.Validate(); err != nil {
return err
}
}
return nil
}
2021-03-01 23:52:44 +08:00
// HasCredential checks if there is a credential in config.
2020-07-26 17:09:05 +08:00
func (cc RpcClientConf) HasCredential() bool {
return len(cc.App) > 0 && len(cc.Token) > 0
}