-
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
2 changed files
with
182 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package http_listener | ||
|
||
type pool struct { | ||
buffers chan []byte | ||
} | ||
|
||
func NewPool(n int) *pool { | ||
p := &pool{ | ||
buffers: make(chan []byte, n), | ||
} | ||
for i := 0; i < n; i++ { | ||
p.buffers <- make([]byte, MAX_LINE_SIZE) | ||
} | ||
return p | ||
} | ||
|
||
func (p *pool) get() []byte { | ||
select { | ||
case b := <-p.buffers: | ||
return b | ||
default: | ||
// pool is empty, so make a new buffer | ||
return make([]byte, MAX_LINE_SIZE) | ||
} | ||
} | ||
|
||
func (p *pool) put(b []byte) { | ||
select { | ||
case p.buffers <- b: | ||
default: | ||
// the pool is full, so drop this buffer | ||
b = nil | ||
} | ||
} |
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