okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

dawnadd charm tui skillf7fcae1

main
845 B40 linesraw
1package main
2
3import (
4	"context"
5	"fmt"
6	"time"
7)
8
9// Every send and wait participates in the same root lifetime. The bounded
10// channel provides backpressure; records are not dropped to keep drawing fast.
11func produce(ctx context.Context, count int, interval time.Duration) <-chan string {
12	ch := make(chan string, 16)
13	go func() {
14		defer close(ch)
15		ticker := time.NewTicker(interval)
16		defer ticker.Stop()
17		for i := 1; i <= count; i++ {
18			select {
19			case <-ctx.Done():
20				return
21			case <-ticker.C:
22			}
23			line := fmt.Sprintf("event %03d · worker completed a unit of work", i)
24			select {
25			case <-ctx.Done():
26				return
27			case ch <- line:
28			}
29		}
30	}()
31	return ch
32}
33func receive(ctx context.Context, ch <-chan string) (string, bool) {
34	select {
35	case <-ctx.Done():
36		return "", false
37	case line, ok := <-ch:
38		return line, ok
39	}
40}