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
|
// 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
|
type Services map[string]ServiceConnection
|
||||||
|
|
||||||
// Get returns the connection configured for name, or the zero value when the
|
// Get returns the connection configured for name, or the zero value when the
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Service contexts. The context decides which addresses a service must end up
|
// Service contexts. The context decides which addresses a service must end up
|
||||||
@@ -36,7 +33,7 @@ type ServiceSpec struct {
|
|||||||
Listen string
|
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
|
// spelling a consumer looks up and the spelling that was registered are the
|
||||||
// same token, not two string literals that a typo can separate.
|
// same token, not two string literals that a typo can separate.
|
||||||
const (
|
const (
|
||||||
@@ -46,37 +43,34 @@ const (
|
|||||||
ServiceNameDaemon = "daemon"
|
ServiceNameDaemon = "daemon"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
// serviceRegistry holds the services this repository ships. What differs
|
||||||
serviceRegistryMu sync.RWMutex
|
// between them is data here, not a branch elsewhere: the gateway's target is
|
||||||
serviceRegistry = map[string]ServiceSpec{}
|
// defaulted while the State Store's is supplied by the operator.
|
||||||
)
|
var serviceRegistry = map[string]ServiceSpec{
|
||||||
|
ServiceNameGateway: {
|
||||||
// RegisterService declares one service, replacing any existing spec of that
|
Context: ServiceBoth,
|
||||||
// name. It panics on an invalid registration.
|
Name: ServiceNameGateway,
|
||||||
func RegisterService(context, name, target, listen string) {
|
Target: "127.0.0.1:8585",
|
||||||
switch context {
|
Listen: "127.0.0.1:8585",
|
||||||
case ServiceClient, ServiceServer, ServiceBoth:
|
},
|
||||||
default:
|
ServiceNameState: {
|
||||||
panic(fmt.Sprintf(
|
Context: ServiceClient,
|
||||||
"config.RegisterService(%q): unknown context %q, want %q, %q or %q",
|
Name: ServiceNameState,
|
||||||
name, context, ServiceClient, ServiceServer, ServiceBoth,
|
Target: "",
|
||||||
))
|
Listen: "127.0.0.1:9090",
|
||||||
}
|
},
|
||||||
if context != ServiceClient && strings.TrimSpace(listen) == "" {
|
ServiceNameMessaging: {
|
||||||
panic(fmt.Sprintf(
|
Context: ServiceBoth,
|
||||||
"config.RegisterService(%q): a %q service needs a default listen address; "+
|
Name: ServiceNameMessaging,
|
||||||
"an empty one reaches net.Listen as \"any free port\"",
|
Target: "127.0.0.1:8586",
|
||||||
name, context,
|
Listen: "127.0.0.1:8586",
|
||||||
))
|
},
|
||||||
}
|
ServiceNameDaemon: {
|
||||||
serviceRegistryMu.Lock()
|
Context: ServiceBoth,
|
||||||
defer serviceRegistryMu.Unlock()
|
Name: ServiceNameDaemon,
|
||||||
serviceRegistry[name] = ServiceSpec{
|
Target: "127.0.0.1:8587",
|
||||||
Context: context,
|
Listen: "127.0.0.1:8587",
|
||||||
Name: name,
|
},
|
||||||
Target: target,
|
|
||||||
Listen: listen,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LookupService returns the spec registered for name. The registry is the
|
// 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
|
// report a typo'd [services.gatway] once Services is a map that would
|
||||||
// otherwise decode any section at all.
|
// otherwise decode any section at all.
|
||||||
func LookupService(name string) (ServiceSpec, bool) {
|
func LookupService(name string) (ServiceSpec, bool) {
|
||||||
serviceRegistryMu.RLock()
|
|
||||||
defer serviceRegistryMu.RUnlock()
|
|
||||||
spec, ok := serviceRegistry[name]
|
spec, ok := serviceRegistry[name]
|
||||||
return spec, ok
|
return spec, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisteredServices returns every spec, ordered by name so defaulting and
|
// RegisteredServices returns a copy of every spec, ordered by name so
|
||||||
// validation run in a stable order regardless of map iteration.
|
// defaulting and validation run in a stable order regardless of map iteration.
|
||||||
func RegisteredServices() []ServiceSpec {
|
func RegisteredServices() []ServiceSpec {
|
||||||
serviceRegistryMu.RLock()
|
|
||||||
defer serviceRegistryMu.RUnlock()
|
|
||||||
out := make([]ServiceSpec, 0, len(serviceRegistry))
|
out := make([]ServiceSpec, 0, len(serviceRegistry))
|
||||||
for _, spec := range serviceRegistry {
|
for _, spec := range serviceRegistry {
|
||||||
out = append(out, spec)
|
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 })
|
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||||
return out
|
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