30 lines
537 B
Go
30 lines
537 B
Go
package agent
|
|
|
|
import "sync"
|
|
|
|
// Fifo is a thread-safe queue for buffered prompts.
|
|
type Fifo struct {
|
|
mu sync.Mutex
|
|
items []string
|
|
}
|
|
|
|
// Push appends a prompt to the queue.
|
|
func (f *Fifo) Push(s string) {
|
|
f.mu.Lock()
|
|
f.items = append(f.items, s)
|
|
f.mu.Unlock()
|
|
}
|
|
|
|
// Pop removes and returns the next prompt.
|
|
// Returns ("", false) if the queue is empty.
|
|
func (f *Fifo) Pop() (string, bool) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
if len(f.items) == 0 {
|
|
return "", false
|
|
}
|
|
s := f.items[0]
|
|
f.items = f.items[1:]
|
|
return s, true
|
|
}
|