go-micro.dev/v5@v5.12.0/registry/consul/options.go (about) 1 package consul 2 3 import ( 4 "context" 5 "fmt" 6 "time" 7 8 consul "github.com/hashicorp/consul/api" 9 "go-micro.dev/v5/registry" 10 ) 11 12 // Define a custom type for context keys to avoid collisions. 13 type contextKey string 14 15 const consulConnectKey contextKey = "consul_connect" 16 const consulConfigKey contextKey = "consul_config" 17 const consulAllowStaleKey contextKey = "consul_allow_stale" 18 const consulQueryOptionsKey contextKey = "consul_query_options" 19 const consulTCPCheckKey contextKey = "consul_tcp_check" 20 const consulHTTPCheckConfigKey contextKey = "consul_http_check_config" 21 22 // Connect specifies services should be registered as Consul Connect services. 23 func Connect() registry.Option { 24 return func(o *registry.Options) { 25 if o.Context == nil { 26 o.Context = context.Background() 27 } 28 o.Context = context.WithValue(o.Context, consulConnectKey, true) 29 } 30 } 31 32 func Config(c *consul.Config) registry.Option { 33 return func(o *registry.Options) { 34 if o.Context == nil { 35 o.Context = context.Background() 36 } 37 o.Context = context.WithValue(o.Context, consulConfigKey, c) 38 } 39 } 40 41 // AllowStale sets whether any Consul server (non-leader) can service 42 // a read. This allows for lower latency and higher throughput 43 // at the cost of potentially stale data. 44 // Works similar to Consul DNS Config option [1]. 45 // Defaults to true. 46 // 47 // [1] https://www.consul.io/docs/agent/options.html#allow_stale 48 func AllowStale(v bool) registry.Option { 49 return func(o *registry.Options) { 50 if o.Context == nil { 51 o.Context = context.Background() 52 } 53 o.Context = context.WithValue(o.Context, consulAllowStaleKey, v) 54 } 55 } 56 57 // QueryOptions specifies the QueryOptions to be used when calling 58 // Consul. See `Consul API` for more information [1]. 59 // 60 // [1] https://godoc.org/github.com/hashicorp/consul/api#QueryOptions 61 func QueryOptions(q *consul.QueryOptions) registry.Option { 62 return func(o *registry.Options) { 63 if q == nil { 64 return 65 } 66 if o.Context == nil { 67 o.Context = context.Background() 68 } 69 o.Context = context.WithValue(o.Context, consulQueryOptionsKey, q) 70 } 71 } 72 73 // TCPCheck will tell the service provider to check the service address 74 // and port every `t` interval. It will enabled only if `t` is greater than 0. 75 // See `TCP + Interval` for more information [1]. 76 // 77 // [1] https://www.consul.io/docs/agent/checks.html 78 func TCPCheck(t time.Duration) registry.Option { 79 return func(o *registry.Options) { 80 if t <= time.Duration(0) { 81 return 82 } 83 if o.Context == nil { 84 o.Context = context.Background() 85 } 86 o.Context = context.WithValue(o.Context, consulTCPCheckKey, t) 87 } 88 } 89 90 // HTTPCheck will tell the service provider to invoke the health check endpoint 91 // with an interval and timeout. It will be enabled only if interval and 92 // timeout are greater than 0. 93 // See `HTTP + Interval` for more information [1]. 94 // 95 // [1] https://www.consul.io/docs/agent/checks.html 96 func HTTPCheck(protocol, port, httpEndpoint string, interval, timeout time.Duration) registry.Option { 97 return func(o *registry.Options) { 98 if interval <= time.Duration(0) || timeout <= time.Duration(0) { 99 return 100 } 101 if o.Context == nil { 102 o.Context = context.Background() 103 } 104 check := consul.AgentServiceCheck{ 105 HTTP: fmt.Sprintf("%s://{host}:%s%s", protocol, port, httpEndpoint), 106 Interval: fmt.Sprintf("%v", interval), 107 Timeout: fmt.Sprintf("%v", timeout), 108 } 109 o.Context = context.WithValue(o.Context, consulHTTPCheckConfigKey, check) 110 } 111 }