diff --git a/changelog/unreleased/fix-nats-registry.md b/changelog/unreleased/fix-nats-registry.md new file mode 100644 index 00000000000..02df7a66969 --- /dev/null +++ b/changelog/unreleased/fix-nats-registry.md @@ -0,0 +1,6 @@ +Bugfix: Fix nats registry + +Using `nats` as service registry did work, but when a service would restart and gets a new ip it couldn't re-register. +We fixed this by using `"put"` register action instead of the default `"create"` + +https://github.com/owncloud/ocis/pull/6881 diff --git a/go.mod b/go.mod index f70d4cb0bb4..93115ba28e2 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( github.com/go-micro/plugins/v4/registry/kubernetes v1.1.2-0.20230605104008-a179a6b8f8e6 github.com/go-micro/plugins/v4/registry/mdns v1.2.0 github.com/go-micro/plugins/v4/registry/memory v1.2.0 - github.com/go-micro/plugins/v4/registry/nats v1.2.1 + github.com/go-micro/plugins/v4/registry/nats v1.2.2-0.20230723205323-1ada01245674 github.com/go-micro/plugins/v4/server/grpc v1.2.0 github.com/go-micro/plugins/v4/server/http v1.2.1 github.com/go-micro/plugins/v4/wrapper/breaker/gobreaker v1.2.0 diff --git a/go.sum b/go.sum index 1bf75b7a229..1aeaed70f94 100644 --- a/go.sum +++ b/go.sum @@ -790,8 +790,8 @@ github.com/go-micro/plugins/v4/registry/mdns v1.2.0 h1:BsGnco+PgycvSX+HS0XbeUQEP github.com/go-micro/plugins/v4/registry/mdns v1.2.0/go.mod h1:re0JvO5F56n59WEDaAKj2jtboKa2dklAd6iWyz5xa54= github.com/go-micro/plugins/v4/registry/memory v1.2.0 h1:R0G2tltffuG+fQnk+/JuAdgEJX4J+LuOafZDoNd8ow0= github.com/go-micro/plugins/v4/registry/memory v1.2.0/go.mod h1:4t5YiXJT5BVtMWImxy807lY3ywjv/PHpdHnN+LXSsI4= -github.com/go-micro/plugins/v4/registry/nats v1.2.1 h1:YsGmSYxhdu9NK7d8NYMY/xhaYMB4AhcCWoSrk7YGdEY= -github.com/go-micro/plugins/v4/registry/nats v1.2.1/go.mod h1:RDsrDhcjJggCzAvvUzo/Bzy68d9s9+tu0KOfofXVCog= +github.com/go-micro/plugins/v4/registry/nats v1.2.2-0.20230723205323-1ada01245674 h1:NBjrNT5TKhKB0ENBGpse+FRCY6GQAeS8LxKrspMKZs0= +github.com/go-micro/plugins/v4/registry/nats v1.2.2-0.20230723205323-1ada01245674/go.mod h1:RDsrDhcjJggCzAvvUzo/Bzy68d9s9+tu0KOfofXVCog= github.com/go-micro/plugins/v4/server/grpc v1.2.0 h1:lXfM+/0oE/u1g0hVBYsvbP4lYOYXYOmwf5qH7ghi7Cc= github.com/go-micro/plugins/v4/server/grpc v1.2.0/go.mod h1:+Ah9Pf/vMSXxBM3fup/hc3N+zN2as3nIpcRaR4sBjnY= github.com/go-micro/plugins/v4/server/http v1.2.1 h1:Cia924J90rgFT/4qWWvyLvN+XqEm5T9tiQyQ+GU4bOQ= diff --git a/ocis-pkg/registry/registry.go b/ocis-pkg/registry/registry.go index 6b4ccda0159..dd2245cfa4a 100644 --- a/ocis-pkg/registry/registry.go +++ b/ocis-pkg/registry/registry.go @@ -50,6 +50,7 @@ func GetRegistry() mRegistry.Registry { case "nats": reg = natsr.NewRegistry( mRegistry.Addrs(addresses...), + natsr.RegisterAction("put"), ) case "kubernetes": reg = kubernetesr.NewRegistry( diff --git a/vendor/github.com/go-micro/plugins/v4/registry/nats/nats.go b/vendor/github.com/go-micro/plugins/v4/registry/nats/nats.go index 2b7ee173ea5..6e0d6ebfae3 100644 --- a/vendor/github.com/go-micro/plugins/v4/registry/nats/nats.go +++ b/vendor/github.com/go-micro/plugins/v4/registry/nats/nats.go @@ -14,11 +14,12 @@ import ( ) type natsRegistry struct { - addrs []string - opts registry.Options - nopts nats.Options - queryTopic string - watchTopic string + addrs []string + opts registry.Options + nopts nats.Options + queryTopic string + watchTopic string + registerAction string sync.RWMutex conn *nats.Conn @@ -27,8 +28,9 @@ type natsRegistry struct { } var ( - defaultQueryTopic = "micro.registry.nats.query" - defaultWatchTopic = "micro.registry.nats.watch" + defaultQueryTopic = "micro.registry.nats.query" + defaultWatchTopic = "micro.registry.nats.watch" + defaultRegisterAction = "create" ) func init() { @@ -55,6 +57,11 @@ func configure(n *natsRegistry, opts ...registry.Option) error { watchTopic = wt } + registerAction := defaultRegisterAction + if ra, ok := n.opts.Context.Value(registerActionKey{}).(string); ok { + registerAction = ra + } + // registry.Options have higher priority than nats.Options // only if Addrs, Secure or TLSConfig were not set through a registry.Option // we read them from nats.Option @@ -78,6 +85,7 @@ func configure(n *natsRegistry, opts ...registry.Option) error { n.nopts = natsOptions n.queryTopic = queryTopic n.watchTopic = watchTopic + n.registerAction = registerAction return nil } @@ -322,7 +330,7 @@ func (n *natsRegistry) Register(s *registry.Service, opts ...registry.RegisterOp return err } - b, err := json.Marshal(®istry.Result{Action: "create", Service: s}) + b, err := json.Marshal(®istry.Result{Action: n.registerAction, Service: s}) if err != nil { return err } diff --git a/vendor/github.com/go-micro/plugins/v4/registry/nats/options.go b/vendor/github.com/go-micro/plugins/v4/registry/nats/options.go index a3efb6782b5..22db3b94e04 100644 --- a/vendor/github.com/go-micro/plugins/v4/registry/nats/options.go +++ b/vendor/github.com/go-micro/plugins/v4/registry/nats/options.go @@ -11,6 +11,7 @@ type contextQuorumKey struct{} type optionsKey struct{} type watchTopicKey struct{} type queryTopicKey struct{} +type registerActionKey struct{} var ( DefaultQuorum = 0 @@ -70,3 +71,17 @@ func WatchTopic(s string) registry.Option { 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) + } +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 5e7b0860d48..f9179968c3a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -908,7 +908,7 @@ github.com/go-micro/plugins/v4/registry/mdns # github.com/go-micro/plugins/v4/registry/memory v1.2.0 ## explicit; go 1.17 github.com/go-micro/plugins/v4/registry/memory -# github.com/go-micro/plugins/v4/registry/nats v1.2.1 +# github.com/go-micro/plugins/v4/registry/nats v1.2.2-0.20230723205323-1ada01245674 ## explicit; go 1.17 github.com/go-micro/plugins/v4/registry/nats # github.com/go-micro/plugins/v4/server/grpc v1.2.0