mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-10-10 04:02:29 +02:00
* pauseThreshold in AsyncBuffer * Additional close() condition * io.ReadCloser in buffer * ticker + fixes * Minor fixes for asyncbuffer * Renamed ticker to cond * warn if close of upstream reader failed * ticker -> chunkCond * Fix io.EOF behaviour
59 lines
937 B
Go
59 lines
937 B
Go
package asyncbuffer
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type condCh = chan struct{}
|
|
|
|
// Cond signals that an event has occurred to a multiple waiters.
|
|
type Cond struct {
|
|
mu sync.RWMutex
|
|
ch condCh
|
|
closeOnce sync.Once
|
|
}
|
|
|
|
// NewCond creates a new Ticker instance with an initialized channel.
|
|
func NewCond() *Cond {
|
|
return &Cond{
|
|
ch: make(condCh),
|
|
}
|
|
}
|
|
|
|
// Tick signals that an event has occurred by closing the channel.
|
|
func (t *Cond) Tick() {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
|
|
if t.ch != nil {
|
|
close(t.ch)
|
|
t.ch = make(condCh)
|
|
}
|
|
}
|
|
|
|
// Wait blocks until the channel is closed, indicating that an event has occurred.
|
|
func (t *Cond) Wait() {
|
|
t.mu.RLock()
|
|
ch := t.ch
|
|
t.mu.RUnlock()
|
|
|
|
if ch == nil {
|
|
return
|
|
}
|
|
|
|
<-ch
|
|
}
|
|
|
|
// Close closes the ticker channel and prevents further ticks.
|
|
func (t *Cond) Close() {
|
|
t.closeOnce.Do(func() {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
|
|
if t.ch != nil {
|
|
close(t.ch)
|
|
t.ch = nil
|
|
}
|
|
})
|
|
}
|