diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-14 15:06:37 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-12-15 10:13:03 +0300 |
commit | f3438d8f2378facd6d9856078e2d10251f09493f (patch) | |
tree | a21bc2d892fd33a3f73ae7b75220eba4b0a9213d /internal | |
parent | 84a404618f9d5046020bffd9a5d99de80f180efb (diff) |
limithandler: Reorder functions for better readability
The functions in the limithandler's middleware implementation are
seemingly ordered at random. Reorder them to group structures and their
receiver functions together. Furthermore, order constructors right after
their struct.
Diffstat (limited to 'internal')
-rw-r--r-- | internal/middleware/limithandler/middleware.go | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/internal/middleware/limithandler/middleware.go b/internal/middleware/limithandler/middleware.go index c276183a2..df228ce6f 100644 --- a/internal/middleware/limithandler/middleware.go +++ b/internal/middleware/limithandler/middleware.go @@ -7,6 +7,8 @@ import ( "google.golang.org/grpc" ) +var maxConcurrencyPerRepoPerRPC map[string]int + // GetLockKey function defines the lock key of an RPC invocation based on its context type GetLockKey func(context.Context) string @@ -16,15 +18,14 @@ type LimiterMiddleware struct { getLockKey GetLockKey } -type wrappedStream struct { - grpc.ServerStream - info *grpc.StreamServerInfo - limiterMiddleware *LimiterMiddleware - initial bool +// New creates a new rate limiter +func New(getLockKey GetLockKey) LimiterMiddleware { + return LimiterMiddleware{ + methodLimiters: createLimiterConfig(), + getLockKey: getLockKey, + } } -var maxConcurrencyPerRepoPerRPC map[string]int - // UnaryInterceptor returns a Unary Interceptor func (c *LimiterMiddleware) UnaryInterceptor() grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { @@ -53,6 +54,28 @@ func (c *LimiterMiddleware) StreamInterceptor() grpc.StreamServerInterceptor { } } +func createLimiterConfig() map[string]*ConcurrencyLimiter { + result := make(map[string]*ConcurrencyLimiter) + + for fullMethodName, max := range maxConcurrencyPerRepoPerRPC { + result[fullMethodName] = NewLimiter(max, NewPromMonitor("gitaly", fullMethodName)) + } + + return result +} + +// SetMaxRepoConcurrency Configures the max concurrency per repo per RPC +func SetMaxRepoConcurrency(config map[string]int) { + maxConcurrencyPerRepoPerRPC = config +} + +type wrappedStream struct { + grpc.ServerStream + info *grpc.StreamServerInfo + limiterMiddleware *LimiterMiddleware + initial bool +} + func (w *wrappedStream) RecvMsg(m interface{}) error { if err := w.ServerStream.RecvMsg(m); err != nil { return err @@ -97,26 +120,3 @@ func (w *wrappedStream) RecvMsg(m interface{}) error { return nil } } - -// New creates a new rate limiter -func New(getLockKey GetLockKey) LimiterMiddleware { - return LimiterMiddleware{ - methodLimiters: createLimiterConfig(), - getLockKey: getLockKey, - } -} - -func createLimiterConfig() map[string]*ConcurrencyLimiter { - result := make(map[string]*ConcurrencyLimiter) - - for fullMethodName, max := range maxConcurrencyPerRepoPerRPC { - result[fullMethodName] = NewLimiter(max, NewPromMonitor("gitaly", fullMethodName)) - } - - return result -} - -// SetMaxRepoConcurrency Configures the max concurrency per repo per RPC -func SetMaxRepoConcurrency(config map[string]int) { - maxConcurrencyPerRepoPerRPC = config -} |