Skip to content

Commit

Permalink
Merge pull request #9 from L2ncE/feat/add_option_for_register_with_co…
Browse files Browse the repository at this point in the history
…nfig

feat: add option for `NewConsulRegisterWithConfig`
  • Loading branch information
GuangmingLuo authored Apr 6, 2023
2 parents b727d6d + e3af90f commit 7d341f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions consul_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ func NewConsulRegister(address string, opts ...Option) (registry.Registry, error
}

// NewConsulRegisterWithConfig create a new registry using consul, with a custom config.
func NewConsulRegisterWithConfig(config *api.Config) (*consulRegistry, error) {
func NewConsulRegisterWithConfig(config *api.Config, opts ...Option) (*consulRegistry, error) {
client, err := api.NewClient(config)
if err != nil {
return nil, err
}

return &consulRegistry{consulClient: client}, nil
op := options{
check: defaultCheck(),
}

for _, option := range opts {
option(&op)
}

return &consulRegistry{consulClient: client, opts: op}, nil
}

// Register register a service to consul.
Expand Down
6 changes: 5 additions & 1 deletion consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ func TestNewConsulRegisterWithConfig(t *testing.T) {
Address: consulAddr,
WaitTime: 5 * time.Second,
Namespace: "TEST-NS",
})
}, WithCheck(&consulapi.AgentServiceCheck{
Interval: "7s",
Timeout: "5s",
DeregisterCriticalServiceAfter: "15s",
}))
assert.NoError(t, err)
}

Expand Down
6 changes: 5 additions & 1 deletion example/custom-config/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ func main() {
Address: "127.0.0.1:8500",
Token: "TEST-MY-TOKEN",
}
r, err := consul.NewConsulRegisterWithConfig(&consulConfig)
r, err := consul.NewConsulRegisterWithConfig(&consulConfig, consul.WithCheck(&consulapi.AgentServiceCheck{
Interval: "7s",
Timeout: "5s",
DeregisterCriticalServiceAfter: "15s",
}))
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 7d341f0

Please sign in to comment.