Skip to content

Commit

Permalink
add parameter to allow stale service health queries (#2)
Browse files Browse the repository at this point in the history
- if parameter is not passed in, a nil QueryOptions pointer is still used (previous behavior)
- if paramter is marked true, then only the AllowStale struct field is set

the behavior of setQueryOptions on zero value struct members
- act on non-empty string values
- all boleans QueryOptions are checked for truthiness, default behavior is nothing on false
- single number type struct member only acts on > 0 values

the net effect is that only AllowStale is added by this change (intended at least)
  • Loading branch information
davidbirdsong authored and anubhavmishra committed Oct 8, 2019
1 parent 866c30b commit e49b078
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 3 additions & 2 deletions consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

type Consul struct {
client *api.Client
client *api.Client
queryOpts *api.QueryOptions
}

type ConsulService struct {
Expand All @@ -18,7 +19,7 @@ type ConsulService struct {
func (c *Consul) GetService(serviceName string) ([]ConsulService, error) {
serviceAddressesPorts := []ConsulService{}
// get consul service addresses and ports
addresses, _, err := c.client.Health().Service(serviceName, "", true, nil)
addresses, _, err := c.client.Health().Service(serviceName, "", true, c.queryOpts)
if err != nil {
return nil, errors.Wrap(err, "get consul service")
}
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
consulHTTPAddrFlag = "consul-http-addr"
consulACLTokenFlag = "consul-acl-token"
consulSchemeFlag = "consul-scheme"
consulAllowStaleFlag = "consul-stale"

defaultPort = 8080
defaultConsulDatacenter = "dc1"
Expand Down Expand Up @@ -71,6 +72,10 @@ func configureCli(app *cli.App) {
Value: defaultConsulScheme,
Usage: "The scheme for consul",
},
cli.BoolFlag{
Name: consulAllowStaleFlag,
Usage: "Set stale parameter on consul service health queries",
},
}
cli.AppHelpTemplate = `{{.Name}} - {{.Usage}}
Expand All @@ -90,6 +95,7 @@ func validateConfig(c *cli.Context) (*ServerConfig, error) {
var consulHTTPAddr = c.String(consulHTTPAddrFlag)
var consulACLToken = os.Getenv("CONSUL_HTTP_TOKEN")
var consulScheme = c.String(consulSchemeFlag)
var consulAllowStaleFlag = c.Bool(consulAllowStaleFlag)

if !c.IsSet(consulHTTPAddrFlag) {
consulHTTPAddr = os.Getenv("CONSUL_HTTP_ADDR")
Expand All @@ -104,5 +110,6 @@ func validateConfig(c *cli.Context) (*ServerConfig, error) {
consulHTTPAddr: consulHTTPAddr,
consulACLToken: consulACLToken,
consulScheme: consulScheme,
consulAllowStale: consulAllowStaleFlag,
}, nil
}
9 changes: 8 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ServerConfig struct {
consulHTTPAddr string
consulACLToken string
consulScheme string
consulAllowStale bool
}

func NewServer(config *ServerConfig) (*Server, error) {
Expand All @@ -48,11 +49,17 @@ func NewServer(config *ServerConfig) (*Server, error) {
return nil, errors.Wrap(err, "initializing consul client")
}

var queryOpts *api.QueryOptions
if config.consulAllowStale {
queryOpts = &api.QueryOptions{AllowStale: true}

}

return &Server{
port: config.port,
version: version,
engine: engine,
consul: &Consul{client: client},
consul: &Consul{client: client, queryOpts: queryOpts},
}, nil
}

Expand Down

0 comments on commit e49b078

Please sign in to comment.