2024-02-28 23:51:05 +03:00
|
|
|
package memory
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-28 23:51:05 +03:00
|
|
|
"go.unistack.org/micro/v4/register"
|
2021-02-12 16:33:16 +03:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-04-11 22:20:37 +03:00
|
|
|
"go.unistack.org/micro/v4/logger"
|
|
|
|
"go.unistack.org/micro/v4/util/id"
|
2021-02-12 16:33:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
sendEventTime = 10 * time.Millisecond
|
|
|
|
ttlPruneTime = time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
LastSeen time.Time
|
2024-02-28 23:51:05 +03:00
|
|
|
*register.Node
|
2021-03-06 19:45:13 +03:00
|
|
|
TTL time.Duration
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type record struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
Metadata map[string]string
|
|
|
|
Nodes map[string]*node
|
2024-02-28 23:51:05 +03:00
|
|
|
Endpoints []*register.Endpoint
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type memory struct {
|
2022-03-30 15:37:02 +03:00
|
|
|
sync.RWMutex
|
2021-02-12 16:33:16 +03:00
|
|
|
records map[string]services
|
|
|
|
watchers map[string]*watcher
|
2024-02-28 23:51:05 +03:00
|
|
|
opts register.Options
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// services is a KV map with service name as the key and a map of records as the value
|
|
|
|
type services map[string]map[string]*record
|
|
|
|
|
|
|
|
// NewRegister returns an initialized in-memory register
|
2024-02-28 23:51:05 +03:00
|
|
|
func NewRegister(opts ...register.Option) register.Register {
|
2021-02-12 16:33:16 +03:00
|
|
|
r := &memory{
|
2024-02-28 23:51:05 +03:00
|
|
|
opts: register.NewOptions(opts...),
|
2021-02-12 16:33:16 +03:00
|
|
|
records: make(map[string]services),
|
|
|
|
watchers: make(map[string]*watcher),
|
|
|
|
}
|
|
|
|
|
|
|
|
go r.ttlPrune()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) ttlPrune() {
|
|
|
|
prune := time.NewTicker(ttlPruneTime)
|
|
|
|
defer prune.Stop()
|
|
|
|
|
2021-02-13 01:46:16 +03:00
|
|
|
for range prune.C {
|
|
|
|
m.Lock()
|
|
|
|
for domain, services := range m.records {
|
|
|
|
for service, versions := range services {
|
|
|
|
for version, record := range versions {
|
|
|
|
for id, n := range record.Nodes {
|
|
|
|
if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL {
|
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "RegisterTTL expired for node "+n.ID+" of service "+service)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
2021-02-13 01:46:16 +03:00
|
|
|
delete(m.records[domain][service][version].Nodes, id)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-13 01:46:16 +03:00
|
|
|
m.Unlock()
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) sendEvent(r *register.Result) {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.RLock()
|
|
|
|
watchers := make([]*watcher, 0, len(m.watchers))
|
|
|
|
for _, w := range m.watchers {
|
|
|
|
watchers = append(watchers, w)
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
|
|
|
|
for _, w := range watchers {
|
|
|
|
select {
|
|
|
|
case <-w.exit:
|
|
|
|
m.Lock()
|
|
|
|
delete(m.watchers, w.id)
|
|
|
|
m.Unlock()
|
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case w.res <- r:
|
|
|
|
case <-time.After(sendEventTime):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) Connect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) Disconnect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) Init(opts ...register.Option) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add services
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) Options() register.Options {
|
2021-02-12 16:33:16 +03:00
|
|
|
return m.opts
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) Register(ctx context.Context, s *register.Service, opts ...register.RegisterOption) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
options := register.NewRegisterOptions(opts...)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
// get the services for this domain from the register
|
|
|
|
srvs, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
|
|
|
srvs = make(services)
|
|
|
|
}
|
|
|
|
|
|
|
|
// domain is set in metadata so it can be passed to watchers
|
|
|
|
if s.Metadata == nil {
|
|
|
|
s.Metadata = map[string]string{"domain": options.Domain}
|
|
|
|
} else {
|
|
|
|
s.Metadata["domain"] = options.Domain
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure the service name exists
|
|
|
|
r := serviceToRecord(s, options.TTL)
|
|
|
|
if _, ok := srvs[s.Name]; !ok {
|
|
|
|
srvs[s.Name] = make(map[string]*record)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := srvs[s.Name][s.Version]; !ok {
|
|
|
|
srvs[s.Name][s.Version] = r
|
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "register added new service: "+s.Name+", version "+s.Version)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
m.records[options.Domain] = srvs
|
2024-02-28 23:51:05 +03:00
|
|
|
go m.sendEvent(®ister.Result{Action: "create", Service: s})
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var addedNodes bool
|
|
|
|
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
// check if already exists
|
2021-04-27 08:32:47 +03:00
|
|
|
if _, ok := srvs[s.Name][s.Version].Nodes[n.ID]; ok {
|
2021-02-12 16:33:16 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-07 14:53:58 +03:00
|
|
|
metadata := make(map[string]string, len(n.Metadata))
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
// make copy of metadata
|
|
|
|
for k, v := range n.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the domain
|
|
|
|
metadata["domain"] = options.Domain
|
|
|
|
|
|
|
|
// add the node
|
2021-04-27 08:32:47 +03:00
|
|
|
srvs[s.Name][s.Version].Nodes[n.ID] = &node{
|
2024-02-28 23:51:05 +03:00
|
|
|
Node: ®ister.Node{
|
2021-04-27 08:32:47 +03:00
|
|
|
ID: n.ID,
|
2021-02-12 16:33:16 +03:00
|
|
|
Address: n.Address,
|
|
|
|
Metadata: metadata,
|
|
|
|
},
|
|
|
|
TTL: options.TTL,
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
addedNodes = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if addedNodes {
|
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "register added new node to service: "+s.Name+", version "+s.Version)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
2024-02-28 23:51:05 +03:00
|
|
|
go m.sendEvent(®ister.Result{Action: "update", Service: s})
|
2021-02-12 16:33:16 +03:00
|
|
|
} else {
|
|
|
|
// refresh TTL and timestamp
|
|
|
|
for _, n := range s.Nodes {
|
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "updated registration for service: "+s.Name+", version "+s.Version)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
srvs[s.Name][s.Version].Nodes[n.ID].TTL = options.TTL
|
|
|
|
srvs[s.Name][s.Version].Nodes[n.ID].LastSeen = time.Now()
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.records[options.Domain] = srvs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...register.DeregisterOption) error {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
options := register.NewDeregisterOptions(opts...)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
// domain is set in metadata so it can be passed to watchers
|
|
|
|
if s.Metadata == nil {
|
|
|
|
s.Metadata = map[string]string{"domain": options.Domain}
|
|
|
|
} else {
|
|
|
|
s.Metadata["domain"] = options.Domain
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the domain doesn't exist, there is nothing to deregister
|
|
|
|
services, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no services with this name and version exist, there is nothing to deregister
|
|
|
|
versions, ok := services[s.Name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
version, ok := versions[s.Version]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deregister all of the service nodes from this version
|
|
|
|
for _, n := range s.Nodes {
|
2021-04-27 08:32:47 +03:00
|
|
|
if _, ok := version.Nodes[n.ID]; ok {
|
2021-02-12 16:33:16 +03:00
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "register removed node from service: "+s.Name+", version "+s.Version)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
delete(version.Nodes, n.ID)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the nodes not empty, we replace the version in the store and exist, the rest of the logic
|
|
|
|
// is cleanup
|
|
|
|
if len(version.Nodes) > 0 {
|
|
|
|
m.records[options.Domain][s.Name][s.Version] = version
|
2024-02-28 23:51:05 +03:00
|
|
|
go m.sendEvent(®ister.Result{Action: "update", Service: s})
|
2021-02-12 16:33:16 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this version was the only version of the service, we can remove the whole service from the
|
|
|
|
// register and exit
|
|
|
|
if len(versions) == 1 {
|
|
|
|
delete(m.records[options.Domain], s.Name)
|
2024-02-28 23:51:05 +03:00
|
|
|
go m.sendEvent(®ister.Result{Action: "delete", Service: s})
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "register removed service: "+s.Name)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// there are other versions of the service running, so only remove this version of it
|
|
|
|
delete(m.records[options.Domain][s.Name], s.Version)
|
2024-02-28 23:51:05 +03:00
|
|
|
go m.sendEvent(®ister.Result{Action: "delete", Service: s})
|
2021-02-12 16:33:16 +03:00
|
|
|
if m.opts.Logger.V(logger.DebugLevel) {
|
2023-10-14 18:59:27 +03:00
|
|
|
m.opts.Logger.Debug(m.opts.Context, "register removed service: "+s.Name+", version "+s.Version)
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) LookupService(ctx context.Context, name string, opts ...register.LookupOption) ([]*register.Service, error) {
|
|
|
|
options := register.NewLookupOptions(opts...)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
// if it's a wildcard domain, return from all domains
|
2024-02-28 23:51:05 +03:00
|
|
|
if options.Domain == register.WildcardDomain {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.RLock()
|
|
|
|
recs := m.records
|
|
|
|
m.RUnlock()
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
var services []*register.Service
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
for domain := range recs {
|
2024-02-28 23:51:05 +03:00
|
|
|
srvs, err := m.LookupService(ctx, name, append(opts, register.LookupDomain(domain))...)
|
|
|
|
if err == register.ErrNotFound {
|
2021-02-12 16:33:16 +03:00
|
|
|
continue
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
services = append(services, srvs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services) == 0 {
|
2024-02-28 23:51:05 +03:00
|
|
|
return nil, register.ErrNotFound
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
// check the domain exists
|
|
|
|
services, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
2024-02-28 23:51:05 +03:00
|
|
|
return nil, register.ErrNotFound
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the service exists
|
|
|
|
versions, ok := services[name]
|
|
|
|
if !ok || len(versions) == 0 {
|
2024-02-28 23:51:05 +03:00
|
|
|
return nil, register.ErrNotFound
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// serialize the response
|
2024-02-28 23:51:05 +03:00
|
|
|
result := make([]*register.Service, len(versions))
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
var i int
|
|
|
|
|
|
|
|
for _, r := range versions {
|
|
|
|
result[i] = recordToService(r, options.Domain)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) ListServices(ctx context.Context, opts ...register.ListOption) ([]*register.Service, error) {
|
|
|
|
options := register.NewListOptions(opts...)
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
// if it's a wildcard domain, list from all domains
|
2024-02-28 23:51:05 +03:00
|
|
|
if options.Domain == register.WildcardDomain {
|
2021-02-12 16:33:16 +03:00
|
|
|
m.RLock()
|
|
|
|
recs := m.records
|
|
|
|
m.RUnlock()
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
var services []*register.Service
|
2021-02-12 16:33:16 +03:00
|
|
|
|
|
|
|
for domain := range recs {
|
2024-02-28 23:51:05 +03:00
|
|
|
srvs, err := m.ListServices(ctx, append(opts, register.ListDomain(domain))...)
|
2021-02-12 16:33:16 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
services = append(services, srvs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
|
|
|
|
|
|
|
// ensure the domain exists
|
|
|
|
services, ok := m.records[options.Domain]
|
|
|
|
if !ok {
|
2024-02-28 23:51:05 +03:00
|
|
|
return make([]*register.Service, 0), nil
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// serialize the result, each version counts as an individual service
|
2024-02-28 23:51:05 +03:00
|
|
|
var result []*register.Service
|
2021-02-12 16:33:16 +03:00
|
|
|
|
2021-03-03 18:16:54 +03:00
|
|
|
for _, service := range services {
|
2021-02-12 16:33:16 +03:00
|
|
|
for _, version := range service {
|
2021-03-03 18:16:54 +03:00
|
|
|
result = append(result, recordToService(version, options.Domain))
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *memory) Watch(ctx context.Context, opts ...register.WatchOption) (register.Watcher, error) {
|
2021-08-20 22:40:48 +03:00
|
|
|
id, err := id.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-28 23:51:05 +03:00
|
|
|
wo := register.NewWatchOptions(opts...)
|
2021-02-12 16:33:16 +03:00
|
|
|
// construct the watcher
|
|
|
|
w := &watcher{
|
|
|
|
exit: make(chan bool),
|
2024-02-28 23:51:05 +03:00
|
|
|
res: make(chan *register.Result),
|
2021-08-20 22:40:48 +03:00
|
|
|
id: id,
|
2021-02-12 16:33:16 +03:00
|
|
|
wo: wo,
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Lock()
|
|
|
|
m.watchers[w.id] = w
|
|
|
|
m.Unlock()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *memory) String() string {
|
|
|
|
return "memory"
|
|
|
|
}
|
|
|
|
|
|
|
|
type watcher struct {
|
2024-02-28 23:51:05 +03:00
|
|
|
res chan *register.Result
|
2021-02-12 16:33:16 +03:00
|
|
|
exit chan bool
|
2024-02-28 23:51:05 +03:00
|
|
|
wo register.WatchOptions
|
2021-03-06 19:45:13 +03:00
|
|
|
id string
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func (m *watcher) Next() (*register.Result, error) {
|
2021-02-12 16:33:16 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r := <-m.res:
|
|
|
|
if r.Service == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(m.wo.Service) > 0 && m.wo.Service != r.Service.Name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract domain from service metadata
|
|
|
|
var domain string
|
|
|
|
if r.Service.Metadata != nil && len(r.Service.Metadata["domain"]) > 0 {
|
|
|
|
domain = r.Service.Metadata["domain"]
|
|
|
|
} else {
|
2024-02-28 23:51:05 +03:00
|
|
|
domain = register.DefaultDomain
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// only send the event if watching the wildcard or this specific domain
|
2024-02-28 23:51:05 +03:00
|
|
|
if m.wo.Domain == register.WildcardDomain || m.wo.Domain == domain {
|
2021-02-12 16:33:16 +03:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
case <-m.exit:
|
2024-02-28 23:51:05 +03:00
|
|
|
return nil, register.ErrWatcherStopped
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *watcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-m.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(m.exit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func serviceToRecord(s *register.Service, ttl time.Duration) *record {
|
2021-02-12 16:33:16 +03:00
|
|
|
metadata := make(map[string]string, len(s.Metadata))
|
|
|
|
for k, v := range s.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := make(map[string]*node, len(s.Nodes))
|
|
|
|
for _, n := range s.Nodes {
|
2021-04-27 08:32:47 +03:00
|
|
|
nodes[n.ID] = &node{
|
2021-02-12 16:33:16 +03:00
|
|
|
Node: n,
|
|
|
|
TTL: ttl,
|
|
|
|
LastSeen: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
endpoints := make([]*register.Endpoint, len(s.Endpoints))
|
2023-10-14 18:59:27 +03:00
|
|
|
for i, e := range s.Endpoints { // TODO: vtolstov use copy
|
2021-02-12 16:33:16 +03:00
|
|
|
endpoints[i] = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return &record{
|
|
|
|
Name: s.Name,
|
|
|
|
Version: s.Version,
|
|
|
|
Metadata: metadata,
|
|
|
|
Nodes: nodes,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
func recordToService(r *record, domain string) *register.Service {
|
2021-02-12 16:33:16 +03:00
|
|
|
metadata := make(map[string]string, len(r.Metadata))
|
|
|
|
for k, v := range r.Metadata {
|
|
|
|
metadata[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the domain in metadata so it can be determined when a wildcard query is performed
|
|
|
|
metadata["domain"] = domain
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
endpoints := make([]*register.Endpoint, len(r.Endpoints))
|
2021-02-12 16:33:16 +03:00
|
|
|
for i, e := range r.Endpoints {
|
2021-04-27 08:32:47 +03:00
|
|
|
md := make(map[string]string, len(e.Metadata))
|
2021-02-12 16:33:16 +03:00
|
|
|
for k, v := range e.Metadata {
|
2021-04-27 08:32:47 +03:00
|
|
|
md[k] = v
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
endpoints[i] = ®ister.Endpoint{
|
2021-02-12 16:33:16 +03:00
|
|
|
Name: e.Name,
|
2021-04-20 12:39:21 +03:00
|
|
|
Request: e.Request,
|
|
|
|
Response: e.Response,
|
2021-04-27 08:32:47 +03:00
|
|
|
Metadata: md,
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
nodes := make([]*register.Node, len(r.Nodes))
|
2021-02-12 16:33:16 +03:00
|
|
|
i := 0
|
|
|
|
for _, n := range r.Nodes {
|
2021-04-27 08:32:47 +03:00
|
|
|
md := make(map[string]string, len(n.Metadata))
|
2021-02-12 16:33:16 +03:00
|
|
|
for k, v := range n.Metadata {
|
2021-04-27 08:32:47 +03:00
|
|
|
md[k] = v
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
nodes[i] = ®ister.Node{
|
2021-04-27 08:32:47 +03:00
|
|
|
ID: n.ID,
|
2021-02-12 16:33:16 +03:00
|
|
|
Address: n.Address,
|
2021-04-27 08:32:47 +03:00
|
|
|
Metadata: md,
|
2021-02-12 16:33:16 +03:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2024-02-28 23:51:05 +03:00
|
|
|
return ®ister.Service{
|
2021-02-12 16:33:16 +03:00
|
|
|
Name: r.Name,
|
|
|
|
Version: r.Version,
|
|
|
|
Metadata: metadata,
|
|
|
|
Endpoints: endpoints,
|
|
|
|
Nodes: nodes,
|
|
|
|
}
|
|
|
|
}
|