Created
June 6, 2016 10:46
-
-
Save simcap/6fe510d8490b7ea4217343bd1b526e3b to your computer and use it in GitHub Desktop.
Prez - Collectors Pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
bodyBuf := getBuf() | |
_, err := bodyBuf.ReadFrom(body) | |
if err != nil { | |
putBuf(bodyBuf) | |
jsonError(w, http.StatusInternalServerError, "problem reading request body") | |
return | |
} | |
// ... | |
var bufPool = sync.Pool{New: func() interface{} { return new(bytes.Buffer) }} | |
func getBuf() *bytes.Buffer { | |
if bb, ok := bufPool.Get().(*bytes.Buffer); ok { | |
return bb | |
} | |
return new(bytes.Buffer) | |
} | |
func putBuf(b *bytes.Buffer) { | |
b.Reset() | |
bufPool.Put(b) | |
} | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment