go-micro.dev/v5@v5.12.0/registry/nats/nats_options.go (about) 1 package nats 2 3 import ( 4 "context" 5 6 "github.com/nats-io/nats.go" 7 "go-micro.dev/v5/registry" 8 ) 9 10 type contextQuorumKey struct{} 11 type optionsKey struct{} 12 type watchTopicKey struct{} 13 type queryTopicKey struct{} 14 type registerActionKey struct{} 15 16 var ( 17 DefaultQuorum = 0 18 ) 19 20 func getQuorum(o registry.Options) int { 21 if o.Context == nil { 22 return DefaultQuorum 23 } 24 25 value := o.Context.Value(contextQuorumKey{}) 26 if v, ok := value.(int); ok { 27 return v 28 } else { 29 return DefaultQuorum 30 } 31 } 32 33 func Quorum(n int) registry.Option { 34 return func(o *registry.Options) { 35 o.Context = context.WithValue(o.Context, contextQuorumKey{}, n) 36 } 37 } 38 39 // Options allow to inject a nats.Options struct for configuring 40 // the nats connection. 41 func NatsOptions(nopts nats.Options) registry.Option { 42 return func(o *registry.Options) { 43 if o.Context == nil { 44 o.Context = context.Background() 45 } 46 o.Context = context.WithValue(o.Context, optionsKey{}, nopts) 47 } 48 } 49 50 // QueryTopic allows to set a custom nats topic on which service registries 51 // query (survey) other services. All registries listen on this topic and 52 // then respond to the query message. 53 func QueryTopic(s string) registry.Option { 54 return func(o *registry.Options) { 55 if o.Context == nil { 56 o.Context = context.Background() 57 } 58 o.Context = context.WithValue(o.Context, queryTopicKey{}, s) 59 } 60 } 61 62 // WatchTopic allows to set a custom nats topic on which registries broadcast 63 // changes (e.g. when services are added, updated or removed). Since we don't 64 // have a central registry service, each service typically broadcasts in a 65 // determined frequency on this topic. 66 func WatchTopic(s string) registry.Option { 67 return func(o *registry.Options) { 68 if o.Context == nil { 69 o.Context = context.Background() 70 } 71 o.Context = context.WithValue(o.Context, watchTopicKey{}, s) 72 } 73 } 74 75 // RegisterAction allows to set the action to use when registering to nats. 76 // As of now there are three different options: 77 // - "create" (default) only registers if there is noone already registered under the same key. 78 // - "update" only updates the registration if it already exists. 79 // - "put" creates or updates a registration 80 func RegisterAction(s string) registry.Option { 81 return func(o *registry.Options) { 82 if o.Context == nil { 83 o.Context = context.Background() 84 } 85 o.Context = context.WithValue(o.Context, registerActionKey{}, s) 86 } 87 }