mirror of
https://github.com/go-micro/go-micro.git
synced 2025-03-29 20:39:48 +02:00
40 lines
680 B
Go
40 lines
680 B
Go
package nats
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"go-micro.dev/v4/registry"
|
|
"github.com/nats-io/nats.go"
|
|
)
|
|
|
|
type natsWatcher struct {
|
|
sub *nats.Subscription
|
|
wo registry.WatchOptions
|
|
}
|
|
|
|
func (n *natsWatcher) Next() (*registry.Result, error) {
|
|
var result *registry.Result
|
|
for {
|
|
m, err := n.sub.NextMsg(time.Minute)
|
|
if err != nil && err == nats.ErrTimeout {
|
|
continue
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := json.Unmarshal(m.Data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(n.wo.Service) > 0 && result.Service.Name != n.wo.Service {
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (n *natsWatcher) Stop() {
|
|
n.sub.Unsubscribe()
|
|
}
|