Rename ClientConfig to ConfigSpec

The ClientConfig is a fully declarive configuration, so it makes more
sense to rename it to ConfigSpec. It can also mitigate the confusion
between Config and ClientConfig.
This commit is contained in:
ahrtr 2022-03-02 07:24:33 +08:00 committed by ahrtr
parent 3dcbbf62d9
commit 1a3822f2c3
5 changed files with 16 additions and 13 deletions

View File

@ -91,7 +91,10 @@ type Config struct {
// TODO: support custom balancer picker // TODO: support custom balancer picker
} }
type ClientConfig struct { // ConfigSpec is the configuration from users, which comes from command-line flags,
// environment variables or config file. It is a fully declarative configuration,
// and can be serialized & deserialized to/from JSON.
type ConfigSpec struct {
Endpoints []string `json:"endpoints"` Endpoints []string `json:"endpoints"`
RequestTimeout time.Duration `json:"request-timeout"` RequestTimeout time.Duration `json:"request-timeout"`
DialTimeout time.Duration `json:"dial-timeout"` DialTimeout time.Duration `json:"dial-timeout"`

View File

@ -88,7 +88,7 @@ func (*discardValue) String() string { return "" }
func (*discardValue) Set(string) error { return nil } func (*discardValue) Set(string) error { return nil }
func (*discardValue) Type() string { return "" } func (*discardValue) Type() string { return "" }
func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ClientConfig { func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ConfigSpec {
lg, err := zap.NewProduction() lg, err := zap.NewProduction()
if err != nil { if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err) cobrautl.ExitWithError(cobrautl.ExitError, err)
@ -119,7 +119,7 @@ func clientConfigFromCmd(cmd *cobra.Command) *clientv3.ClientConfig {
grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, os.Stderr)) grpclog.SetLoggerV2(grpclog.NewLoggerV2(io.Discard, io.Discard, os.Stderr))
} }
cfg := &clientv3.ClientConfig{} cfg := &clientv3.ConfigSpec{}
cfg.Endpoints, err = endpointsFromCmd(cmd) cfg.Endpoints, err = endpointsFromCmd(cmd)
if err != nil { if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err) cobrautl.ExitWithError(cobrautl.ExitError, err)
@ -150,7 +150,7 @@ func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
return mustClient(cfg) return mustClient(cfg)
} }
func mustClient(cc *clientv3.ClientConfig) *clientv3.Client { func mustClient(cc *clientv3.ConfigSpec) *clientv3.Client {
cfg, err := newClientCfg(cc.Endpoints, cc.DialTimeout, cc.KeepAliveTime, cc.KeepAliveTimeout, cc.Secure, cc.Auth) cfg, err := newClientCfg(cc.Endpoints, cc.DialTimeout, cc.KeepAliveTime, cc.KeepAliveTimeout, cc.Secure, cc.Auth)
if err != nil { if err != nil {
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) cobrautl.ExitWithError(cobrautl.ExitBadArgs, err)

View File

@ -114,7 +114,7 @@ func makeMirrorCommandFunc(cmd *cobra.Command, args []string) {
auth := authDestCfg() auth := authDestCfg()
cc := &clientv3.ClientConfig{ cc := &clientv3.ConfigSpec{
Endpoints: []string{args[0]}, Endpoints: []string{args[0]},
DialTimeout: dialTimeout, DialTimeout: dialTimeout,
KeepAliveTime: keepAliveTime, KeepAliveTime: keepAliveTime,

View File

@ -517,7 +517,7 @@ func NewConfig() *Config {
V2Deprecation: config.V2_DEPR_DEFAULT, V2Deprecation: config.V2_DEPR_DEFAULT,
DiscoveryCfg: v3discovery.DiscoveryConfig{ DiscoveryCfg: v3discovery.DiscoveryConfig{
ClientConfig: clientv3.ClientConfig{ ConfigSpec: clientv3.ConfigSpec{
DialTimeout: DefaultDiscoveryDialTimeout, DialTimeout: DefaultDiscoveryDialTimeout,
RequestTimeout: DefaultDiscoveryRequestTimeOut, RequestTimeout: DefaultDiscoveryRequestTimeOut,
KeepAliveTime: DefaultDiscoveryKeepAliveTime, KeepAliveTime: DefaultDiscoveryKeepAliveTime,

View File

@ -55,8 +55,8 @@ var (
) )
type DiscoveryConfig struct { type DiscoveryConfig struct {
clientv3.ClientConfig `json:"client"` clientv3.ConfigSpec `json:"client"`
Token string `json:"token"` Token string `json:"token"`
} }
type memberInfo struct { type memberInfo struct {
@ -77,18 +77,18 @@ type clusterInfo struct {
} }
// key prefix for each cluster: "/_etcd/registry/<ClusterToken>". // key prefix for each cluster: "/_etcd/registry/<ClusterToken>".
func geClusterKeyPrefix(cluster string) string { func getClusterKeyPrefix(cluster string) string {
return path.Join(discoveryPrefix, cluster) return path.Join(discoveryPrefix, cluster)
} }
// key format for cluster size: "/_etcd/registry/<ClusterToken>/_config/size". // key format for cluster size: "/_etcd/registry/<ClusterToken>/_config/size".
func geClusterSizeKey(cluster string) string { func getClusterSizeKey(cluster string) string {
return path.Join(geClusterKeyPrefix(cluster), "_config/size") return path.Join(getClusterKeyPrefix(cluster), "_config/size")
} }
// key prefix for each member: "/_etcd/registry/<ClusterToken>/members". // key prefix for each member: "/_etcd/registry/<ClusterToken>/members".
func getMemberKeyPrefix(clusterToken string) string { func getMemberKeyPrefix(clusterToken string) string {
return path.Join(geClusterKeyPrefix(clusterToken), "members") return path.Join(getClusterKeyPrefix(clusterToken), "members")
} }
// key format for each member: "/_etcd/registry/<ClusterToken>/members/<memberId>". // key format for each member: "/_etcd/registry/<ClusterToken>/members/<memberId>".
@ -278,7 +278,7 @@ func (d *discovery) joinCluster(config string) (string, error) {
} }
func (d *discovery) getClusterSize() (int, error) { func (d *discovery) getClusterSize() (int, error) {
configKey := geClusterSizeKey(d.clusterToken) configKey := getClusterSizeKey(d.clusterToken)
ctx, cancel := context.WithTimeout(context.Background(), d.cfg.RequestTimeout) ctx, cancel := context.WithTimeout(context.Background(), d.cfg.RequestTimeout)
defer cancel() defer cancel()