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)
- func DoCallAsync(ctx context.Context, c *Conn, action string, payload []byte, cb AsyncCallback) error
- type AsyncCallback
- 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.
func DoCall ¶
DoCall sends an OCPP Call and waits for the matching CallResult or CallError. It returns the raw response payload; typed encoding/decoding happens in the csms/cp generic wrappers.
func DoCallAsync ¶ added in v0.1.3
func DoCallAsync(ctx context.Context, c *Conn, action string, payload []byte, cb AsyncCallback) error
DoCallAsync sends an OCPP Call without blocking and delivers the result to cb.
When SerializeOutboundCalls is set, calls are appended to a per-connection FIFO queue and sent one at a time by a single worker goroutine (at most one outstanding request); cb fires in submission order. Otherwise each call runs in its own goroutine and cb fires as responses arrive.
It returns an error synchronously only if the call could not be accepted: a nil callback, a connection that is closed/not started, or (serialized mode) a full queue (ocppj.ErrQueueFull). Per-call failures are delivered to cb, not returned.
Types ¶
type AsyncCallback ¶ added in v0.1.3
AsyncCallback receives the raw response payload, or an error if the call failed (timeout, CallError, connection closed, ...). Exactly one of the two is meaningful: on error, payload is nil.
type Config ¶
type Config struct {
CallTimeout time.Duration
WriteTimeout time.Duration
PingInterval time.Duration
PongWait time.Duration
SerializeOutboundCalls bool
OutboundQueueSize int
// AsyncQueueSize bounds the per-connection FIFO queue used by DoCallAsync when
// SerializeOutboundCalls is set; enqueuing beyond it returns ocppj.ErrQueueFull.
AsyncQueueSize 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
// SchemaValidateLenient validates a payload in lenient mode. It returns the
// possibly enum-normalized payload to dispatch, the soft-violation keywords
// seen, and a non-nil error only for hard violations. Used only when
// SchemaMode == SchemaModeLenient. Returns primitives so the dispatcher stays
// decoupled from core/schema.
SchemaValidateLenient func(version ocppj.Version, action, kind string, payload []byte) (out []byte, soft []string, err 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 SchemaModeLenient )