-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(natsjsregistry): spread load evenly among instances
Signed-off-by: jkoberg <[email protected]>
- Loading branch information
Showing
3 changed files
with
127 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Fix: Repair nats-js-kv registry | ||
|
||
The registry would always send traffic to only one pod. This is now fixed and load should be spread evenly. Also implements watcher method so the cache can use it. | ||
|
||
https://github.com/owncloud/ocis/pull/9618 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package natsjsregistry | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/nats-io/nats.go" | ||
"go-micro.dev/v4/registry" | ||
) | ||
|
||
// NatsWatcher is the watcher of the nats interface | ||
type NatsWatcher interface { | ||
Watch(bucket string) (nats.KeyWatcher, error) | ||
} | ||
|
||
// Watcher is used to keep track of changes in the registry | ||
type Watcher struct { | ||
watch nats.KeyWatcher | ||
updates <-chan nats.KeyValueEntry | ||
reg *storeregistry | ||
} | ||
|
||
// NewWatcher returns a new watcher | ||
func NewWatcher(s *storeregistry) (*Watcher, error) { | ||
w, ok := s.store.(NatsWatcher) | ||
if !ok { | ||
return nil, errors.New("store does not implement watcher interface") | ||
} | ||
|
||
watcher, err := w.Watch("service-registry") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Watcher{ | ||
watch: watcher, | ||
updates: watcher.Updates(), | ||
reg: s, | ||
}, nil | ||
} | ||
|
||
// Next returns the next result. It is a blocking call | ||
func (w *Watcher) Next() (*registry.Result, error) { | ||
kve := <-w.updates | ||
if kve == nil { | ||
return nil, errors.New("watcher stopped") | ||
} | ||
|
||
service, err := w.reg.getService(kve.Key()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var action string | ||
switch kve.Operation() { | ||
default: | ||
action = "create" | ||
case nats.KeyValuePut: | ||
action = "create" | ||
case nats.KeyValueDelete: | ||
action = "delete" | ||
case nats.KeyValuePurge: | ||
action = "delete" | ||
} | ||
|
||
return ®istry.Result{ | ||
Service: service, | ||
Action: action, | ||
}, nil | ||
} | ||
|
||
// Stop stops the watcher | ||
func (w *Watcher) Stop() { | ||
_ = w.watch.Stop() | ||
} |