mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-23 04:06:39 +00:00
bee8458a2d
* feat: add rate limit middleware * chore: update vendor dir * chore: update readme with new dependency * chore: add rate limit infos to swagger.md file * refactor: add ipv6 mask limiter option Add IPv6 CIDR /64 mask * refactor: increase rate limit to 1000 Address https://github.com/superseriousbusiness/gotosocial/pull/741#discussion_r945584800 Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package bytebuffer
|
|
|
|
import (
|
|
"sync"
|
|
"unsafe"
|
|
)
|
|
|
|
// ByteBuffer is a wrapper around a slice to reduce memory allocation while handling blob of data.
|
|
type ByteBuffer struct {
|
|
blob []byte
|
|
}
|
|
|
|
// New creates a new ByteBuffer instance.
|
|
func New() *ByteBuffer {
|
|
entry := bufferPool.Get().(*ByteBuffer)
|
|
entry.blob = entry.blob[:0]
|
|
return entry
|
|
}
|
|
|
|
// Bytes returns the content buffer.
|
|
func (buffer *ByteBuffer) Bytes() []byte {
|
|
return buffer.blob
|
|
}
|
|
|
|
// String returns the content buffer.
|
|
func (buffer *ByteBuffer) String() string {
|
|
// Copied from strings.(*Builder).String
|
|
return *(*string)(unsafe.Pointer(&buffer.blob)) // nolint: gosec
|
|
}
|
|
|
|
// Concat appends given arguments to blob content
|
|
func (buffer *ByteBuffer) Concat(args ...string) {
|
|
for i := range args {
|
|
buffer.blob = append(buffer.blob, args[i]...)
|
|
}
|
|
}
|
|
|
|
// Close recycles underlying resources of encoder.
|
|
func (buffer *ByteBuffer) Close() {
|
|
// Proper usage of a sync.Pool requires each entry to have approximately
|
|
// the same memory cost. To obtain this property when the stored type
|
|
// contains a variably-sized buffer, we add a hard limit on the maximum buffer
|
|
// to place back in the pool.
|
|
//
|
|
// See https://golang.org/issue/23199
|
|
if buffer != nil && cap(buffer.blob) < (1<<16) {
|
|
bufferPool.Put(buffer)
|
|
}
|
|
}
|
|
|
|
// A byte buffer pool to reduce memory allocation pressure.
|
|
var bufferPool = &sync.Pool{
|
|
New: func() interface{} {
|
|
return &ByteBuffer{
|
|
blob: make([]byte, 0, 1024),
|
|
}
|
|
},
|
|
}
|