Skip to content

Commit

Permalink
refactor(consul): remove WithAdditionInfo option
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <[email protected]>
  • Loading branch information
rogerogers committed Jan 15, 2024
1 parent ead1ccd commit b034df2
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 49 deletions.
12 changes: 1 addition & 11 deletions consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,14 @@ 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)
testSvcAddr = net.JoinHostPort(localIpAddr, testSvcPort)
testSvcWeight = 777
)

r := NewConsulRegister(consulClient, WithAdditionInfo(info))
r := NewConsulRegister(consulClient)
h := server.Default(
server.WithHostPorts(testSvcAddr),
server.WithRegistry(r, &registry.Info{
Expand All @@ -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)
}
}

Expand Down
10 changes: 1 addition & 9 deletions consul/example/custom-config/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 3 additions & 19 deletions consul/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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,
Expand Down
11 changes: 1 addition & 10 deletions consul/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

0 comments on commit b034df2

Please sign in to comment.