Documentation
¶
Overview ¶
Package svcmode pkg/svcmode/svcmode.go c3-vis-core services (transport-discovery, address-resolver, route-finder, service-discovery, config-bootstrapper) that need to listen on HTTP, dmsghttp, or both simultaneously. It centralizes:
Note on dmsg-discovery specifically: dmsg-discovery's HTTP listener is load-bearing — dmsg-servers themselves use disc.NewHTTP(...) (plain HTTP client) to register with it (see cmd/dmsg-commands/ dmsg-server/commands/start/root.go). If dmsg-discovery were to run in ModeDmsg only, dmsg-servers would have no way to register because they ARE the dmsg transport layer. dmsg-discovery must run in ModeHTTP or ModeDual; ModeDmsg is not meaningful for it. This package does not enforce that constraint — dmsg-discovery's command wrapper is responsible for rejecting ModeDmsg explicitly.
- Mode selection (http | dmsg | dual) via the --mode flag, with a sensible default derived from whether a DMSG secret key was provided.
- DMSG bootstrap via the existing cmdutil.BootstrapDmsg helper (which uses the embedded services-config.json dmsg_servers list so services can come up against private deployments without a pre-populated dmsg-discovery).
- HTTP listener (tcpproxy.ListenAndServe).
- dmsghttp listener (dmsghttp.ListenAndServe) and survey whitelist debug handler (dmsghttp.WithDebug) when dmsg is enabled, folded onto the service's dmsg :80 handler rather than a second port.
- Consistent shutdown: a single Handle.Close() that tears down both listeners and the bootstrap dmsg client.
Services keep ownership of their API handler, their CLI flag parsing, and any service-specific extras (UDP listeners, private admin HTTP, CXO publishers, etc.) — svcmode only handles the HTTP + dmsghttp pair that every service has in common.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Mode is the effective listener mode. Use ResolveMode to build
// it from flag/env/SK.
Mode Mode
// Stats, when set, counts the service's HTTP requests and its dmsg
// traffic for its status page (charts.ServiceStats).
Stats Stats
// HTTPAddr is the bind address for the http listener
// (e.g. ":9092"). Required when Mode includes http.
HTTPAddr string
// Handler is the http.Handler used for BOTH the http and
// dmsghttp listeners. Services typically pass their API object
// directly since it satisfies http.Handler.
Handler http.Handler
// DMSG identity. Required when Mode includes dmsg. If SK is
// null and Mode includes dmsg, Start returns an error.
PK cipher.PubKey
SK cipher.SecKey
// DmsgPort is the dmsghttp listener port. Defaults to
// dmsg.DefaultDmsgHTTPPort (80) if zero.
DmsgPort uint16
// DmsgDiscovery is the plain-HTTP URL of the dmsg-discovery
// service, used as a fallback for the embedded-server bootstrap
// and for periodic server-list refresh. Required when Mode
// includes dmsg unless EmbeddedDmsgServers or DmsgDiscoveryDmsg
// is non-empty. Ignored when DmsgDiscoveryDmsg is set —
// deployment services that want strict dmsg-only operation
// should leave this empty.
DmsgDiscovery string
// DmsgDiscoveryDmsg is the dmsg-PK URL of the dmsg-discovery
// service (form `dmsg://<PK>:<port>`). When set, svcmode routes
// all discovery RPC over dmsg-HTTP via the bootstrap dmsg client
// — no plain-HTTP egress to dmsg-discovery occurs. Mutually
// preferred over DmsgDiscovery; the HTTP field is ignored.
DmsgDiscoveryDmsg string
// DmsgServerType, if non-empty, restricts the dmsg client to
// servers whose ServerType matches (e.g. "official").
DmsgServerType string
// EmbeddedDmsgServers is the list of dmsg server entries to use
// for initial bootstrap. Services should typically pass
// dmsg.Prod.DmsgServers from the embedded services-config.json.
EmbeddedDmsgServers []disc.Entry
// SurveyWhitelist is the list of public keys permitted to
// access the dmsghttp debug handler (pprof, etc.). Typically
// deployment.Prod.SurveyWhitelist.
SurveyWhitelist []cipher.PubKey
// Log is the shared logger for svcmode bookkeeping and
// downstream helpers.
Log *logging.Logger
// LogHookLevel is the most verbose level kept for /debug/log, info when empty.
LogHookLevel string
// OnDmsgServersUpdated, if non-nil, is called periodically with
// the current list of connected dmsg server PKs. Services that
// publish this list via their API (e.g. in /health) should set
// this so svcmode's background poll can keep it fresh.
OnDmsgServersUpdated func([]string)
// DmsgServerPollInterval is the interval at which
// OnDmsgServersUpdated is called. Defaults to 1 second if zero
// (matching the existing per-service polling loops).
DmsgServerPollInterval time.Duration
// DisableDHT is retained as a no-op field for backward compatibility
// with callers that previously passed it. The DHT subsystem has
// been removed from the tree; new code should drop the field
// entirely.
//
// Deprecated: no-op.
DisableDHT bool
}
Config configures a svcmode.Handle. All fields are optional unless required by the chosen Mode; Start returns a descriptive error if a required field is missing.
type Handle ¶
type Handle struct {
// DmsgClient is the bootstrap dmsg client or nil when Mode is
// ModeHTTP. Services that need raw dmsg access (e.g. to spawn
// a CXO publisher or TPS listener) can use it directly.
DmsgClient *dmsg.Client
// Mode is the resolved listener mode.
Mode Mode
// contains filtered or unexported fields
}
Handle owns the listeners and the bootstrap dmsg client. Callers should defer Handle.Close() immediately after Start returns.
func Start ¶
Start wires up the listeners described by cfg and returns a Handle. The caller is responsible for blocking on ctx.Done() (or on Handle.Errors()) in its main loop, and for calling Handle.Close() on shutdown.
dmsg bootstrap is performed whenever cfg.SK is non-null, even in ModeHTTP — a service's "mode" controls its inbound listeners only, not whether it has a dmsg.Client. Services with an SK always get a bootstrapped client they can reuse for outbound purposes (Handle.DmsgClient) regardless of mode.
Start returns an error on any validation failure. It does NOT return an error if an async listener (http or dmsghttp) fails to serve — those errors are surfaced via Handle.Errors() instead.
func (*Handle) Close ¶
func (h *Handle) Close()
Close stops the dmsg bootstrap and its listeners. Safe to call multiple times. The http listener stops when the context passed to Start is canceled — svcmode does not own a separate http.Server instance because tcpproxy.ListenAndServe is not cancellable on its own; callers rely on process exit.
type Mode ¶
type Mode string
Mode selects which inbound listeners a service exposes. It does NOT control whether the service has a dmsg client at all — that is determined purely by whether a secret key is configured. A service running in ModeHTTP with an SK still bootstraps a dmsg.Client (useful for outbound-only dmsg consumers such as TPD's CXO publisher); it simply does not spawn a dmsghttp.ListenAndServe.
const ( // ModeHTTP exposes only the bound HTTP address for inbound // client requests. If a secret key is present the dmsg client // is still bootstrapped — which means the service connects to // dmsg-servers via HTTP dmsg-discovery lookup — but no // dmsghttp listener is spawned. Useful for public deployments // that only want inbound HTTP but still need a dmsg.Client for // outbound purposes or for services that historically ran this // way by setting --sk but never had a dmsghttp in their chain. ModeHTTP Mode = "http" // ModeDmsg listens only on dmsghttp (dmsg://<pk>:<port>) for // inbound requests. No plaintext HTTP listener is exposed. // Useful for corporate / private network deployments that must // not serve plaintext HTTP. Requires SK. ModeDmsg Mode = "dmsg" // ModeDual listens on both http and dmsghttp. dmsg-discovery // defaults to it, and test networks select it to reach services // over plain HTTP. ModeDual Mode = "dual" )
func ParseMode ¶
ParseMode normalizes a mode string to one of the known Mode values. Empty / unrecognized input returns an error. Use ResolveMode to apply the "default based on SK presence" fallback.
func ResolveMode ¶
ResolveMode resolves the effective mode: the passed-in value (usually from --mode or the service config), else ModeDmsg if SK is non-null, else ModeHTTP. Deployment services are reached over dmsg and skynet, so a service with a key binds no plain HTTP listener unless asked to. Returns an error only if an explicit value is unrecognized.
func (Mode) IncludesDmsg ¶
IncludesDmsg reports whether the mode spawns a dmsghttp listener.
func (Mode) IncludesHTTP ¶
IncludesHTTP reports whether the mode spawns an http listener.