refactor(config): service registry is a static map, not init() registration (#1926)
This commit is contained in:
2 files changed
+32
-53
No files matched your search
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// Services maps a registered service name to its connection settings. Names
|
||||
// come from RegisterService.
|
||||
// come from the registry in servicespec.go.
|
||||
type Services map[string]ServiceConnection
|
||||
|
||||
// Get returns the connection configured for name, or the zero value when the
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Service contexts. The context decides which addresses a service must end up
|
||||
@@ -36,7 +33,7 @@ type ServiceSpec struct {
|
||||
Listen string
|
||||
}
|
||||
|
||||
// Registered service names. They live beside the registrations below so the
|
||||
// Registered service names. They live beside the registry below so the
|
||||
// spelling a consumer looks up and the spelling that was registered are the
|
||||
// same token, not two string literals that a typo can separate.
|
||||
const (
|
||||
@@ -46,37 +43,34 @@ const (
|
||||
ServiceNameDaemon = "daemon"
|
||||
)
|
||||
|
||||
var (
|
||||
serviceRegistryMu sync.RWMutex
|
||||
serviceRegistry = map[string]ServiceSpec{}
|
||||
)
|
||||
|
||||
// RegisterService declares one service, replacing any existing spec of that
|
||||
// name. It panics on an invalid registration.
|
||||
func RegisterService(context, name, target, listen string) {
|
||||
switch context {
|
||||
case ServiceClient, ServiceServer, ServiceBoth:
|
||||
default:
|
||||
panic(fmt.Sprintf(
|
||||
"config.RegisterService(%q): unknown context %q, want %q, %q or %q",
|
||||
name, context, ServiceClient, ServiceServer, ServiceBoth,
|
||||
))
|
||||
}
|
||||
if context != ServiceClient && strings.TrimSpace(listen) == "" {
|
||||
panic(fmt.Sprintf(
|
||||
"config.RegisterService(%q): a %q service needs a default listen address; "+
|
||||
"an empty one reaches net.Listen as \"any free port\"",
|
||||
name, context,
|
||||
))
|
||||
}
|
||||
serviceRegistryMu.Lock()
|
||||
defer serviceRegistryMu.Unlock()
|
||||
serviceRegistry[name] = ServiceSpec{
|
||||
Context: context,
|
||||
Name: name,
|
||||
Target: target,
|
||||
Listen: listen,
|
||||
}
|
||||
// serviceRegistry holds the services this repository ships. What differs
|
||||
// between them is data here, not a branch elsewhere: the gateway's target is
|
||||
// defaulted while the State Store's is supplied by the operator.
|
||||
var serviceRegistry = map[string]ServiceSpec{
|
||||
ServiceNameGateway: {
|
||||
Context: ServiceBoth,
|
||||
Name: ServiceNameGateway,
|
||||
Target: "127.0.0.1:8585",
|
||||
Listen: "127.0.0.1:8585",
|
||||
},
|
||||
ServiceNameState: {
|
||||
Context: ServiceClient,
|
||||
Name: ServiceNameState,
|
||||
Target: "",
|
||||
Listen: "127.0.0.1:9090",
|
||||
},
|
||||
ServiceNameMessaging: {
|
||||
Context: ServiceBoth,
|
||||
Name: ServiceNameMessaging,
|
||||
Target: "127.0.0.1:8586",
|
||||
Listen: "127.0.0.1:8586",
|
||||
},
|
||||
ServiceNameDaemon: {
|
||||
Context: ServiceBoth,
|
||||
Name: ServiceNameDaemon,
|
||||
Target: "127.0.0.1:8587",
|
||||
Listen: "127.0.0.1:8587",
|
||||
},
|
||||
}
|
||||
|
||||
// LookupService returns the spec registered for name. The registry is the
|
||||
@@ -84,17 +78,13 @@ func RegisterService(context, name, target, listen string) {
|
||||
// report a typo'd [services.gatway] once Services is a map that would
|
||||
// otherwise decode any section at all.
|
||||
func LookupService(name string) (ServiceSpec, bool) {
|
||||
serviceRegistryMu.RLock()
|
||||
defer serviceRegistryMu.RUnlock()
|
||||
spec, ok := serviceRegistry[name]
|
||||
return spec, ok
|
||||
}
|
||||
|
||||
// RegisteredServices returns every spec, ordered by name so defaulting and
|
||||
// validation run in a stable order regardless of map iteration.
|
||||
// RegisteredServices returns a copy of every spec, ordered by name so
|
||||
// defaulting and validation run in a stable order regardless of map iteration.
|
||||
func RegisteredServices() []ServiceSpec {
|
||||
serviceRegistryMu.RLock()
|
||||
defer serviceRegistryMu.RUnlock()
|
||||
out := make([]ServiceSpec, 0, len(serviceRegistry))
|
||||
for _, spec := range serviceRegistry {
|
||||
out = append(out, spec)
|
||||
@@ -102,14 +92,3 @@ func RegisteredServices() []ServiceSpec {
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out
|
||||
}
|
||||
|
||||
func init() {
|
||||
// The services this repository ships. Everything that differs
|
||||
// between them is an argument here, not a branch somewhere else: the
|
||||
// gateway's target is defaulted while the State Store's is supplied by
|
||||
// the operator.
|
||||
RegisterService(ServiceBoth, ServiceNameGateway, "127.0.0.1:8585", "127.0.0.1:8585")
|
||||
RegisterService(ServiceClient, ServiceNameState, "", "127.0.0.1:9090")
|
||||
RegisterService(ServiceBoth, ServiceNameMessaging, "127.0.0.1:8586", "127.0.0.1:8586")
|
||||
RegisterService(ServiceBoth, ServiceNameDaemon, "127.0.0.1:8587", "127.0.0.1:8587")
|
||||
}
|
||||
Reference in new issue
Block a user