-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
in this commit: - chunks out the http request body to avoid making very large allocations. - establishes a limit for the maximum http request body size that the listener will accept. - utilizes a pool of byte buffers to reduce GC pressure.
- Loading branch information
Showing
7 changed files
with
287 additions
and
167 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package http_listener | ||
|
||
import ( | ||
"sync/atomic" | ||
) | ||
|
||
type pool struct { | ||
buffers chan []byte | ||
size int | ||
|
||
created int64 | ||
} | ||
|
||
// NewPool returns a new pool object. | ||
// n is the number of buffers | ||
// bufSize is the size (in bytes) of each buffer | ||
func NewPool(n, bufSize int) *pool { | ||
return &pool{ | ||
buffers: make(chan []byte, n), | ||
size: bufSize, | ||
} | ||
} | ||
|
||
func (p *pool) get() []byte { | ||
select { | ||
case b := <-p.buffers: | ||
return b | ||
default: | ||
atomic.AddInt64(&p.created, 1) | ||
return make([]byte, p.size) | ||
} | ||
} | ||
|
||
func (p *pool) put(b []byte) { | ||
select { | ||
case p.buffers <- b: | ||
default: | ||
} | ||
} | ||
|
||
func (p *pool) ncreated() int64 { | ||
return atomic.LoadInt64(&p.created) | ||
} |
Oops, something went wrong.