response: Add Close method

This commit is contained in:
Adnan Maolood 2021-02-23 17:32:23 -05:00
parent 311233a012
commit ae3fc2fc73
2 changed files with 18 additions and 4 deletions

View File

@ -204,23 +204,29 @@ type ResponseWriter interface {
// Flush sends any buffered data to the client.
Flush() error
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
Close() error
}
type responseWriter struct {
b *bufio.Writer
closer io.Closer
mediatype string
wroteHeader bool
bodyAllowed bool
}
// NewResponseWriter returns a ResponseWriter that uses the provided io.Writer.
func NewResponseWriter(w io.Writer) ResponseWriter {
return newResponseWriter(w)
func NewResponseWriter(wc io.WriteCloser) ResponseWriter {
return newResponseWriter(wc)
}
func newResponseWriter(w io.Writer) *responseWriter {
func newResponseWriter(wc io.WriteCloser) *responseWriter {
return &responseWriter{
b: bufio.NewWriter(w),
b: bufio.NewWriter(wc),
closer: wc,
}
}
@ -266,3 +272,7 @@ func (w *responseWriter) Flush() error {
// Write errors from writeHeader will be returned here.
return w.b.Flush()
}
func (w *responseWriter) Close() error {
return w.closer.Close()
}

View File

@ -102,3 +102,7 @@ func (w *timeoutWriter) writeHeaderLocked(status Status, meta string) {
func (w *timeoutWriter) Flush() error {
return nil
}
func (w *timeoutWriter) Close() error {
return nil
}