New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: bufio: Writer.SetFlushTimeout method #20353
Comments
See https://golang.org/doc/faq#x_in_std This is only a dozen some lines of code for people who need this. Does this already exist on GitHub? We're well past just adding things to the standard library because we can. We prefer things to exist not in the standard library. Would this be used by anything already in the standard library? |
I don't understand why the threshold should be "would be be used in the standard library". That seems pretty circular. |
It's just a pretty good proxy for whether it'd also be useful outside of the standard library. There's enough code in the standard library to get an idea of whether it'd be useful. See recent #20136 for a similar discussion. |
There's already an implementation of this of sorts in the standard library: https://golang.org/src/net/http/httputil/reverseproxy.go func (p *ReverseProxy) copyResponse(dst io.Writer, src io.Reader) {
if p.FlushInterval != 0 {
if wf, ok := dst.(writeFlusher); ok {
mlw := &maxLatencyWriter{
dst: wf,
latency: p.FlushInterval,
done: make(chan bool),
}
go mlw.flushLoop()
defer mlw.stop()
dst = mlw
}
} .... type maxLatencyWriter struct {
dst writeFlusher
latency time.Duration
mu sync.Mutex // protects Write + Flush
done chan bool
}
func (m *maxLatencyWriter) Write(p []byte) (int, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.dst.Write(p)
}
func (m *maxLatencyWriter) flushLoop() {
t := time.NewTicker(m.latency)
defer t.Stop()
for {
select {
case <-m.done:
if onExitFlushLoop != nil {
onExitFlushLoop()
}
return
case <-t.C:
m.mu.Lock()
m.dst.Flush()
m.mu.Unlock()
}
}
}
func (m *maxLatencyWriter) stop() { m.done <- true } It's not quite the same, but pretty close. So that's ~one caller. Where else needs this, even outside the standard library? Do many people end up implementing this? Is there a current popular implementation people use? |
I'd be pretty unhappy to see the notion of time introduced into a low-level package like bufio. |
This is easily done in a third-party package, and it's far from clear we want to put time into bufio. Still waiting for any rationale about usage level. |
No objections raised, so closing. |
Proposal: add a Writer.SetFlushTimeout method which will set a timeout to flush the buffer after a period of inactivity.
Rationale: for performance reasons it's common to not call Flush after every Write. However, in some applications (such as writing messages to a connection or logfiles), timely flushing of data will improve responsiveness or ensure more log data are written prior to a fatal error/panic.
Proposed method signature:
func (w *Writer) SetFlushTimeout(timeout time.Duration) {}
The text was updated successfully, but these errors were encountered: