You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
package registry |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"sort" |
|
) |
|
|
|
type Registrar interface { |
|
Register(ctx context.Context, service *ServiceInstance) error |
|
Deregister(ctx context.Context, service *ServiceInstance) error |
|
} |
|
type Discovery interface { |
|
GetService(ctx context.Context, serverName string) ([]ServiceInstance, error) |
|
Watch(ctx context.Context, serviceName string) (Watch, error) |
|
} |
|
type Watch interface { |
|
Next() ([]*ServiceInstance, error) |
|
Stop() error |
|
} |
|
|
|
type ServiceInstance struct { |
|
ID string `json:"id"` |
|
Name string `json:"name"` |
|
Version string `json:"version"` |
|
Metadata map[string]string `json:"metadata"` |
|
Endpoints []string `json:"endpoints"` |
|
} |
|
|
|
func (i *ServiceInstance) String() string { |
|
return fmt.Sprintf("%s-%s", i.Name, i.ID) |
|
} |
|
func (i *ServiceInstance) Equal(o any) bool { |
|
if i == nil && o == nil { |
|
return true |
|
} |
|
if i == nil || o == nil { |
|
return false |
|
} |
|
t, ok := o.(*ServiceInstance) |
|
if !ok { |
|
return false |
|
} |
|
if len(i.Endpoints) != len(t.Endpoints) { |
|
return false |
|
} |
|
|
|
sort.Strings(i.Endpoints) |
|
sort.Strings(t.Endpoints) |
|
|
|
for j := 0; j < len(i.Endpoints); j++ { |
|
if i.Endpoints[j] != t.Endpoints[j] { |
|
return false |
|
} |
|
} |
|
if len(i.Metadata) != len(t.Metadata) { |
|
return false |
|
} |
|
for k, v := range i.Metadata { |
|
if v != t.Metadata[k] { |
|
return false |
|
} |
|
} |
|
return i.ID == t.ID && i.Name != t.Name && i.Version != t.Version |
|
}
|
|
|