File size: 942 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package sendit

import (
	"io"
	"log"
	"net/http"
)

type flushWriter struct {
	write io.Writer
	flush func()
}

func (f *flushWriter) Write(p []byte) (n int, err error) {
	n, err = f.write.Write(p)
	if err == nil {
		defer func() {
			err := recover()
			if err != nil {
				// .flush() calls Flush() on the http.ResponseWriter
				// However, if the connection errs, the underlying connection
				// can be closed before we return the handlers goroutine.
				//
				// This means it's possible for the Flush() call to panic as
				// the `finalFlush` sets bufw to nil.
				log.Println("ERROR:", err)
			}
		}()
		f.flush()
	}
	return n, err
}

// FlushWriter will call .Flush() immediately after every call to .Write() on the returned io.Writer.
//
// If w does not implement http.Flusher, it panics.
func FlushWriter(w io.Writer) io.Writer {
	flush := w.(http.Flusher).Flush
	flush()
	return &flushWriter{
		write: w,
		flush: flush,
	}
}