driver

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: GPL-2.0, GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// MaxGlobalReconnectRate caps reconnect attempts across all ConnectionManagers
	// in a sliding 1-second window (token bucket style).
	MaxGlobalReconnectRate = 10
)

Global reconnect rate limiter — sole Owner for MaxGlobalReconnectRate. core.ConnectionController must NOT maintain a parallel counter (ScanEngine §5.3 / v5.2).

Functions

func RegisterDriver

func RegisterDriver(name string, factory Factory)

RegisterDriver registers a driver factory for a given protocol name

Types

type BACnetAddressNotifier

type BACnetAddressNotifier interface {
	OnBACnetAddressDiscovered(deviceKey, ip string, port int)
}

BACnetAddressNotifier receives runtime BACnet address updates (e.g. UDP port change after reboot).

type BACnetAddressNotifySetter

type BACnetAddressNotifySetter interface {
	SetBACnetAddressNotifier(BACnetAddressNotifier)
}

BACnetAddressNotifySetter allows wiring a notifier into the BACnet driver.

type ConnState

type ConnState int
const (
	StateDisconnected ConnState = iota
	StateConnecting
	StateConnected
	StateRetrying
	StateDead
)

func (ConnState) String

func (s ConnState) String() string

type ConnectFunc

type ConnectFunc func(ctx context.Context) error

ConnectFunc performs a single connection attempt (dial/open). Retry/backoff is owned exclusively by ConnectionManager.EnsureConnected.

type ConnectionManager

type ConnectionManager struct {
	// contains filtered or unexported fields
}

func NewConnectionManager

func NewConnectionManager(driverName string) *ConnectionManager

func (*ConnectionManager) AttemptHalfOpen

func (cm *ConnectionManager) AttemptHalfOpen(success bool)

func (*ConnectionManager) CanRetry

func (cm *ConnectionManager) CanRetry() (canRetry bool, waitTime time.Duration)

func (*ConnectionManager) Close

func (cm *ConnectionManager) Close()

func (*ConnectionManager) EnsureConnected

func (cm *ConnectionManager) EnsureConnected(ctx context.Context, connect ConnectFunc) error

EnsureConnected is the single entry point for synchronous reconnect with backoff, cooldown, and global rate limiting.

func (*ConnectionManager) GetState

func (cm *ConnectionManager) GetState() ConnState

func (*ConnectionManager) GetStatus

func (cm *ConnectionManager) GetStatus() (state ConnState, retryCount int, maxRetries int, coolDownRemaining time.Duration, lastSuccess time.Time)

func (*ConnectionManager) RecordFailure

func (cm *ConnectionManager) RecordFailure() (shouldRetry bool, backoff time.Duration)

func (*ConnectionManager) RecordSuccess

func (cm *ConnectionManager) RecordSuccess()

func (*ConnectionManager) ResetDaily

func (cm *ConnectionManager) ResetDaily()

func (*ConnectionManager) ScheduleAsyncTask

func (cm *ConnectionManager) ScheduleAsyncTask(parentCtx context.Context, timeout time.Duration, task func(context.Context) error)

ScheduleAsyncTask runs a one-shot async operation under the same single-flight guard as ScheduleReconnect, without requiring a disconnected connection state. Use for device-level recovery probes while the channel remains connected.

func (*ConnectionManager) ScheduleReconnect

func (cm *ConnectionManager) ScheduleReconnect(parentCtx context.Context, timeout time.Duration, connect ConnectFunc)

ScheduleReconnect starts an asynchronous reconnect guarded by single-flight. Duplicate calls while a reconnect is in progress are ignored.

func (*ConnectionManager) SetBackoffParams

func (cm *ConnectionManager) SetBackoffParams(base, max time.Duration, factor float64)

func (*ConnectionManager) SetLinkMutex

func (cm *ConnectionManager) SetLinkMutex(mu *sync.Mutex)

SetLinkMutex binds the shared-link channelMu so dial/install is serialized with I/O.

func (*ConnectionManager) SetMaxFailCount

func (cm *ConnectionManager) SetMaxFailCount(max int)

func (*ConnectionManager) SetMaxRetries

func (cm *ConnectionManager) SetMaxRetries(max int)

func (*ConnectionManager) SetState

func (cm *ConnectionManager) SetState(state ConnState)

func (*ConnectionManager) StartBackgroundLoop

func (cm *ConnectionManager) StartBackgroundLoop(fn func(context.Context))

StartBackgroundLoop runs fn in a single managed goroutine until StopBackgroundLoop is called or the parent context passed to fn is cancelled via StopBackgroundLoop. Only one background loop may run per ConnectionManager.

func (*ConnectionManager) StopBackgroundLoop

func (cm *ConnectionManager) StopBackgroundLoop()

StopBackgroundLoop cancels and waits for the managed background goroutine to exit.

type DeviceCollectionResetter

type DeviceCollectionResetter interface {
	ResetDeviceCollection(deviceID string)
}

DeviceCollectionResetter is optional. Drivers that cache per-device collection state (e.g. OPC UA subscriptions) should implement it so ScanEngine can drop stale state when points are added, removed, or the device is re-registered.

type Driver

type Driver interface {
	Init(cfg model.DriverConfig) error
	Connect(ctx context.Context) error
	Disconnect() error
	ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
	WritePoint(ctx context.Context, point model.Point, value any) error
	Health() HealthStatus
	// SetSlaveID sets the slave/unit ID for protocols that support multiple slaves (optional)
	SetSlaveID(slaveID uint8) error
	// SetDeviceConfig sets device specific configuration (optional, for protocols needing per-device connection info like BACnet IP)
	SetDeviceConfig(config map[string]any) error

	// GetConnectionMetrics returns transport-level connection metrics
	GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)
}

Driver is the unified interface for all protocol drivers

func GetDriver

func GetDriver(name string) (Driver, bool)

GetDriver creates a new driver instance for the given protocol

type Factory

type Factory func() Driver

Factory function type for creating drivers

type HealthStatus

type HealthStatus int

HealthStatus represents the health of the driver connection

const (
	HealthStatusUnknown HealthStatus = iota
	HealthStatusGood
	HealthStatusBad
)

type LinkMutexBinder

type LinkMutexBinder interface {
	BindLinkMutex(mu *sync.Mutex)
}

LinkMutexBinder lets ChannelManager inject the per-channel channelMu into ConnectionManager so shared-link reconnect dial/install cannot race I/O.

type ObjectScanner

type ObjectScanner interface {
	ScanObjects(ctx context.Context, config map[string]any) (any, error)
}

ObjectScanner is an optional interface for drivers that support object/point discovery on a device

type ReconnectScheduler

type ReconnectScheduler interface {
	ScheduleReconnect(ctx context.Context, timeout time.Duration)
}

ReconnectScheduler is optional. Drivers that support async reconnection (e.g. Modbus, BACnet) should implement it so the channel manager can schedule background reconnects without blocking startup.

type Scanner

type Scanner interface {
	Scan(ctx context.Context, params map[string]any) (any, error)
}

Scanner is an optional interface for drivers that support discovery

Directories

Path Synopsis
Package ethercat implements EtherCAT master driver for edgeCore gateway.
Package ethercat implements EtherCAT master driver for edgeCore gateway.

Jump to

Keyboard shortcuts

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