dispatcher

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package dispatcher implements the version-agnostic OCPP connection lifecycle, pending-call tracking, and concurrency control. See spec §3.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDirection

func CheckDirection(role Role, op Op, dir ocppj.Direction) error

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

func DoCall(ctx context.Context, c *Conn, action string, reqPayload []byte) (_ []byte, err error)

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.

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.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns production-sane defaults.

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) Close

func (c *Conn) Close(reason error) error

Close tears down the connection. Safe to call multiple times.

func (*Conn) Context

func (c *Conn) Context() context.Context

Context returns the connection lifecycle context.

func (*Conn) ID

func (c *Conn) ID() string

ID returns the charge point identifier.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() string

RemoteAddr returns the peer network address from the HTTP upgrade request, if available.

func (*Conn) RequestHeader

func (c *Conn) RequestHeader() http.Header

RequestHeader returns a copy of the HTTP upgrade request headers.

func (*Conn) Start

func (c *Conn) Start(parent context.Context)

Start launches the connection goroutines bound to parent.

func (*Conn) StartKeepalive

func (c *Conn) StartKeepalive(interval time.Duration, fn func(context.Context))

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

func (c *Conn) Subprotocol() string

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.

func (*Conn) Version

func (c *Conn) Version() ocppj.Version

Version returns the negotiated OCPP version.

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 HandlerFunc func(ctx context.Context, c *Conn, payload []byte) ([]byte, error)

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 Op

type Op int

Op is the operation being registered/performed.

const (
	OpHandle Op = iota + 1 // registering an inbound handler
	OpCall                 // sending an outbound Call
)

type Role

type Role int

Role is the local peer's role.

const (
	RoleCSMS Role = iota + 1
	RoleCP
)

type SchemaMode

type SchemaMode int

SchemaMode controls how inbound JSON Schema validation failures are handled.

const (
	SchemaModeOff SchemaMode = iota
	SchemaModeTolerant
	SchemaModeStrict
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL