package server import ( "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) // Output flow of machine terminals (protocol.TerminalEvent). Each // connection's subscription to a terminal is a cursor into the terminal's // retained output; the connection's writer sends from it as soon as output // is there, so a frame carries what arrived while the previous write was in // flight. A flow-controlled subscription is sent at most // TerminalFlowWindow bytes past its last terminal_ack, and while one is full // the terminal's reader stops reading the PTY: the program blocks on its // writes instead of output being skipped. A subscription that stays full // without acknowledging for terminalStall no longer holds the terminal back; // it sees a gap later and reads the tail again. While output streams, a // frame waits up to terminalBurst for more to join it. A subscription // without flow (an older client) keeps the old pacing: the newest bytes, one // frame per terminalLegacyPace. const ( terminalStall = 3 * time.Second terminalLegacyPace = 50 * time.Millisecond terminalBurst = 2 * time.Millisecond ) // terminalView is one connection's subscription to one terminal's output. // Its fields after p are guarded by p.mu. type terminalView struct { client *terminalClient p *terminalProcess flow bool // sent is the next byte to send; acked is the end the client consumed. sent, acked int64 // fullSince is when the window was last seen full without progress. fullSince time.Time // paused: a terminal_output reply is on its way; frames wait for it. paused bool lastSent time.Time gone bool } func (p *terminalProcess) id() string { p.mu.Lock() defer p.mu.Unlock() return p.record.ID } // join adds v to the terminal's subscriptions. func (p *terminalProcess) join(v *terminalView) { p.mu.Lock() defer p.mu.Unlock() p.views[v] = struct{}{} } // leave ends v: it is sent nothing more and no longer holds the reader. func (v *terminalView) leave() { p := v.p p.mu.Lock() v.gone = true delete(p.views, v) p.mu.Unlock() p.signalRoom() } // next takes the frame v may be sent now and advances v past it. force // ignores the window and the pacing (an ended terminal's rest). again is how // long a paced subscription waits before its next frame. func (v *terminalView) next(now time.Time, force bool) (frame protocol.TerminalEventFrame, ok bool, again time.Duration) { p := v.p p.mu.Lock() defer p.mu.Unlock() if v.gone || v.paused { return frame, false, 0 } out := p.out if v.sent < out.start { // The bytes after sent were dropped (a stale subscription). Once it // acknowledges again the stream goes on after a gap, with a fresh // window: the client reads the tail again (terminal_output). if v.flow && !force && v.sent-v.acked >= protocol.TerminalFlowWindow { return frame, false, 0 } v.sent, v.acked = out.start, out.start } from, limit := v.sent, out.total switch { case force: case v.flow: limit = min(limit, v.acked+protocol.TerminalFlowWindow) // Streaming (a frame went out within terminalBurst): let a small // remainder gather into one frame rather than one per PTY read. // A keystroke's echo after a pause goes at once. waiting := limit - from if since := now.Sub(v.lastSent); waiting > 0 && waiting < protocol.TerminalChunkLimit && limit == out.total && !p.outDone && since < terminalBurst { return frame, false, terminalBurst - since } default: if out.total <= v.sent { return frame, false, 0 } if wait := terminalLegacyPace - now.Sub(v.lastSent); wait > 0 { return frame, false, wait } from = out.newestFrom(from, protocol.TerminalChunkLimit) } text, offset, end, ok := out.chunk(from, limit, protocol.TerminalChunkLimit, p.outDone) if !ok { return frame, false, 0 } v.sent, v.lastSent = end, now return protocol.TerminalEventFrame{TerminalID: p.record.ID, Output: text, Offset: offset, End: end}, true, 0 } // ack records that the client consumed the output up to end. func (v *terminalView) ack(end int64) { p := v.p p.mu.Lock() end = min(end, v.sent) advanced := end > v.acked if advanced { v.acked, v.fullSince = end, time.Time{} } p.mu.Unlock() if advanced { p.signalRoom() v.client.poke() } } // resetLocked restarts v at end (a terminal_output reply covers what was // before), paused until that reply is written. p.mu is held. func (v *terminalView) resetLocked(end int64) { v.sent, v.acked, v.fullSince, v.paused = end, end, time.Time{}, true } // release lets frames follow the terminal_output reply just written. func (v *terminalView) release() { v.p.mu.Lock() v.paused = false v.p.mu.Unlock() v.client.poke() } // pokeViewsLocked wakes the writer of every subscription. p.mu is held. func (p *terminalProcess) pokeViewsLocked() { for v := range p.views { v.client.poke() } } func (p *terminalProcess) signalRoom() { select { case p.room <- struct{}{}: default: } } // heldLocked reports whether a flow-controlled subscription is full and // still within terminalStall of its last progress, and how long until the // first such one goes stale. p.mu is held. func (p *terminalProcess) heldLocked(now time.Time) (bool, time.Duration) { held, wait := false, time.Duration(0) for v := range p.views { if !v.flow || v.gone { continue } if p.out.total-v.acked < protocol.TerminalFlowWindow { v.fullSince = time.Time{} continue } if v.fullSince.IsZero() { v.fullSince = now } left := terminalStall - now.Sub(v.fullSince) if left <= 0 { continue } if !held || left < wait { wait = left } held = true } return held, wait } // awaitRoom blocks the reader while a live flow-controlled subscription is // full. Once the attachment exited its output is drained regardless. func (p *terminalProcess) awaitRoom() { for { p.mu.Lock() held, wait := p.heldLocked(time.Now()) p.mu.Unlock() if !held { return } timer := time.NewTimer(wait) select { case <-p.room: case <-p.exited: timer.Stop() return case <-timer.C: } timer.Stop() } }