okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
f7fcae1
main
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 { 12ch := make (chan string ,16 ) 13go func () { 14defer close (ch ) 15ticker := time .NewTicker (interval ) 16defer ticker .Stop () 17for i := 1 ;i <= count ;i ++ { 18select { 19case <- ctx .Done (): 20return 21case <- ticker .C : 22 } 23line := fmt .Sprintf ("event %03d · worker completed a unit of work" ,i ) 24select { 25case <- ctx .Done (): 26return 27case ch <- line : 28 } 29 } 30 }() 31return ch 32} 33func receive (ctx context.Context ,ch <- chan string ) (string ,bool ) { 34select { 35case <- ctx .Done (): 36return "" ,false 37case line ,ok := <- ch : 38return line ,ok 39 } 40}