github.com/puellanivis/breton@v0.2.16/lib/io/bufpipe/option.go (about) 1 package bufpipe 2 3 // Option defines a function that will apply a specific value or feature to a given Pipe. 4 type Option func(*Pipe) Option 5 6 // WithAutoFlush sets the internal buffer size which shall trigger an Automatic Flush. 7 // If this size is negative, then no automatic flushing will ever trigger. 8 // Otherwise, after any successful Write, if the buffer size is greater than this value, 9 // a Flush will be triggered automatically before returning to the calling program. 10 func WithAutoFlush(size int) Option { 11 return func(p *Pipe) Option { 12 p.mu.Lock() 13 defer p.mu.Unlock() 14 15 save := p.autoFlush 16 17 p.autoFlush = size 18 19 return WithAutoFlush(save) 20 } 21 } 22 23 // WithNoAutoFlush is a more readable version of WithAutoFlush(-1). 24 func WithNoAutoFlush() Option { 25 return WithAutoFlush(-1) 26 } 27 28 // WithMaxOutstanding sets the maximal size of the internal buffer before a Read is forced. 29 // If this value is set to be greater than zero, if any Write would cause the internal buffer to exceed this value, 30 // then that Write will block until a Read is performed that empties the internal buffer. 31 func WithMaxOutstanding(size int) Option { 32 return func(p *Pipe) Option { 33 p.mu.Lock() 34 defer p.mu.Unlock() 35 36 save := p.maxOutstanding 37 38 p.maxOutstanding = size 39 40 return WithMaxOutstanding(save) 41 } 42 }