Documentation
¶
Overview ¶
Package dispatcher implements the version-agnostic OCPP connection lifecycle, pending-call tracking, and concurrency control. See spec §3.
Index ¶
- func CheckDirection(role Role, op Op, dir ocppj.Direction) error
- func DoCall(ctx context.Context, c *Conn, action string, reqPayload []byte) (_ []byte, err error)
- type Config
- type Conn
- func (c *Conn) Close(reason error) error
- func (c *Conn) Context() context.Context
- func (c *Conn) ID() string
- func (c *Conn) RemoteAddr() string
- func (c *Conn) RequestHeader() http.Header
- func (c *Conn) Start(parent context.Context)
- func (c *Conn) StartKeepalive(interval time.Duration, fn func(context.Context))
- func (c *Conn) Subprotocol() string
- func (c *Conn) TLS() *tls.ConnectionState
- func (c *Conn) Version() ocppj.Version
- type ConnMetadata
- type HandlerFunc
- type HandlerRegistry
- type MetricsHook
- type Op
- type Role
- type SchemaMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDirection ¶
CheckDirection validates that a role may perform op on a message with the given direction. CSMS handles SentByCP and calls SentByCSMS; CP is the mirror.
Types ¶
type Config ¶
type Config struct {
CallTimeout time.Duration
WriteTimeout time.Duration
PingInterval time.Duration
PongWait time.Duration
SerializeOutboundCalls bool
OutboundQueueSize int
MaxConcurrentHandlers int64
// GlobalHandlerLimiter, when non-nil, bounds the total number of inbound
// handlers running concurrently across all connections that share it. It is
// acquired in addition to the per-connection MaxConcurrentHandlers budget.
// nil disables the global cap. The same *semaphore.Weighted must be shared
// by every connection for the limit to be server-wide.
GlobalHandlerLimiter *semaphore.Weighted
Logger *slog.Logger
Metrics MetricsHook
Tracer observability.Tracer
// SchemaValidate optionally validates a payload for the given version.
// nil disables validation. SchemaMode controls how returned errors are handled.
SchemaValidate func(version ocppj.Version, action, kind string, payload []byte) error
SchemaMode SchemaMode
}
Config controls a single connection's behavior.
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is one OCPP connection. It owns reader, writer, and dispatcher goroutines whose lifetime is bound to ctx. All teardown flows from cancel(cause).
func NewConn ¶
func NewConn(id string, ws transport.WS, cfg Config, reg *HandlerRegistry, meta ...ConnMetadata) *Conn
NewConn creates a connection. Call Start to launch its goroutines.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the peer network address from the HTTP upgrade request, if available.
func (*Conn) RequestHeader ¶
RequestHeader returns a copy of the HTTP upgrade request headers.
func (*Conn) StartKeepalive ¶
StartKeepalive must be called immediately after Start, before any Close, so it joins the connection WaitGroup while other goroutines are still live. StartKeepalive runs fn every interval until the connection context is done. interval <= 0 disables it. Used by csms/cp to send OCPP Heartbeat messages.
func (*Conn) Subprotocol ¶
Subprotocol returns the negotiated WebSocket subprotocol.
func (*Conn) TLS ¶
func (c *Conn) TLS() *tls.ConnectionState
TLS returns a copy of the HTTP upgrade TLS connection state, if available.
type ConnMetadata ¶
type ConnMetadata struct {
RemoteAddr string
RequestHeader http.Header
TLS *tls.ConnectionState
}
ConnMetadata contains HTTP upgrade metadata associated with a connection. Fields are empty for connections that were not created from an inbound HTTP upgrade request.
type HandlerFunc ¶
type HandlerRegistry ¶
type HandlerRegistry struct {
// contains filtered or unexported fields
}
func NewHandlerRegistry ¶
func NewHandlerRegistry() *HandlerRegistry
func (*HandlerRegistry) Lookup ¶
func (r *HandlerRegistry) Lookup(action string) (HandlerFunc, bool)
func (*HandlerRegistry) Register ¶
func (r *HandlerRegistry) Register(action string, h HandlerFunc)
type MetricsHook ¶
type MetricsHook interface {
ConnectionOpened()
ConnectionClosed()
CallStarted(action, direction string)
CallCompleted(action, direction string, dur time.Duration, status string)
PendingDelta(delta int)
}
MetricsHook receives connection/call lifecycle events. The default is a no-op; Phase 4 wires Prometheus/OTel implementations.
var NoopMetrics MetricsHook = noopMetrics{}
NoopMetrics is the default metrics hook.
func MetricsHookFrom ¶
func MetricsHookFrom(m observability.Metrics, version string) MetricsHook
MetricsHookFrom adapts an observability.Metrics to a MetricsHook bound to a specific OCPP version.
type SchemaMode ¶
type SchemaMode int
SchemaMode controls how inbound JSON Schema validation failures are handled.
const ( SchemaModeOff SchemaMode = iota SchemaModeTolerant SchemaModeStrict )