Skip to content

Commit d663e3d

Browse files
committed
Use pool to avoid allocations
1 parent 73ab839 commit d663e3d

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

gzip.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"net/http"
2525
"strings"
26+
"sync"
2627

2728
"github.com/klauspost/compress/gzip"
2829
"gopkg.in/macaron.v1"
@@ -42,6 +43,32 @@ type nopCloser struct {
4243

4344
func (nopCloser) Close() error { return nil }
4445

46+
type WriterPool struct {
47+
pool sync.Pool
48+
}
49+
50+
func NewWriterPool() *WriterPool {
51+
return &WriterPool{pool: sync.Pool{
52+
New: func() interface{} { return nil },
53+
}}
54+
}
55+
56+
func (wp *WriterPool) Get(rw macaron.ResponseWriter) *gzip.Writer {
57+
ret := wp.pool.Get()
58+
if ret == nil {
59+
ret = gzip.NewWriter(rw)
60+
} else {
61+
ret.(*gzip.Writer).Reset(rw)
62+
}
63+
return ret.(*gzip.Writer)
64+
}
65+
66+
func (wp *WriterPool) Put(w *gzip.Writer) {
67+
wp.pool.Put(w)
68+
}
69+
70+
var writerPool WriterPool
71+
4572
/*
4673
Gzip middleware inspired by https://github.com/go-macaron/gzip
4774
@@ -97,7 +124,7 @@ func (grw *gzipResponseWriter) setup() {
97124
headers := grw.Header()
98125
headers.Set(_HEADER_CONTENT_ENCODING, "gzip")
99126
headers.Set(_HEADER_VARY, _HEADER_ACCEPT_ENCODING)
100-
grw.w = gzip.NewWriter(grw.ResponseWriter)
127+
grw.w = writerPool.Get(grw.ResponseWriter)
101128
}
102129
}
103130

@@ -107,6 +134,12 @@ func (grw *gzipResponseWriter) close() {
107134
return
108135
}
109136
grw.w.Close()
137+
138+
if poolWriter, ok := grw.w.(*gzip.Writer); ok {
139+
writerPool.Put(poolWriter)
140+
}
141+
142+
grw.w = nil
110143
}
111144

112145
func (grw *gzipResponseWriter) Write(p []byte) (int, error) {

0 commit comments

Comments
 (0)