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 ¶
- Variables
- func ParseTranscript(source string) ([]session.Item, error)
- func PromptAnswer(p session.Prompt, line string) (talk.Value, bool)
- func PromptLines(p session.Prompt) []string
- func ReplayTranscript(recorded []session.Item, trace func(string)) (host *session.Host, items []session.Item, err error)
- func RunREPL(ctx context.Context, input io.Reader, output io.Writer, options REPLOptions) error
- func WriteTranscript(items []session.Item) string
- type Clock
- type ManualClock
- type Member
- type Pool
- type PoolOptions
- type REPLOptions
- type Timer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("driver: pool closed")
ErrClosed is returned for work offered to a closed Pool or a removed Member.
Functions ¶
func PromptAnswer ¶
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 ¶
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 ¶
RunREPL serializes all Host calls, leaving foreground deadline waits asleep while typed lines queue. Background deadlines continue at the prompt.
func WriteTranscript ¶
Types ¶
type Clock ¶
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.
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 ¶
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 ¶
Group is the Member's Group. Queued calls on it are safe from any goroutine; make worker calls through Do.
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 (*Pool) Close ¶
func (p *Pool) Close()
Close stops the workers after any running Pumps finish, and stops every timer. Queued Groups are left unpumped.
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.