1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-11-23 21:44:41 +02:00
Files
go-micro/registry/nats/nats_options.go
Brian Ketelsen cd2b40ca4a Add a selection of plugins to the core repo (#2755)
* WIP

* fix: default memory registry, add registrations for mdns, nats

* fix: same for broker

* fix: add more

* fix: http port

* rename redis

* chore: linting
2025-05-15 18:47:35 +01:00

88 lines
2.3 KiB
Go

package nats
import (
"context"
"github.com/nats-io/nats.go"
"go-micro.dev/v5/registry"
)
type contextQuorumKey struct{}
type optionsKey struct{}
type watchTopicKey struct{}
type queryTopicKey struct{}
type registerActionKey struct{}
var (
DefaultQuorum = 0
)
func getQuorum(o registry.Options) int {
if o.Context == nil {
return DefaultQuorum
}
value := o.Context.Value(contextQuorumKey{})
if v, ok := value.(int); ok {
return v
} else {
return DefaultQuorum
}
}
func Quorum(n int) registry.Option {
return func(o *registry.Options) {
o.Context = context.WithValue(o.Context, contextQuorumKey{}, n)
}
}
// Options allow to inject a nats.Options struct for configuring
// the nats connection.
func NatsOptions(nopts nats.Options) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, optionsKey{}, nopts)
}
}
// QueryTopic allows to set a custom nats topic on which service registries
// query (survey) other services. All registries listen on this topic and
// then respond to the query message.
func QueryTopic(s string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, queryTopicKey{}, s)
}
}
// WatchTopic allows to set a custom nats topic on which registries broadcast
// changes (e.g. when services are added, updated or removed). Since we don't
// have a central registry service, each service typically broadcasts in a
// determined frequency on this topic.
func WatchTopic(s string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, watchTopicKey{}, s)
}
}
// RegisterAction allows to set the action to use when registering to nats.
// As of now there are three different options:
// - "create" (default) only registers if there is noone already registered under the same key.
// - "update" only updates the registration if it already exists.
// - "put" creates or updates a registration
func RegisterAction(s string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, registerActionKey{}, s)
}
}