Documentation
¶
Overview ¶
Package driverbase provides a reusable lifecycle skeleton for southbound drivers (Modbus, OPC UA, S7). Concrete drivers embed BaseDriver and supply protocol-specific hooks (connect, closeConn, init) during Init; the base provides Start/Stop/Restart/Status/reconnectLoop/handleConnectionLost.
This eliminates ~176 lines of cross-driver lifecycle duplication (DUP-001) and the 3-line ParseReconnectSettings block repeated in each driver (DUP-004).
Index ¶
- type BaseDriver
- func (d *BaseDriver) AddReadCount(n uint64)
- func (d *BaseDriver) Config() core.DriverConfig
- func (d *BaseDriver) Context() context.Context
- func (d *BaseDriver) ErrorCount() uint64
- func (d *BaseDriver) GetState() core.ConnState
- func (d *BaseDriver) GetStateLocked() core.ConnState
- func (d *BaseDriver) HandleConnectionLost()
- func (d *BaseDriver) Lock()
- func (d *BaseDriver) Logger() core.Logger
- func (d *BaseDriver) MaxReconnectBackoff() time.Duration
- func (d *BaseDriver) Name() string
- func (d *BaseDriver) ParseReconnectSettings(settings map[string]any)
- func (d *BaseDriver) RLock()
- func (d *BaseDriver) RUnlock()
- func (d *BaseDriver) ReadCount() uint64
- func (d *BaseDriver) ReconnectBackoff() time.Duration
- func (d *BaseDriver) ReconnectCount() uint64
- func (d *BaseDriver) RecordError()
- func (d *BaseDriver) RecordRead()
- func (d *BaseDriver) Restart(ctx context.Context, config core.DriverConfig) error
- func (d *BaseDriver) SetCloseConnFunc(f func())
- func (d *BaseDriver) SetConnectFunc(f func() error)
- func (d *BaseDriver) SetExtraShutdown(f func())
- func (d *BaseDriver) SetInitFunc(f func(context.Context, core.DriverConfig) error)
- func (d *BaseDriver) SetLastError(err string)
- func (d *BaseDriver) SetLastErrorLocked(err string)
- func (d *BaseDriver) SetLastRead(t time.Time)
- func (d *BaseDriver) SetLogger(l core.Logger)
- func (d *BaseDriver) SetMeta(name, driverType string, config core.DriverConfig)
- func (d *BaseDriver) SetOnConnLost(f func())
- func (d *BaseDriver) SetState(s core.ConnState)
- func (d *BaseDriver) SetStateLocked(s core.ConnState)
- func (d *BaseDriver) SetTagCountFunc(f func() int)
- func (d *BaseDriver) Start(ctx context.Context) error
- func (d *BaseDriver) Status() core.DriverStatus
- func (d *BaseDriver) Stop() error
- func (d *BaseDriver) Type() string
- func (d *BaseDriver) Unlock()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseDriver ¶
type BaseDriver struct {
// contains filtered or unexported fields
}
BaseDriver holds the shared lifecycle state and hooks for a southbound driver. Concrete drivers embed this struct and set the hook fields during Init.
func (*BaseDriver) AddReadCount ¶
func (d *BaseDriver) AddReadCount(n uint64)
AddReadCount adds n to the read counter.
func (*BaseDriver) Config ¶
func (d *BaseDriver) Config() core.DriverConfig
Config returns the driver's config.
func (*BaseDriver) Context ¶
func (d *BaseDriver) Context() context.Context
Context returns the driver's context (set during Start).
func (*BaseDriver) ErrorCount ¶
func (d *BaseDriver) ErrorCount() uint64
ErrorCount returns the total error count.
func (*BaseDriver) GetState ¶
func (d *BaseDriver) GetState() core.ConnState
GetState returns the current connection state (thread-safe).
func (*BaseDriver) GetStateLocked ¶
func (d *BaseDriver) GetStateLocked() core.ConnState
GetStateLocked returns state without locking (caller must hold mu).
func (*BaseDriver) HandleConnectionLost ¶
func (d *BaseDriver) HandleConnectionLost()
HandleConnectionLost transitions to StateError, closes the connection, runs onConnLost (if set), and starts the reconnect loop. Exported so concrete drivers can call it from Read/Write error paths.
func (*BaseDriver) Logger ¶
func (d *BaseDriver) Logger() core.Logger
Logger returns the driver's logger.
func (*BaseDriver) MaxReconnectBackoff ¶
func (d *BaseDriver) MaxReconnectBackoff() time.Duration
MaxReconnectBackoff returns the maximum reconnect backoff interval.
func (*BaseDriver) ParseReconnectSettings ¶
func (d *BaseDriver) ParseReconnectSettings(settings map[string]any)
ParseReconnectSettings reads reconnect-interval, reconnect-max-interval, and max-reconnect-failures from the settings map with core defaults. Replaces the identical 3-line block inlined in each driver's Init (DUP-004).
func (*BaseDriver) ReadCount ¶
func (d *BaseDriver) ReadCount() uint64
ReadCount returns the total read count.
func (*BaseDriver) ReconnectBackoff ¶
func (d *BaseDriver) ReconnectBackoff() time.Duration
ReconnectBackoff returns the initial reconnect backoff interval.
func (*BaseDriver) ReconnectCount ¶
func (d *BaseDriver) ReconnectCount() uint64
ReconnectCount returns the current reconnect count.
func (*BaseDriver) RecordError ¶
func (d *BaseDriver) RecordError()
RecordError increments errorCount.
func (*BaseDriver) RecordRead ¶
func (d *BaseDriver) RecordRead()
RecordRead updates lastRead and increments readCount (caller holds mu or ensures no concurrent access to lastRead).
func (*BaseDriver) Restart ¶
func (d *BaseDriver) Restart(ctx context.Context, config core.DriverConfig) error
Restart stops the driver, re-initializes with the new config, and starts again.
func (*BaseDriver) SetCloseConnFunc ¶
func (d *BaseDriver) SetCloseConnFunc(f func())
SetCloseConnFunc sets the connection close hook (called with mu held).
func (*BaseDriver) SetConnectFunc ¶
func (d *BaseDriver) SetConnectFunc(f func() error)
SetConnectFunc sets the protocol-specific connect function.
func (*BaseDriver) SetExtraShutdown ¶
func (d *BaseDriver) SetExtraShutdown(f func())
SetExtraShutdown sets the optional pre-lock teardown hook.
func (*BaseDriver) SetInitFunc ¶
func (d *BaseDriver) SetInitFunc(f func(context.Context, core.DriverConfig) error)
SetInitFunc sets the init function used by Restart.
func (*BaseDriver) SetLastError ¶
func (d *BaseDriver) SetLastError(err string)
SetLastError records the last error message (thread-safe).
func (*BaseDriver) SetLastErrorLocked ¶
func (d *BaseDriver) SetLastErrorLocked(err string)
SetLastErrorLocked sets lastError without locking (caller must hold mu).
func (*BaseDriver) SetLastRead ¶
func (d *BaseDriver) SetLastRead(t time.Time)
SetLastRead sets the last read timestamp (caller holds mu).
func (*BaseDriver) SetLogger ¶
func (d *BaseDriver) SetLogger(l core.Logger)
SetLogger injects a structured logger. Called by the engine before Init.
func (*BaseDriver) SetMeta ¶
func (d *BaseDriver) SetMeta(name, driverType string, config core.DriverConfig)
SetMeta sets the driver name, type, and config. Called during Init. If no logger has been injected via SetLogger, defaults to NoopLogger.
func (*BaseDriver) SetOnConnLost ¶
func (d *BaseDriver) SetOnConnLost(f func())
SetOnConnLost sets the optional post-unlock cleanup hook.
func (*BaseDriver) SetState ¶
func (d *BaseDriver) SetState(s core.ConnState)
SetState updates the connection state (thread-safe).
func (*BaseDriver) SetStateLocked ¶
func (d *BaseDriver) SetStateLocked(s core.ConnState)
SetStateLocked sets state without locking (caller must hold mu).
func (*BaseDriver) SetTagCountFunc ¶
func (d *BaseDriver) SetTagCountFunc(f func() int)
SetTagCountFunc sets the tag count function for Status().
func (*BaseDriver) Start ¶
func (d *BaseDriver) Start(ctx context.Context) error
Start attempts the initial connection. On failure, transitions to StateConnecting and starts the reconnect loop in the background.
func (*BaseDriver) Status ¶
func (d *BaseDriver) Status() core.DriverStatus
Status returns the current driver status snapshot.
func (*BaseDriver) Stop ¶
func (d *BaseDriver) Stop() error
Stop cancels the context, waits for the reconnect goroutine, runs extraShutdown (if set), then closes the protocol connection.