From b034df21efaf3e0f3521248d4f6f897b5ea7c0d7 Mon Sep 17 00:00:00 2001 From: rogerogers Date: Mon, 15 Jan 2024 11:25:06 +0800 Subject: [PATCH] refactor(consul): remove WithAdditionInfo option Signed-off-by: rogerogers --- consul/consul_test.go | 12 +---------- consul/example/custom-config/server/main.go | 10 +--------- consul/registry.go | 22 +++------------------ consul/utils.go | 11 +---------- 4 files changed, 6 insertions(+), 49 deletions(-) diff --git a/consul/consul_test.go b/consul/consul_test.go index f22c459..d7086d8 100644 --- a/consul/consul_test.go +++ b/consul/consul_test.go @@ -152,14 +152,6 @@ func TestConsulRegister(t *testing.T) { return } - info := &AdditionInfo{ - Tags: []string{"tag1", "tag2", "tag3"}, - Meta: map[string]string{ - "meta1": "value1", - "meta2": "value2", - }, - } - var ( testSvcName = "hertz.test.demo1" testSvcPort = fmt.Sprintf("%d", 8581) @@ -167,7 +159,7 @@ func TestConsulRegister(t *testing.T) { testSvcWeight = 777 ) - r := NewConsulRegister(consulClient, WithAdditionInfo(info)) + r := NewConsulRegister(consulClient) h := server.Default( server.WithHostPorts(testSvcAddr), server.WithRegistry(r, ®istry.Info{ @@ -193,8 +185,6 @@ func TestConsulRegister(t *testing.T) { assert.Equal(t, testSvcName, gotSvc.Service) assert.Equal(t, testSvcAddr, net.JoinHostPort(gotSvc.Address, fmt.Sprintf("%d", gotSvc.Port))) assert.Equal(t, testSvcWeight, gotSvc.Weights.Passing) - assert.Equal(t, info.Tags, gotSvc.Tags) - assert.Equal(t, info.Meta, gotSvc.Meta) } } diff --git a/consul/example/custom-config/server/main.go b/consul/example/custom-config/server/main.go index 413430e..d4f09cc 100644 --- a/consul/example/custom-config/server/main.go +++ b/consul/example/custom-config/server/main.go @@ -54,16 +54,8 @@ func main() { Timeout: "5s", DeregisterCriticalServiceAfter: "15s", } - // custom addition info - additionInfo := &consul.AdditionInfo{ - Tags: []string{"tag1", "tag2"}, - Meta: map[string]string{ - "meta1": "val1", - "meta2": "val2", - }, - } r := consul.NewConsulRegister(consulClient, - consul.WithCheck(check), consul.WithAdditionInfo(additionInfo), + consul.WithCheck(check), ) wg.Add(2) diff --git a/consul/registry.go b/consul/registry.go index 5e18b6b..8edeb1e 100644 --- a/consul/registry.go +++ b/consul/registry.go @@ -40,16 +40,10 @@ type consulRegistry struct { opts options } -type AdditionInfo struct { - Tags []string - Meta map[string]string -} - var _ registry.Registry = (*consulRegistry)(nil) type options struct { - check *api.AgentServiceCheck - AdditionInfo AdditionInfo + check *api.AgentServiceCheck } // Option is the option of Consul. @@ -60,11 +54,6 @@ func WithCheck(check *api.AgentServiceCheck) Option { return func(o *options) { o.check = check } } -// WithAdditionInfo is consul registry option to set AdditionInfo. -func WithAdditionInfo(info *AdditionInfo) Option { - return func(o *options) { o.AdditionInfo = *info } -} - // NewConsulRegister create a new registry using consul. func NewConsulRegister(consulClient *api.Client, opts ...Option) registry.Registry { op := options{ @@ -95,12 +84,8 @@ func (c *consulRegistry) Register(info *registry.Info) error { } tags, err := convTagMapToSlice(info.Tags) - if err == nil { - for _, tag := range c.opts.AdditionInfo.Tags { - if !inArray(tag, tags) { - tags = append(tags, tag) - } - } + if err != nil { + return err } svcInfo := &api.AgentServiceRegistration{ @@ -109,7 +94,6 @@ func (c *consulRegistry) Register(info *registry.Info) error { Address: host, Port: port, Tags: tags, - Meta: c.opts.AdditionInfo.Meta, Weights: &api.AgentWeights{ Passing: info.Weight, Warning: info.Weight, diff --git a/consul/utils.go b/consul/utils.go index 8910554..07ab172 100644 --- a/consul/utils.go +++ b/consul/utils.go @@ -67,7 +67,7 @@ func getServiceId(info *registry.Info) (string, error) { return fmt.Sprintf("%s:%s:%d", info.ServiceName, host, port), nil } -// convTagMapToSlice Tags map be convert to slice. +// convTagMapToSlice Tags map be converted to slice. // Keys must not contain `:`. func convTagMapToSlice(tagMap map[string]string) ([]string, error) { svcTags := make([]string, 0, len(tagMap)) @@ -110,12 +110,3 @@ func splitTags(tags []string) map[string]string { return tagMap } - -func inArray(needle string, haystack []string) bool { - for _, k := range haystack { - if needle == k { - return true - } - } - return false -}