mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[chore]: Bump github.com/KimMachineGun/automemlimit from 0.2.6 to 0.3.0 (#2165)
This commit is contained in:
5
vendor/github.com/KimMachineGun/automemlimit/README.md
generated
vendored
5
vendor/github.com/KimMachineGun/automemlimit/README.md
generated
vendored
@ -32,6 +32,11 @@ package main
|
||||
import "github.com/KimMachineGun/automemlimit/memlimit"
|
||||
|
||||
func init() {
|
||||
memlimit.SetGoMemLimitWithOpts(
|
||||
memlimit.WithRatio(0.9),
|
||||
memlimit.WithEnv(),
|
||||
memlimit.WithProvider(memlimit.FromCgroup),
|
||||
)
|
||||
memlimit.SetGoMemLimitWithEnv()
|
||||
memlimit.SetGoMemLimit(0.9)
|
||||
memlimit.SetGoMemLimitWithProvider(memlimit.Limit(1024*1024), 0.9)
|
||||
|
152
vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
generated
vendored
152
vendor/github.com/KimMachineGun/automemlimit/memlimit/memlimit.go
generated
vendored
@ -2,6 +2,7 @@ package memlimit
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
@ -25,10 +26,113 @@ var (
|
||||
ErrNoCgroup = errors.New("process is not in cgroup")
|
||||
// ErrCgroupsNotSupported is returned when the system does not support cgroups.
|
||||
ErrCgroupsNotSupported = errors.New("cgroups is not supported on this system")
|
||||
|
||||
logger = log.New(io.Discard, "", log.LstdFlags)
|
||||
)
|
||||
|
||||
type config struct {
|
||||
logger *log.Logger
|
||||
ratio float64
|
||||
env bool
|
||||
provider Provider
|
||||
}
|
||||
|
||||
// Option is a function that configures the behavior of SetGoMemLimitWithOptions.
|
||||
type Option func(cfg *config)
|
||||
|
||||
// WithRatio configures the ratio of the memory limit to set as GOMEMLIMIT.
|
||||
//
|
||||
// Default: 0.9
|
||||
func WithRatio(ratio float64) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.ratio = ratio
|
||||
}
|
||||
}
|
||||
|
||||
// WithEnv configures whether to use environment variables.
|
||||
//
|
||||
// Default: false
|
||||
func WithEnv() Option {
|
||||
return func(cfg *config) {
|
||||
cfg.env = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithProvider configures the provider.
|
||||
//
|
||||
// Default: FromCgroup
|
||||
func WithProvider(provider Provider) Option {
|
||||
return func(cfg *config) {
|
||||
cfg.provider = provider
|
||||
}
|
||||
}
|
||||
|
||||
// SetGoMemLimitWithOpts sets GOMEMLIMIT with options.
|
||||
//
|
||||
// Options:
|
||||
// - WithRatio
|
||||
// - WithEnv (see more SetGoMemLimitWithEnv)
|
||||
// - WithProvider
|
||||
func SetGoMemLimitWithOpts(opts ...Option) (_ int64, _err error) {
|
||||
cfg := &config{
|
||||
logger: log.New(io.Discard, "", log.LstdFlags),
|
||||
ratio: defaultAUTOMEMLIMIT,
|
||||
env: false,
|
||||
provider: FromCgroup,
|
||||
}
|
||||
if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" {
|
||||
cfg.logger = log.Default()
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(cfg)
|
||||
}
|
||||
defer func() {
|
||||
if _err != nil {
|
||||
cfg.logger.Println(_err)
|
||||
}
|
||||
}()
|
||||
|
||||
snapshot := debug.SetMemoryLimit(-1)
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
if _err != nil {
|
||||
cfg.logger.Println(_err)
|
||||
}
|
||||
_err = fmt.Errorf("panic during setting the Go's memory limit, rolling back to previous value %d: %v", snapshot, err)
|
||||
debug.SetMemoryLimit(snapshot)
|
||||
}
|
||||
}()
|
||||
|
||||
if val, ok := os.LookupEnv(envGOMEMLIMIT); ok {
|
||||
cfg.logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
|
||||
return
|
||||
}
|
||||
|
||||
ratio := cfg.ratio
|
||||
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
|
||||
if val == "off" {
|
||||
cfg.logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
|
||||
return
|
||||
}
|
||||
_ratio, err := strconv.ParseFloat(val, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("cannot parse AUTOMEMLIMIT: %s", val)
|
||||
}
|
||||
ratio = _ratio
|
||||
}
|
||||
if ratio <= 0 || ratio > 1 {
|
||||
return 0, fmt.Errorf("invalid AUTOMEMLIMIT: %f", ratio)
|
||||
}
|
||||
|
||||
limit, err := SetGoMemLimitWithProvider(cfg.provider, ratio)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to set GOMEMLIMIT: %w", err)
|
||||
}
|
||||
|
||||
cfg.logger.Printf("GOMEMLIMIT=%d\n", limit)
|
||||
|
||||
return limit, nil
|
||||
}
|
||||
|
||||
// SetGoMemLimitWithEnv sets GOMEMLIMIT with the value from the environment variable.
|
||||
// You can configure how much memory of the cgroup's memory limit to set as GOMEMLIMIT
|
||||
// through AUTOMEMLIMIT in the half-open range (0.0,1.0].
|
||||
@ -36,49 +140,7 @@ var (
|
||||
// If AUTOMEMLIMIT is not set, it defaults to 0.9. (10% is the headroom for memory sources the Go runtime is unaware of.)
|
||||
// If GOMEMLIMIT is already set or AUTOMEMLIMIT=off, this function does nothing.
|
||||
func SetGoMemLimitWithEnv() {
|
||||
snapshot := debug.SetMemoryLimit(-1)
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
logger.Printf("panic during SetGoMemLimitWithEnv, rolling back to previous value %d: %v\n", snapshot, err)
|
||||
debug.SetMemoryLimit(snapshot)
|
||||
}
|
||||
}()
|
||||
|
||||
if os.Getenv(envAUTOMEMLIMIT_DEBUG) == "true" {
|
||||
logger = log.Default()
|
||||
}
|
||||
|
||||
if val, ok := os.LookupEnv(envGOMEMLIMIT); ok {
|
||||
logger.Printf("GOMEMLIMIT is set already, skipping: %s\n", val)
|
||||
return
|
||||
}
|
||||
|
||||
ratio := defaultAUTOMEMLIMIT
|
||||
if val, ok := os.LookupEnv(envAUTOMEMLIMIT); ok {
|
||||
if val == "off" {
|
||||
logger.Printf("AUTOMEMLIMIT is set to off, skipping\n")
|
||||
return
|
||||
}
|
||||
_ratio, err := strconv.ParseFloat(val, 64)
|
||||
if err != nil {
|
||||
logger.Printf("cannot parse AUTOMEMLIMIT: %s\n", val)
|
||||
return
|
||||
}
|
||||
ratio = _ratio
|
||||
}
|
||||
if ratio <= 0 || ratio > 1 {
|
||||
logger.Printf("invalid AUTOMEMLIMIT: %f\n", ratio)
|
||||
return
|
||||
}
|
||||
|
||||
limit, err := SetGoMemLimit(ratio)
|
||||
if err != nil {
|
||||
logger.Printf("failed to set GOMEMLIMIT: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Printf("GOMEMLIMIT=%d\n", limit)
|
||||
_, _ = SetGoMemLimitWithOpts(WithEnv())
|
||||
}
|
||||
|
||||
// SetGoMemLimit sets GOMEMLIMIT with the value from the cgroup's memory limit and given ratio.
|
||||
|
65
vendor/github.com/ulule/limiter/v3/drivers/middleware/gin/middleware.go
generated
vendored
65
vendor/github.com/ulule/limiter/v3/drivers/middleware/gin/middleware.go
generated
vendored
@ -1,65 +0,0 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/ulule/limiter/v3"
|
||||
)
|
||||
|
||||
// Middleware is the middleware for gin.
|
||||
type Middleware struct {
|
||||
Limiter *limiter.Limiter
|
||||
OnError ErrorHandler
|
||||
OnLimitReached LimitReachedHandler
|
||||
KeyGetter KeyGetter
|
||||
ExcludedKey func(string) bool
|
||||
}
|
||||
|
||||
// NewMiddleware return a new instance of a gin middleware.
|
||||
func NewMiddleware(limiter *limiter.Limiter, options ...Option) gin.HandlerFunc {
|
||||
middleware := &Middleware{
|
||||
Limiter: limiter,
|
||||
OnError: DefaultErrorHandler,
|
||||
OnLimitReached: DefaultLimitReachedHandler,
|
||||
KeyGetter: DefaultKeyGetter,
|
||||
ExcludedKey: nil,
|
||||
}
|
||||
|
||||
for _, option := range options {
|
||||
option.apply(middleware)
|
||||
}
|
||||
|
||||
return func(ctx *gin.Context) {
|
||||
middleware.Handle(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle gin request.
|
||||
func (middleware *Middleware) Handle(c *gin.Context) {
|
||||
key := middleware.KeyGetter(c)
|
||||
if middleware.ExcludedKey != nil && middleware.ExcludedKey(key) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
context, err := middleware.Limiter.Get(c, key)
|
||||
if err != nil {
|
||||
middleware.OnError(c, err)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Header("X-RateLimit-Limit", strconv.FormatInt(context.Limit, 10))
|
||||
c.Header("X-RateLimit-Remaining", strconv.FormatInt(context.Remaining, 10))
|
||||
c.Header("X-RateLimit-Reset", strconv.FormatInt(context.Reset, 10))
|
||||
|
||||
if context.Reached {
|
||||
middleware.OnLimitReached(c)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
71
vendor/github.com/ulule/limiter/v3/drivers/middleware/gin/options.go
generated
vendored
71
vendor/github.com/ulule/limiter/v3/drivers/middleware/gin/options.go
generated
vendored
@ -1,71 +0,0 @@
|
||||
package gin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Option is used to define Middleware configuration.
|
||||
type Option interface {
|
||||
apply(*Middleware)
|
||||
}
|
||||
|
||||
type option func(*Middleware)
|
||||
|
||||
func (o option) apply(middleware *Middleware) {
|
||||
o(middleware)
|
||||
}
|
||||
|
||||
// ErrorHandler is an handler used to inform when an error has occurred.
|
||||
type ErrorHandler func(c *gin.Context, err error)
|
||||
|
||||
// WithErrorHandler will configure the Middleware to use the given ErrorHandler.
|
||||
func WithErrorHandler(handler ErrorHandler) Option {
|
||||
return option(func(middleware *Middleware) {
|
||||
middleware.OnError = handler
|
||||
})
|
||||
}
|
||||
|
||||
// DefaultErrorHandler is the default ErrorHandler used by a new Middleware.
|
||||
func DefaultErrorHandler(c *gin.Context, err error) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// LimitReachedHandler is an handler used to inform when the limit has exceeded.
|
||||
type LimitReachedHandler func(c *gin.Context)
|
||||
|
||||
// WithLimitReachedHandler will configure the Middleware to use the given LimitReachedHandler.
|
||||
func WithLimitReachedHandler(handler LimitReachedHandler) Option {
|
||||
return option(func(middleware *Middleware) {
|
||||
middleware.OnLimitReached = handler
|
||||
})
|
||||
}
|
||||
|
||||
// DefaultLimitReachedHandler is the default LimitReachedHandler used by a new Middleware.
|
||||
func DefaultLimitReachedHandler(c *gin.Context) {
|
||||
c.String(http.StatusTooManyRequests, "Limit exceeded")
|
||||
}
|
||||
|
||||
// KeyGetter will define the rate limiter key given the gin Context.
|
||||
type KeyGetter func(c *gin.Context) string
|
||||
|
||||
// WithKeyGetter will configure the Middleware to use the given KeyGetter.
|
||||
func WithKeyGetter(handler KeyGetter) Option {
|
||||
return option(func(middleware *Middleware) {
|
||||
middleware.KeyGetter = handler
|
||||
})
|
||||
}
|
||||
|
||||
// DefaultKeyGetter is the default KeyGetter used by a new Middleware.
|
||||
// It returns the Client IP address.
|
||||
func DefaultKeyGetter(c *gin.Context) string {
|
||||
return c.ClientIP()
|
||||
}
|
||||
|
||||
// WithExcludedKey will configure the Middleware to ignore key(s) using the given function.
|
||||
func WithExcludedKey(handler func(string) bool) Option {
|
||||
return option(func(middleware *Middleware) {
|
||||
middleware.ExcludedKey = handler
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user