Update multireader.go

more description
This commit is contained in:
XoxJlopeZi4BB 2025-07-09 00:42:50 +04:00
parent c80e6f9d5b
commit 5cfec0f2ef

View File

@ -15,13 +15,13 @@ func tickReader(sem chan struct{}, ticker *time.Ticker, s string) io.Reader {
r, w := io.Pipe() r, w := io.Pipe()
go func() { go func() {
for { for {
sem <- struct{}{} sem <- struct{}{} // acquire a ticket, possibly waiting for it
select { select {
case t := <-ticker.C: case t := <-ticker.C:
fmt.Fprintf(w, "%s with %v\n", s, t) fmt.Fprintf(w, "%s with %v\n", s, t)
default: // try to comment this and then run :) default: // try to comment this and then run :)
} }
<-sem <-sem // add a ticket to the semaphore
} }
}() }()
return r return r
@ -45,7 +45,10 @@ func tickToReader(semaphore chan struct{}, v tick) (io.Reader, error) {
func main() { func main() {
var ( var (
wg sync.WaitGroup wg sync.WaitGroup
semaphore = make(chan struct{}, 1) semaphore = make(
chan struct{},
1, // only one goroutine can do its thing
)
) )
var tickers = []tick{ var tickers = []tick{