driverbase

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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

func (d *BaseDriver) Lock()

Lock acquires the write lock.

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

func (d *BaseDriver) Name() string

Name returns the driver instance name.

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

func (d *BaseDriver) RLock()

RLock acquires the read lock.

func (*BaseDriver) RUnlock

func (d *BaseDriver) RUnlock()

RUnlock releases the read lock.

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.

func (*BaseDriver) Type

func (d *BaseDriver) Type() string

Type returns the driver type string.

func (*BaseDriver) Unlock

func (d *BaseDriver) Unlock()

Unlock releases the write lock.

Jump to

Keyboard shortcuts

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