driver

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 18 Imported by: 1 module, 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 driver supplies deterministic transcript replay and the terminal driver around the ordinary Session Host, and a Pool that pumps many Groups in parallel. It is separate from the Core.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("driver: pool closed")

ErrClosed is returned for work offered to a closed Pool or a removed Member.

Functions

func ParseTranscript

func ParseTranscript(source string) ([]session.Item, error)

func PromptAnswer

func PromptAnswer(p session.Prompt, line string) (talk.Value, bool)

PromptAnswer is the answer a typed line gives a user prompt, or false when the line answers nothing and the question is asked again.

func PromptLines

func PromptLines(p session.Prompt) []string

PromptLines are the lines the REPL shows for a user prompt; the Script Runner shows the same.

func ReplayTranscript

func ReplayTranscript(recorded []session.Item, trace func(string)) (host *session.Host, items []session.Item, err error)

ReplayTranscript replays a Transcript through a new session, as a Driver drives it: each input, typed line, answer, deadline and declaration it holds is one Host call, each crossing and Restore is answered from it, and it never writes files. Under a real Clock, each Host call reads the `@` line the session takes next, or else the last one. The returned Items are what the session recorded, with the Transcript's comments in place, for the caller to compare.

func RunREPL

func RunREPL(ctx context.Context, input io.Reader, output io.Writer, options REPLOptions) error

RunREPL serializes all Host calls, leaving foreground deadline waits asleep while typed lines queue. Background deadlines continue at the prompt.

func WriteTranscript

func WriteTranscript(items []session.Item) string

Types

type Clock

type Clock interface {
	Now() time.Time
	AfterFunc(d time.Duration, f func()) Timer
}

Clock gives a Pool its readings and its per-Group deadline timers. RealClock reads the wall clock; ManualClock lets a Host or test move time by hand. AfterFunc must not call f before it returns.

func RealClock

func RealClock() Clock

RealClock reads the Host's wall clock.

type ManualClock

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

ManualClock is a Clock that moves only when told to, firing due timers in deadline order. It suits replayable Hosts and tests.

func NewManualClock

func NewManualClock(now time.Time) *ManualClock

NewManualClock starts a ManualClock at now.

func (*ManualClock) Advance

func (c *ManualClock) Advance(d time.Duration)

Advance moves the clock forward by d, then fires every timer now due, earliest first. A Pool pumps those Groups later, reading the clock then, so a Host that needs each deadline's own reading advances to it and waits.

func (*ManualClock) AfterFunc

func (c *ManualClock) AfterFunc(d time.Duration, f func()) Timer

AfterFunc never calls f itself, even when d isn't positive: the next Advance does, Advance(0) included.

func (*ManualClock) Now

func (c *ManualClock) Now() time.Time

type Member

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

Member is one Group in a Pool.

func (*Member) Do

func (m *Member) Do(f func(g *talk.Group) error) error

Do runs f with the Group held, so its worker calls (Load, Reload, Extend, Inspect, Counters, Save and the rest) never overlap a Pump. Queued inputs f makes reach the run queue through OnReady as usual.

func (*Member) Group

func (m *Member) Group() *talk.Group

Group is the Member's Group. Queued calls on it are safe from any goroutine; make worker calls through Do.

func (*Member) Ready

func (m *Member) Ready()

Ready puts the Group on the run queue, once. It is the Group's OnReady.

func (*Member) Remove

func (m *Member) Remove()

Remove takes the Member out of the Pool: it is never pumped again, and its timer is stopped. A Pump already running finishes first.

type Pool

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

Pool pumps many Groups on a fixed set of workers. Each Group has one run queue entry at most and one deadline timer, and is pumped by one worker at a time, so its worker calls never overlap (spec chapter 9, Threads and the input queue).

Example

A Pool pumps each of its Groups when a Delivery or deadline makes it ready, so a Host only loads Scripts and sends messages.

package main

import (
	"context"
	"fmt"

	talk "northtalk.org/go"
	"northtalk.org/go/driver"
)

func main() {
	pool := driver.NewPool(driver.PoolOptions{Workers: 2})
	defer pool.Close()
	core := talk.New()

	var pending []*talk.Pending
	for _, tenant := range []string{"acme", "globex"} {
		member := pool.NewGroup(core, talk.GroupOptions{Name: tenant})
		err := member.Do(func(g *talk.Group) error {
			script, err := g.Load(talk.LoadOptions{Name: "hello", Source: "on hi who\n  return \"hi \" & who\nend hi\n"})
			if err != nil {
				return err
			}
			name, err := talk.Text(tenant)
			if err != nil {
				return err
			}
			_, p, err := script.Request(context.Background(), talk.Message{Name: "hi", Args: []talk.Value{name}})
			pending = append(pending, p)
			return err
		})
		if err != nil {
			panic(err)
		}
	}
	for _, p := range pending {
		<-p.Done()
		value, _ := p.Result()
		text, _ := value.AsText()
		fmt.Println(text)
	}
}
Output:
hi acme
hi globex

func NewPool

func NewPool(o PoolOptions) *Pool

NewPool starts a Pool's workers.

func (*Pool) Close

func (p *Pool) Close()

Close stops the workers after any running Pumps finish, and stops every timer. Queued Groups are left unpumped.

func (*Pool) NewGroup

func (p *Pool) NewGroup(core *talk.Core, o talk.GroupOptions) *Member

NewGroup makes a Group whose OnReady puts it on this Pool's run queue. Any OnReady in o is replaced.

func (*Pool) Wait

func (p *Pool) Wait()

Wait blocks until no Group is queued or being pumped. Groups waiting only on a deadline count as settled; their timers stay armed.

type PoolOptions

type PoolOptions struct {
	Workers int   // goroutines pumping Groups; 0 means GOMAXPROCS
	Clock   Clock // nil means RealClock
	// Pump is passed to every Pump. A FuelSlice sends a Group that still has
	// work to the back of the run queue, so one tenant can't hold a worker.
	Pump talk.PumpOptions
	// OnPump receives every Pump's result on the worker that made it, while
	// the Group is still held, so it may make worker calls such as Reload
	// after a Rewound Pump. It must not block for long.
	OnPump func(m *Member, r talk.PumpResult, err error)
}

PoolOptions configures a Pool.

type REPLOptions

type REPLOptions struct {
	Environment session.Environment
	Prompt      bool
	Interrupt   <-chan os.Signal
	Questions   io.Writer
}

REPLOptions controls terminal-only presentation and I/O, outside parity. Input must be closed by its owner if a blocked reader needs to be released. Interrupt cancels a foreground Run or drops an unfinished Entry. Questions receives a user prompt's question, which isn't Session output; when nil, it is output with a Prompt, and stderr without.

type Timer

type Timer interface{ Stop() bool }

Timer is a pending AfterFunc call.

Jump to

Keyboard shortcuts

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