github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/registry/consul/config.go (about)

     1  package consul
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	consul "github.com/hashicorp/consul/api"
     8  	"github.com/volts-dev/volts/registry"
     9  )
    10  
    11  // Connect specifies services should be registered as Consul Connect services
    12  func Connect() registry.Option {
    13  	return func(o *registry.Config) {
    14  		if o.Context == nil {
    15  			o.Context = context.Background()
    16  		}
    17  		o.Context = context.WithValue(o.Context, "consul_connect", true)
    18  	}
    19  }
    20  
    21  func Config(c *consul.Config) registry.Option {
    22  	return func(o *registry.Config) {
    23  		if o.Context == nil {
    24  			o.Context = context.Background()
    25  		}
    26  		o.Context = context.WithValue(o.Context, "consul_config", c)
    27  	}
    28  }
    29  
    30  // AllowStale sets whether any Consul server (non-leader) can service
    31  // a read. This allows for lower latency and higher throughput
    32  // at the cost of potentially stale data.
    33  // Works similar to Consul DNS Config option [1].
    34  // Defaults to true.
    35  //
    36  // [1] https://www.consul.io/docs/agent/options.html#allow_stale
    37  //
    38  func AllowStale(v bool) registry.Option {
    39  	return func(o *registry.Config) {
    40  		if o.Context == nil {
    41  			o.Context = context.Background()
    42  		}
    43  		o.Context = context.WithValue(o.Context, "consul_allow_stale", v)
    44  	}
    45  }
    46  
    47  // QueryOptions specifies the QueryOptions to be used when calling
    48  // Consul. See `Consul API` for more information [1].
    49  //
    50  // [1] https://godoc.org/github.com/hashicorp/consul/api#QueryOptions
    51  //
    52  func QueryOptions(q *consul.QueryOptions) registry.Option {
    53  	return func(o *registry.Config) {
    54  		if q == nil {
    55  			return
    56  		}
    57  		if o.Context == nil {
    58  			o.Context = context.Background()
    59  		}
    60  		o.Context = context.WithValue(o.Context, "consul_query_options", q)
    61  	}
    62  }
    63  
    64  //
    65  // TCPCheck will tell the service provider to check the service address
    66  // and port every `t` interval. It will enabled only if `t` is greater than 0.
    67  // See `TCP + Interval` for more information [1].
    68  //
    69  // [1] https://www.consul.io/docs/agent/checks.html
    70  //
    71  func TCPCheck(t time.Duration) registry.Option {
    72  	return func(o *registry.Config) {
    73  		if t <= time.Duration(0) {
    74  			return
    75  		}
    76  		if o.Context == nil {
    77  			o.Context = context.Background()
    78  		}
    79  		o.Context = context.WithValue(o.Context, "consul_tcp_check", t)
    80  	}
    81  }