From 7b4788ad661ef924107e709b8d1aae77080bd91d Mon Sep 17 00:00:00 2001 From: AlexStocks <alexstocks@foxmail.com> Date: Sat, 30 Mar 2019 22:21:03 +0800 Subject: [PATCH] Imp: delete reduntant constants --- registry/event.go | 2 +- registry/options.go | 12 ++++++--- registry/zookeeper/consumer.go | 4 +-- registry/zookeeper/listener.go | 14 +++++----- registry/zookeeper/zookeeper.go | 48 ++++++++++++++++++--------------- 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/registry/event.go b/registry/event.go index 95e381e00..97a88bc42 100644 --- a/registry/event.go +++ b/registry/event.go @@ -44,5 +44,5 @@ type ServiceURLEvent struct { } func (e ServiceURLEvent) String() string { - return fmt.Sprintf("ServiceURLEvent{Action{%s}, Service{%s}}", e.Action.String(), e.Service) + return fmt.Sprintf("ServiceURLEvent{Action{%s}, Service{%s}}", e.Action, e.Service) } diff --git a/registry/options.go b/registry/options.go index 9ab38be35..28197a7af 100644 --- a/registry/options.go +++ b/registry/options.go @@ -34,8 +34,8 @@ func (t DubboType) Role() string { // dubbo config & options ///////////////////////////////// -type RegistryOptions interface { - ToString() string +type RegistryOption interface { + Name() string } type ApplicationConfig struct { @@ -52,13 +52,17 @@ type Options struct { DubboType DubboType } -func (o *Options) ToString() string { - return fmt.Sprintf("Options{name:%s, version:%s, owner:%s, module:%s, organization:%s, type:%s}", +func (o *Options) String() string { + return fmt.Sprintf("name:%s, version:%s, owner:%s, module:%s, organization:%s, type:%s", o.Name, o.Version, o.Owner, o.Module, o.Organization, o.DubboType) } type Option func(*Options) +func (Option) Name() string { + return "dubbogo-registry-option" +} + func WithDubboType(typ DubboType) Option { return func(o *Options) { o.DubboType = typ diff --git a/registry/zookeeper/consumer.go b/registry/zookeeper/consumer.go index 1bbcfbf23..f91693226 100644 --- a/registry/zookeeper/consumer.go +++ b/registry/zookeeper/consumer.go @@ -140,7 +140,7 @@ func (r *ZkRegistry) listen() { return } log.Warn("getListener() = err:%s", jerrors.ErrorStack(err)) - time.Sleep(timeSecondDuration(REGISTRY_CONN_DELAY)) + time.Sleep(timeSecondDuration(RegistryConnDelay)) continue } if err = listener.listenEvent(r); err != nil { @@ -152,7 +152,7 @@ func (r *ZkRegistry) listen() { listener.close() - time.Sleep(timeSecondDuration(REGISTRY_CONN_DELAY)) + time.Sleep(timeSecondDuration(RegistryConnDelay)) continue } } diff --git a/registry/zookeeper/listener.go b/registry/zookeeper/listener.go index ec3000be2..7fb21d52f 100644 --- a/registry/zookeeper/listener.go +++ b/registry/zookeeper/listener.go @@ -19,9 +19,7 @@ import ( ) const ( - MAX_TIMES = 15 - ZkEvent_Channel_Size = 32 - ZKCLIENT_EVENT_CHANNEL_SIZE = 4 + MaxFailTimes = 15 ) type zkEvent struct { @@ -44,7 +42,7 @@ type zkEventListener struct { func newZkEventListener(client *zookeeperClient) *zkEventListener { return &zkEventListener{ client: client, - events: make(chan zkEvent, ZkEvent_Channel_Size), + events: make(chan zkEvent, 32), serviceMap: make(map[string]struct{}), } } @@ -166,15 +164,15 @@ func (l *zkEventListener) listenDirEvent(zkPath string, conf *service.ServiceCon event chan struct{} zkEvent zk.Event ) - event = make(chan struct{}, ZKCLIENT_EVENT_CHANNEL_SIZE) + event = make(chan struct{}, 4) defer close(event) for { // get current children for a zkPath children, childEventCh, err := l.client.getChildrenW(zkPath) if err != nil { failTimes++ - if MAX_TIMES <= failTimes { - failTimes = MAX_TIMES + if MaxFailTimes <= failTimes { + failTimes = MaxFailTimes } log.Error("listenDirEvent(path{%s}) = error{%v}", zkPath, err) // clear the event channel @@ -188,7 +186,7 @@ func (l *zkEventListener) listenDirEvent(zkPath string, conf *service.ServiceCon } l.client.registerEvent(zkPath, &event) select { - case <-time.After(timeSecondDuration(failTimes * REGISTRY_CONN_DELAY)): + case <-time.After(timeSecondDuration(failTimes * RegistryConnDelay)): l.client.unregisterEvent(zkPath, &event) continue case <-l.client.done(): diff --git a/registry/zookeeper/zookeeper.go b/registry/zookeeper/zookeeper.go index 6f01a1c0d..659505708 100644 --- a/registry/zookeeper/zookeeper.go +++ b/registry/zookeeper/zookeeper.go @@ -23,10 +23,9 @@ import ( ) const ( - defaultTimeout = int64(10e9) - RegistryZkClient = "zk registry" - DEFAULT_REGISTRY_TIMEOUT = 1 * time.Second - REGISTRY_CONN_DELAY = 3 + defaultTimeout = int64(10e9) + RegistryZkClient = "zk registry" + RegistryConnDelay = 3 ) var ( @@ -34,6 +33,11 @@ var ( localIP = "" ) +func init() { + processID = fmt.Sprintf("%d", os.Getpid()) + localIP, _ = gxnet.GetLocalIP() +} + type ZkRegistryConfig struct { Address []string `required:"true" yaml:"address" json:"address,omitempty"` UserName string `yaml:"user_name" json:"user_name,omitempty"` @@ -47,10 +51,21 @@ type Options struct { ZkRegistryConfig } +func (o Options) ToString() string { + return fmt.Sprintf("%s, address:%+v, user:%s, password:%s, conn-timeout:%s", + o.Options, o.Address, o.UserName, o.Password, o.Timeout) +} + type Option func(*Options) -func (Option) OptionName() string { - return "zk's option func" +func (Option) Name() string { + return "dubbogo-zookeeper-registry-option" +} + +func WithRegistryConf(conf ZkRegistryConfig) Option { + return func(o *Options) { + o.ZkRegistryConfig = conf + } } type ZkRegistry struct { @@ -69,18 +84,7 @@ type ZkRegistry struct { outerEventCh chan *registry.ServiceURLEvent } -func init() { - processID = fmt.Sprintf("%d", os.Getpid()) - localIP, _ = gxnet.GetLocalIP() -} - -func WithRegistryConf(conf ZkRegistryConfig) Option { - return func(o *Options) { - o.ZkRegistryConfig = conf - } -} - -func NewZkRegistry(opts ...registry.RegistryOptions) (registry.Registry, error) { +func NewZkRegistry(opts ...registry.RegistryOption) (registry.Registry, error) { var ( err error r *ZkRegistry @@ -115,7 +119,7 @@ func NewZkRegistry(opts ...registry.RegistryOptions) (registry.Registry, error) } if r.ZkRegistryConfig.Timeout == 0 { - r.ZkRegistryConfig.Timeout = DEFAULT_REGISTRY_TIMEOUT + r.ZkRegistryConfig.Timeout = 1e9 } err = r.validateZookeeperClient() if err != nil { @@ -195,7 +199,7 @@ LOOP: case <-r.done: log.Warn("(ZkProviderRegistry)reconnectZkRegistry goroutine exit now...") break LOOP - case <-time.After(time.Duration(1e9 * failTimes * REGISTRY_CONN_DELAY)): // 闃叉鐤媯閲嶈繛zk + case <-time.After(time.Duration(1e9 * failTimes * RegistryConnDelay)): // 闃叉鐤媯閲嶈繛zk } err = r.validateZookeeperClient() log.Info("ZkProviderRegistry.validateZookeeperClient(zkAddr{%s}) = error{%#v}", @@ -223,8 +227,8 @@ LOOP: } } failTimes++ - if MAX_TIMES <= failTimes { - failTimes = MAX_TIMES + if MaxFailTimes <= failTimes { + failTimes = MaxFailTimes } } } -- GitLab