session

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package session provides safe-mode style change journals with rollback.

Index

Constants

View Source
const (
	StatusActive     = "active"
	StatusCommitted  = "committed"
	StatusRolledBack = "rolled_back"
)

Variables

This section is empty.

Functions

func BuildInverse

func BuildInverse(command string, args []string, createdID string) []string

BuildInverse constructs a simple inverse for common RouterOS mutations. Returns command + args for the inverse, or empty if unknown.

func BuildRemoveInverse added in v0.3.0

func BuildRemoveInverse(removeCommand string, preState map[string]string) []string

BuildRemoveInverse recreates a removed row via /add using printable writable fields.

func BuildSetInverse added in v0.3.0

func BuildSetInverse(setCommand, id string, preState map[string]string, changedArgs []string) []string

BuildSetInverse restores previously changed properties via /set. changedArgs are the =key=value pairs from the original set (excluding .id). When id is empty (singleton menus like /ip/cloud), the inverse omits =.id=.

func ChangedSetKeys added in v0.3.0

func ChangedSetKeys(args []string) []string

ChangedSetKeys extracts property keys from set args (excluding .id).

func DefaultDir

func DefaultDir() string

DefaultDir returns ~/.config/ros/sessions.

func IsReadOnlyField added in v0.3.0

func IsReadOnlyField(key string) bool

IsReadOnlyField reports whether a printed field should be omitted from restore args.

func Watch added in v0.3.0

func Watch(ctx context.Context, store *Store, sess *Session, cfg WatchConfig) error

Watch runs until ctx is cancelled. After FailThreshold consecutive probe failures it marks auto_rollback_pending and keeps retrying Probe+Rollback until rollback succeeds (link restored) or the context ends.

Types

type Change

type Change struct {
	ID        string            `json:"id"`
	Command   string            `json:"command"`
	Args      []string          `json:"args"`
	Inverse   []string          `json:"inverse"` // first element is command, rest are args
	PreState  map[string]string `json:"pre_state,omitempty"`
	AppliedAt time.Time         `json:"applied_at"`
	Note      string            `json:"note,omitempty"`
}

Change records a single mutating operation and how to undo it.

type ProbeFunc added in v0.3.0

type ProbeFunc func(ctx context.Context) error

ProbeFunc dials/probes the device. Returns nil when healthy.

type RollbackFunc added in v0.3.0

type RollbackFunc func(ctx context.Context, sess *Session) error

RollbackFunc applies journal inverses for the session.

type Session

type Session struct {
	ID                  string    `json:"id"`
	Device              string    `json:"device"`
	Safe                bool      `json:"safe"`
	Status              string    `json:"status"`
	AutoRollbackPending bool      `json:"auto_rollback_pending,omitempty"`
	StartedAt           time.Time `json:"started_at"`
	UpdatedAt           time.Time `json:"updated_at"`
	Changes             []Change  `json:"changes"`
}

Session is a journal of pending changes for a device.

type Store

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

Store persists sessions to disk under a base directory.

func NewStore

func NewStore(dir string) (*Store, error)

NewStore creates a session store rooted at dir (created if needed).

func (*Store) Active

func (s *Store) Active(device string) (*Session, error)

Active returns the active session for a device, if any.

func (*Store) AppendChange

func (s *Store) AppendChange(sess *Session, change Change) error

AppendChange records a change on an active session.

func (*Store) Begin

func (s *Store) Begin(device string, safe bool) (*Session, error)

Begin starts a new active session for a device. Only one active session per device.

func (*Store) ClearAutoRollbackPending added in v0.3.0

func (s *Store) ClearAutoRollbackPending(sess *Session) error

ClearAutoRollbackPending clears the pending flag after a successful or abandoned rollback.

func (*Store) Commit

func (s *Store) Commit(sess *Session) error

Commit marks the session as committed and clears the active lock.

func (*Store) Get

func (s *Store) Get(id string) (*Session, error)

Get loads a session by ID.

func (*Store) MarkAutoRollbackPending added in v0.3.0

func (s *Store) MarkAutoRollbackPending(sess *Session) error

MarkAutoRollbackPending flags the session so the next connect can finish rollback.

func (*Store) MarkRolledBack

func (s *Store) MarkRolledBack(sess *Session) error

MarkRolledBack marks the session rolled back and clears the active lock.

type WatchConfig added in v0.3.0

type WatchConfig struct {
	Interval      time.Duration
	FailThreshold int
	Probe         ProbeFunc
	Rollback      RollbackFunc
	OnLinkLost    func(sess *Session)
	OnRolledBack  func(sess *Session, err error)
}

WatchConfig controls heartbeat behavior.

Jump to

Keyboard shortcuts

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