From 7334211495306a699f6b2c3df12bb24b48f190fe Mon Sep 17 00:00:00 2001 From: David Weitzman Date: Tue, 26 May 2020 16:41:49 -0700 Subject: [PATCH] Separate redis-specific logic from generic k/v store logic This is a pure refactoring with no behavior changes as a step toward being able to add memcache support (see #140). RateLimitCache moves from the redis package to the new limiter package, along with code for time/jitter and building cache keys. Those can all be reused for memcache. The redis package is imported in exactly two places: - in service_cmd/runner/runner.go to call redis.NewRateLimiterCacheImplFromSettings() - in service/ratelimit.go in ShouldRateLimit to identify if a recovered panic is a redis.RedisError. If so, a stat is incremented and the panic() propagation is ended and in favor of returning the error as a the function result. --- src/{redis => limiter}/cache.go | 2 +- src/limiter/cache_key.go | 90 ++++++++ src/{redis => limiter}/local_cache_stats.go | 2 +- src/limiter/time.go | 40 ++++ src/redis/cache_impl.go | 170 ++++----------- src/server/server_impl.go | 5 +- src/service/ratelimit.go | 7 +- src/service_cmd/runner/runner.go | 20 +- test/mocks/config/config.go | 10 +- test/mocks/limiter/limiter.go | 136 ++++++++++++ test/mocks/mocks.go | 3 +- test/mocks/redis/redis.go | 226 +++++++------------- test/mocks/runtime/loader/loader.go | 4 + test/mocks/runtime/snapshot/snapshot.go | 16 ++ test/redis/cache_impl_test.go | 16 +- test/service/ratelimit_test.go | 16 +- 16 files changed, 442 insertions(+), 321 deletions(-) rename src/{redis => limiter}/cache.go (98%) create mode 100644 src/limiter/cache_key.go rename src/{redis => limiter}/local_cache_stats.go (98%) create mode 100644 src/limiter/time.go create mode 100644 test/mocks/limiter/limiter.go diff --git a/src/redis/cache.go b/src/limiter/cache.go similarity index 98% rename from src/redis/cache.go rename to src/limiter/cache.go index b090cc995..2ca16956f 100644 --- a/src/redis/cache.go +++ b/src/limiter/cache.go @@ -1,4 +1,4 @@ -package redis +package limiter import ( pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" diff --git a/src/limiter/cache_key.go b/src/limiter/cache_key.go new file mode 100644 index 000000000..65540fa22 --- /dev/null +++ b/src/limiter/cache_key.go @@ -0,0 +1,90 @@ +package limiter + +import ( + "bytes" + "strconv" + "sync" + + pb_struct "github.com/envoyproxy/go-control-plane/envoy/api/v2/ratelimit" + pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" + "github.com/envoyproxy/ratelimit/src/config" +) + +type CacheKeyGenerator struct { + // bytes.Buffer pool used to efficiently generate cache keys. + bufferPool sync.Pool +} + +func NewCacheKeyGenerator() CacheKeyGenerator { + return CacheKeyGenerator{bufferPool: sync.Pool{ + New: func() interface{} { + return new(bytes.Buffer) + }, + }} +} + +type CacheKey struct { + Key string + // True if the key corresponds to a limit with a SECOND unit. False otherwise. + PerSecond bool +} + +func isPerSecondLimit(unit pb.RateLimitResponse_RateLimit_Unit) bool { + return unit == pb.RateLimitResponse_RateLimit_SECOND +} + +// Convert a rate limit into a time divider. +// @param unit supplies the unit to convert. +// @return the divider to use in time computations. +func UnitToDivider(unit pb.RateLimitResponse_RateLimit_Unit) int64 { + switch unit { + case pb.RateLimitResponse_RateLimit_SECOND: + return 1 + case pb.RateLimitResponse_RateLimit_MINUTE: + return 60 + case pb.RateLimitResponse_RateLimit_HOUR: + return 60 * 60 + case pb.RateLimitResponse_RateLimit_DAY: + return 60 * 60 * 24 + } + + panic("should not get here") +} + +// Generate a cache key for a limit lookup. +// @param domain supplies the cache key domain. +// @param descriptor supplies the descriptor to generate the key for. +// @param limit supplies the rate limit to generate the key for (may be nil). +// @param now supplies the current unix time. +// @return CacheKey struct. +func (this *CacheKeyGenerator) GenerateCacheKey( + domain string, descriptor *pb_struct.RateLimitDescriptor, limit *config.RateLimit, now int64) CacheKey { + + if limit == nil { + return CacheKey{ + Key: "", + PerSecond: false, + } + } + + b := this.bufferPool.Get().(*bytes.Buffer) + defer this.bufferPool.Put(b) + b.Reset() + + b.WriteString(domain) + b.WriteByte('_') + + for _, entry := range descriptor.Entries { + b.WriteString(entry.Key) + b.WriteByte('_') + b.WriteString(entry.Value) + b.WriteByte('_') + } + + divider := UnitToDivider(limit.Limit.Unit) + b.WriteString(strconv.FormatInt((now/divider)*divider, 10)) + + return CacheKey{ + Key: b.String(), + PerSecond: isPerSecondLimit(limit.Limit.Unit)} +} diff --git a/src/redis/local_cache_stats.go b/src/limiter/local_cache_stats.go similarity index 98% rename from src/redis/local_cache_stats.go rename to src/limiter/local_cache_stats.go index 60a94194f..d0d59dc27 100644 --- a/src/redis/local_cache_stats.go +++ b/src/limiter/local_cache_stats.go @@ -1,4 +1,4 @@ -package redis +package limiter import ( "github.com/coocood/freecache" diff --git a/src/limiter/time.go b/src/limiter/time.go new file mode 100644 index 000000000..e6a779e70 --- /dev/null +++ b/src/limiter/time.go @@ -0,0 +1,40 @@ +package limiter + +import ( + "math/rand" + "sync" + "time" +) + +type timeSourceImpl struct{} + +func NewTimeSourceImpl() TimeSource { + return &timeSourceImpl{} +} + +func (this *timeSourceImpl) UnixNow() int64 { + return time.Now().Unix() +} + +// rand for jitter. +type lockedSource struct { + lk sync.Mutex + src rand.Source +} + +func NewLockedSource(seed int64) JitterRandSource { + return &lockedSource{src: rand.NewSource(seed)} +} + +func (r *lockedSource) Int63() (n int64) { + r.lk.Lock() + n = r.src.Int63() + r.lk.Unlock() + return +} + +func (r *lockedSource) Seed(seed int64) { + r.lk.Lock() + r.src.Seed(seed) + r.lk.Unlock() +} diff --git a/src/redis/cache_impl.go b/src/redis/cache_impl.go index 5cc7b63e3..fbccad3f3 100644 --- a/src/redis/cache_impl.go +++ b/src/redis/cache_impl.go @@ -1,18 +1,16 @@ package redis import ( - "bytes" "math" "math/rand" - "strconv" - "sync" - "time" "github.com/coocood/freecache" - pb_struct "github.com/envoyproxy/go-control-plane/envoy/api/v2/ratelimit" pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" "github.com/envoyproxy/ratelimit/src/assert" "github.com/envoyproxy/ratelimit/src/config" + "github.com/envoyproxy/ratelimit/src/limiter" + "github.com/envoyproxy/ratelimit/src/server" + "github.com/envoyproxy/ratelimit/src/settings" logger "github.com/sirupsen/logrus" "golang.org/x/net/context" ) @@ -24,72 +22,11 @@ type rateLimitCacheImpl struct { // limits regardless of unit. If this pool is not nil, then it // is used for limits that have a SECOND unit. perSecondPool Pool - timeSource TimeSource + timeSource limiter.TimeSource jitterRand *rand.Rand expirationJitterMaxSeconds int64 - // bytes.Buffer pool used to efficiently generate cache keys. - bufferPool sync.Pool - localCache *freecache.Cache -} - -// Convert a rate limit into a time divider. -// @param unit supplies the unit to convert. -// @return the divider to use in time computations. -func unitToDivider(unit pb.RateLimitResponse_RateLimit_Unit) int64 { - switch unit { - case pb.RateLimitResponse_RateLimit_SECOND: - return 1 - case pb.RateLimitResponse_RateLimit_MINUTE: - return 60 - case pb.RateLimitResponse_RateLimit_HOUR: - return 60 * 60 - case pb.RateLimitResponse_RateLimit_DAY: - return 60 * 60 * 24 - } - - panic("should not get here") -} - -// Generate a cache key for a limit lookup. -// @param domain supplies the cache key domain. -// @param descriptor supplies the descriptor to generate the key for. -// @param limit supplies the rate limit to generate the key for (may be nil). -// @param now supplies the current unix time. -// @return cacheKey struct. -func (this *rateLimitCacheImpl) generateCacheKey( - domain string, descriptor *pb_struct.RateLimitDescriptor, limit *config.RateLimit, now int64) cacheKey { - - if limit == nil { - return cacheKey{ - key: "", - perSecond: false, - } - } - - b := this.bufferPool.Get().(*bytes.Buffer) - defer this.bufferPool.Put(b) - b.Reset() - - b.WriteString(domain) - b.WriteByte('_') - - for _, entry := range descriptor.Entries { - b.WriteString(entry.Key) - b.WriteByte('_') - b.WriteString(entry.Value) - b.WriteByte('_') - } - - divider := unitToDivider(limit.Limit.Unit) - b.WriteString(strconv.FormatInt((now/divider)*divider, 10)) - - return cacheKey{ - key: b.String(), - perSecond: isPerSecondLimit(limit.Limit.Unit)} -} - -func isPerSecondLimit(unit pb.RateLimitResponse_RateLimit_Unit) bool { - return unit == pb.RateLimitResponse_RateLimit_SECOND + cacheKeyGenerator limiter.CacheKeyGenerator + localCache *freecache.Cache } func max(a uint32, b uint32) uint32 { @@ -99,12 +36,6 @@ func max(a uint32, b uint32) uint32 { return b } -type cacheKey struct { - key string - // True if the key corresponds to a limit with a SECOND unit. False otherwise. - perSecond bool -} - func pipelineAppend(conn Connection, key string, hitsAddend uint32, expirationSeconds int64) { conn.PipeAppend("INCRBY", key, hitsAddend) conn.PipeAppend("EXPIRE", key, expirationSeconds) @@ -138,14 +69,15 @@ func (this *rateLimitCacheImpl) DoLimit( // request.HitsAddend could be 0 (default value) if not specified by the caller in the Ratelimit request. hitsAddend := max(1, request.HitsAddend) - // First build a list of all cache keys that we are actually going to hit. generateCacheKey() + // First build a list of all cache keys that we are actually going to hit. GenerateCacheKey() // returns an empty string in the key if there is no limit so that we can keep the arrays // all the same size. assert.Assert(len(request.Descriptors) == len(limits)) - cacheKeys := make([]cacheKey, len(request.Descriptors)) + cacheKeys := make([]limiter.CacheKey, len(request.Descriptors)) now := this.timeSource.UnixNow() for i := 0; i < len(request.Descriptors); i++ { - cacheKeys[i] = this.generateCacheKey(request.Domain, request.Descriptors[i], limits[i], now) + cacheKeys[i] = this.cacheKeyGenerator.GenerateCacheKey( + request.Domain, request.Descriptors[i], limits[i], now) // Increase statistics for limits hit by their respective requests. if limits[i] != nil { @@ -157,32 +89,32 @@ func (this *rateLimitCacheImpl) DoLimit( // Now, actually setup the pipeline, skipping empty cache keys. for i, cacheKey := range cacheKeys { - if cacheKey.key == "" { + if cacheKey.Key == "" { continue } if this.localCache != nil { // Get returns the value or not found error. - _, err := this.localCache.Get([]byte(cacheKey.key)) + _, err := this.localCache.Get([]byte(cacheKey.Key)) if err == nil { isOverLimitWithLocalCache[i] = true - logger.Debugf("cache key is over the limit: %s", cacheKey.key) + logger.Debugf("cache key is over the limit: %s", cacheKey.Key) continue } } - logger.Debugf("looking up cache key: %s", cacheKey.key) + logger.Debugf("looking up cache key: %s", cacheKey.Key) - expirationSeconds := unitToDivider(limits[i].Limit.Unit) + expirationSeconds := limiter.UnitToDivider(limits[i].Limit.Unit) if this.expirationJitterMaxSeconds > 0 { expirationSeconds += this.jitterRand.Int63n(this.expirationJitterMaxSeconds) } // Use the perSecondConn if it is not nil and the cacheKey represents a per second Limit. - if perSecondConn != nil && cacheKey.perSecond { - pipelineAppend(perSecondConn, cacheKey.key, hitsAddend, expirationSeconds) + if perSecondConn != nil && cacheKey.PerSecond { + pipelineAppend(perSecondConn, cacheKey.Key, hitsAddend, expirationSeconds) } else { - pipelineAppend(conn, cacheKey.key, hitsAddend, expirationSeconds) + pipelineAppend(conn, cacheKey.Key, hitsAddend, expirationSeconds) } } @@ -190,7 +122,7 @@ func (this *rateLimitCacheImpl) DoLimit( responseDescriptorStatuses := make([]*pb.RateLimitResponse_DescriptorStatus, len(request.Descriptors)) for i, cacheKey := range cacheKeys { - if cacheKey.key == "" { + if cacheKey.Key == "" { responseDescriptorStatuses[i] = &pb.RateLimitResponse_DescriptorStatus{ Code: pb.RateLimitResponse_OK, @@ -214,7 +146,7 @@ func (this *rateLimitCacheImpl) DoLimit( var limitAfterIncrease uint32 // Use the perSecondConn if it is not nil and the cacheKey represents a per second Limit. - if this.perSecondPool != nil && cacheKey.perSecond { + if this.perSecondPool != nil && cacheKey.PerSecond { limitAfterIncrease = pipelineFetch(perSecondConn) } else { limitAfterIncrease = pipelineFetch(conn) @@ -226,7 +158,7 @@ func (this *rateLimitCacheImpl) DoLimit( // We need to know it in both the OK and OVER_LIMIT scenarios. nearLimitThreshold := uint32(math.Floor(float64(float32(overLimitThreshold) * config.NearLimitRatio))) - logger.Debugf("cache key: %s current: %d", cacheKey.key, limitAfterIncrease) + logger.Debugf("cache key: %s current: %d", cacheKey.Key, limitAfterIncrease) if limitAfterIncrease > overLimitThreshold { responseDescriptorStatuses[i] = &pb.RateLimitResponse_DescriptorStatus{ @@ -257,9 +189,9 @@ func (this *rateLimitCacheImpl) DoLimit( // similar to mongo_1h, mongo_2h, etc. In the hour 1 (0h0m - 0h59m), the cache key is mongo_1h, we start // to get ratelimited in the 50th minute, the ttl of local_cache will be set as 1 hour(0h50m-1h49m). // In the time of 1h1m, since the cache key becomes different (mongo_2h), it won't get ratelimited. - err := this.localCache.Set([]byte(cacheKey.key), []byte{}, int(unitToDivider(limits[i].Limit.Unit))) + err := this.localCache.Set([]byte(cacheKey.Key), []byte{}, int(limiter.UnitToDivider(limits[i].Limit.Unit))) if err != nil { - logger.Errorf("Failing to set local cache key: %s", cacheKey.key) + logger.Errorf("Failing to set local cache key: %s", cacheKey.Key) } } } else { @@ -288,55 +220,31 @@ func (this *rateLimitCacheImpl) DoLimit( return responseDescriptorStatuses } -func NewRateLimitCacheImpl(pool Pool, perSecondPool Pool, timeSource TimeSource, jitterRand *rand.Rand, expirationJitterMaxSeconds int64, localCache *freecache.Cache) RateLimitCache { +func NewRateLimitCacheImpl(pool Pool, perSecondPool Pool, timeSource limiter.TimeSource, jitterRand *rand.Rand, expirationJitterMaxSeconds int64, localCache *freecache.Cache) limiter.RateLimitCache { return &rateLimitCacheImpl{ pool: pool, perSecondPool: perSecondPool, timeSource: timeSource, jitterRand: jitterRand, expirationJitterMaxSeconds: expirationJitterMaxSeconds, - bufferPool: newBufferPool(), + cacheKeyGenerator: limiter.NewCacheKeyGenerator(), localCache: localCache, } } -func newBufferPool() sync.Pool { - return sync.Pool{ - New: func() interface{} { - return new(bytes.Buffer) - }, +func NewRateLimiterCacheImplFromSettings(s settings.Settings, localCache *freecache.Cache, srv server.Server, timeSource limiter.TimeSource, jitterRand *rand.Rand, expirationJitterMaxSeconds int64) limiter.RateLimitCache { + var perSecondPool Pool + if s.RedisPerSecond { + perSecondPool = NewPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondTls, s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize) } -} - -type timeSourceImpl struct{} - -func NewTimeSourceImpl() TimeSource { - return &timeSourceImpl{} -} - -func (this *timeSourceImpl) UnixNow() int64 { - return time.Now().Unix() -} - -// rand for jitter. -type lockedSource struct { - lk sync.Mutex - src rand.Source -} - -func NewLockedSource(seed int64) JitterRandSource { - return &lockedSource{src: rand.NewSource(seed)} -} - -func (r *lockedSource) Int63() (n int64) { - r.lk.Lock() - n = r.src.Int63() - r.lk.Unlock() - return -} - -func (r *lockedSource) Seed(seed int64) { - r.lk.Lock() - r.src.Seed(seed) - r.lk.Unlock() + var otherPool Pool + otherPool = NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisTls, s.RedisAuth, s.RedisUrl, s.RedisPoolSize) + + return NewRateLimitCacheImpl( + otherPool, + perSecondPool, + timeSource, + jitterRand, + expirationJitterMaxSeconds, + localCache) } diff --git a/src/server/server_impl.go b/src/server/server_impl.go index 2b27f06cd..9793528ca 100644 --- a/src/server/server_impl.go +++ b/src/server/server_impl.go @@ -8,8 +8,6 @@ import ( "net/http/pprof" "sort" - "github.com/envoyproxy/ratelimit/src/redis" - "os" "os/signal" "syscall" @@ -17,6 +15,7 @@ import ( "net" "github.com/coocood/freecache" + "github.com/envoyproxy/ratelimit/src/limiter" "github.com/envoyproxy/ratelimit/src/settings" "github.com/gorilla/mux" reuseport "github.com/kavu/go_reuseport" @@ -126,7 +125,7 @@ func newServer(name string, store stats.Store, localCache *freecache.Cache, opts ret.scope = ret.store.Scope(name) ret.store.AddStatGenerator(stats.NewRuntimeStats(ret.scope.Scope("go"))) if localCache != nil { - ret.store.AddStatGenerator(redis.NewLocalCacheStats(localCache, ret.scope.Scope("localcache"))) + ret.store.AddStatGenerator(limiter.NewLocalCacheStats(localCache, ret.scope.Scope("localcache"))) } // setup runtime diff --git a/src/service/ratelimit.go b/src/service/ratelimit.go index 3982a39ed..07a8c3132 100644 --- a/src/service/ratelimit.go +++ b/src/service/ratelimit.go @@ -7,9 +7,10 @@ import ( pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" "github.com/envoyproxy/ratelimit/src/assert" "github.com/envoyproxy/ratelimit/src/config" + "github.com/envoyproxy/ratelimit/src/limiter" "github.com/envoyproxy/ratelimit/src/redis" "github.com/lyft/goruntime/loader" - "github.com/lyft/gostats" + stats "github.com/lyft/gostats" logger "github.com/sirupsen/logrus" "golang.org/x/net/context" ) @@ -52,7 +53,7 @@ type service struct { configLoader config.RateLimitConfigLoader config config.RateLimitConfig runtimeUpdateEvent chan int - cache redis.RateLimitCache + cache limiter.RateLimitCache stats serviceStats rlStatsScope stats.Scope legacy *legacyService @@ -174,7 +175,7 @@ func (this *service) GetCurrentConfig() config.RateLimitConfig { return this.config } -func NewService(runtime loader.IFace, cache redis.RateLimitCache, +func NewService(runtime loader.IFace, cache limiter.RateLimitCache, configLoader config.RateLimitConfigLoader, stats stats.Scope) RateLimitServiceServer { newService := &service{ diff --git a/src/service_cmd/runner/runner.go b/src/service_cmd/runner/runner.go index ee9da4b39..e24529b3e 100644 --- a/src/service_cmd/runner/runner.go +++ b/src/service_cmd/runner/runner.go @@ -14,6 +14,7 @@ import ( pb_legacy "github.com/envoyproxy/ratelimit/proto/ratelimit" "github.com/envoyproxy/ratelimit/src/config" + "github.com/envoyproxy/ratelimit/src/limiter" "github.com/envoyproxy/ratelimit/src/redis" "github.com/envoyproxy/ratelimit/src/server" ratelimit "github.com/envoyproxy/ratelimit/src/service" @@ -49,22 +50,15 @@ func (runner *Runner) Run() { srv := server.NewServer("ratelimit", runner.statsStore, localCache, settings.GrpcUnaryInterceptor(nil)) - var perSecondPool redis.Pool - if s.RedisPerSecond { - perSecondPool = redis.NewPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondTls, s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize) - } - var otherPool redis.Pool - otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisTls, s.RedisAuth, s.RedisUrl, s.RedisPoolSize) + timeSource := limiter.NewTimeSourceImpl() + jitterRand := rand.New(limiter.NewLockedSource(time.Now().Unix())) + expirationJitterMaxSeconds := s.ExpirationJitterMaxSeconds + + cache := redis.NewRateLimiterCacheImplFromSettings(s, localCache, srv, timeSource, jitterRand, expirationJitterMaxSeconds) service := ratelimit.NewService( srv.Runtime(), - redis.NewRateLimitCacheImpl( - otherPool, - perSecondPool, - redis.NewTimeSourceImpl(), - rand.New(redis.NewLockedSource(time.Now().Unix())), - s.ExpirationJitterMaxSeconds, - localCache), + cache, config.NewRateLimitConfigLoaderImpl(), srv.Scope().Scope("service")) diff --git a/test/mocks/config/config.go b/test/mocks/config/config.go index e4ddc17b8..044b55ec9 100644 --- a/test/mocks/config/config.go +++ b/test/mocks/config/config.go @@ -9,7 +9,7 @@ import ( ratelimit "github.com/envoyproxy/go-control-plane/envoy/api/v2/ratelimit" config "github.com/envoyproxy/ratelimit/src/config" gomock "github.com/golang/mock/gomock" - gostats "github.com/lyft/gostats" + stats "github.com/lyft/gostats" reflect "reflect" ) @@ -38,6 +38,7 @@ func (m *MockRateLimitConfig) EXPECT() *MockRateLimitConfigMockRecorder { // Dump mocks base method func (m *MockRateLimitConfig) Dump() string { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Dump") ret0, _ := ret[0].(string) return ret0 @@ -45,11 +46,13 @@ func (m *MockRateLimitConfig) Dump() string { // Dump indicates an expected call of Dump func (mr *MockRateLimitConfigMockRecorder) Dump() *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Dump", reflect.TypeOf((*MockRateLimitConfig)(nil).Dump)) } // GetLimit mocks base method func (m *MockRateLimitConfig) GetLimit(arg0 context.Context, arg1 string, arg2 *ratelimit.RateLimitDescriptor) *config.RateLimit { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetLimit", arg0, arg1, arg2) ret0, _ := ret[0].(*config.RateLimit) return ret0 @@ -57,6 +60,7 @@ func (m *MockRateLimitConfig) GetLimit(arg0 context.Context, arg1 string, arg2 * // GetLimit indicates an expected call of GetLimit func (mr *MockRateLimitConfigMockRecorder) GetLimit(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLimit", reflect.TypeOf((*MockRateLimitConfig)(nil).GetLimit), arg0, arg1, arg2) } @@ -84,7 +88,8 @@ func (m *MockRateLimitConfigLoader) EXPECT() *MockRateLimitConfigLoaderMockRecor } // Load mocks base method -func (m *MockRateLimitConfigLoader) Load(arg0 []config.RateLimitConfigToLoad, arg1 gostats.Scope) config.RateLimitConfig { +func (m *MockRateLimitConfigLoader) Load(arg0 []config.RateLimitConfigToLoad, arg1 stats.Scope) config.RateLimitConfig { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Load", arg0, arg1) ret0, _ := ret[0].(config.RateLimitConfig) return ret0 @@ -92,5 +97,6 @@ func (m *MockRateLimitConfigLoader) Load(arg0 []config.RateLimitConfigToLoad, ar // Load indicates an expected call of Load func (mr *MockRateLimitConfigLoaderMockRecorder) Load(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Load", reflect.TypeOf((*MockRateLimitConfigLoader)(nil).Load), arg0, arg1) } diff --git a/test/mocks/limiter/limiter.go b/test/mocks/limiter/limiter.go new file mode 100644 index 000000000..f5c9f8bfa --- /dev/null +++ b/test/mocks/limiter/limiter.go @@ -0,0 +1,136 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/envoyproxy/ratelimit/src/limiter (interfaces: RateLimitCache,TimeSource,JitterRandSource) + +// Package mock_limiter is a generated GoMock package. +package mock_limiter + +import ( + context "context" + v2 "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" + config "github.com/envoyproxy/ratelimit/src/config" + gomock "github.com/golang/mock/gomock" + reflect "reflect" +) + +// MockRateLimitCache is a mock of RateLimitCache interface +type MockRateLimitCache struct { + ctrl *gomock.Controller + recorder *MockRateLimitCacheMockRecorder +} + +// MockRateLimitCacheMockRecorder is the mock recorder for MockRateLimitCache +type MockRateLimitCacheMockRecorder struct { + mock *MockRateLimitCache +} + +// NewMockRateLimitCache creates a new mock instance +func NewMockRateLimitCache(ctrl *gomock.Controller) *MockRateLimitCache { + mock := &MockRateLimitCache{ctrl: ctrl} + mock.recorder = &MockRateLimitCacheMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockRateLimitCache) EXPECT() *MockRateLimitCacheMockRecorder { + return m.recorder +} + +// DoLimit mocks base method +func (m *MockRateLimitCache) DoLimit(arg0 context.Context, arg1 *v2.RateLimitRequest, arg2 []*config.RateLimit) []*v2.RateLimitResponse_DescriptorStatus { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DoLimit", arg0, arg1, arg2) + ret0, _ := ret[0].([]*v2.RateLimitResponse_DescriptorStatus) + return ret0 +} + +// DoLimit indicates an expected call of DoLimit +func (mr *MockRateLimitCacheMockRecorder) DoLimit(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoLimit", reflect.TypeOf((*MockRateLimitCache)(nil).DoLimit), arg0, arg1, arg2) +} + +// MockTimeSource is a mock of TimeSource interface +type MockTimeSource struct { + ctrl *gomock.Controller + recorder *MockTimeSourceMockRecorder +} + +// MockTimeSourceMockRecorder is the mock recorder for MockTimeSource +type MockTimeSourceMockRecorder struct { + mock *MockTimeSource +} + +// NewMockTimeSource creates a new mock instance +func NewMockTimeSource(ctrl *gomock.Controller) *MockTimeSource { + mock := &MockTimeSource{ctrl: ctrl} + mock.recorder = &MockTimeSourceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockTimeSource) EXPECT() *MockTimeSourceMockRecorder { + return m.recorder +} + +// UnixNow mocks base method +func (m *MockTimeSource) UnixNow() int64 { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UnixNow") + ret0, _ := ret[0].(int64) + return ret0 +} + +// UnixNow indicates an expected call of UnixNow +func (mr *MockTimeSourceMockRecorder) UnixNow() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UnixNow", reflect.TypeOf((*MockTimeSource)(nil).UnixNow)) +} + +// MockJitterRandSource is a mock of JitterRandSource interface +type MockJitterRandSource struct { + ctrl *gomock.Controller + recorder *MockJitterRandSourceMockRecorder +} + +// MockJitterRandSourceMockRecorder is the mock recorder for MockJitterRandSource +type MockJitterRandSourceMockRecorder struct { + mock *MockJitterRandSource +} + +// NewMockJitterRandSource creates a new mock instance +func NewMockJitterRandSource(ctrl *gomock.Controller) *MockJitterRandSource { + mock := &MockJitterRandSource{ctrl: ctrl} + mock.recorder = &MockJitterRandSourceMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockJitterRandSource) EXPECT() *MockJitterRandSourceMockRecorder { + return m.recorder +} + +// Int63 mocks base method +func (m *MockJitterRandSource) Int63() int64 { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Int63") + ret0, _ := ret[0].(int64) + return ret0 +} + +// Int63 indicates an expected call of Int63 +func (mr *MockJitterRandSourceMockRecorder) Int63() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Int63", reflect.TypeOf((*MockJitterRandSource)(nil).Int63)) +} + +// Seed mocks base method +func (m *MockJitterRandSource) Seed(arg0 int64) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Seed", arg0) +} + +// Seed indicates an expected call of Seed +func (mr *MockJitterRandSourceMockRecorder) Seed(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Seed", reflect.TypeOf((*MockJitterRandSource)(nil).Seed), arg0) +} diff --git a/test/mocks/mocks.go b/test/mocks/mocks.go index efe80f841..6be37e81c 100644 --- a/test/mocks/mocks.go +++ b/test/mocks/mocks.go @@ -3,4 +3,5 @@ package mocks //go:generate mockgen -destination ./runtime/snapshot/snapshot.go github.com/lyft/goruntime/snapshot IFace //go:generate mockgen -destination ./runtime/loader/loader.go github.com/lyft/goruntime/loader IFace //go:generate mockgen -destination ./config/config.go github.com/envoyproxy/ratelimit/src/config RateLimitConfig,RateLimitConfigLoader -//go:generate mockgen -destination ./redis/redis.go github.com/envoyproxy/ratelimit/src/redis RateLimitCache,Pool,Connection,Response,TimeSource,JitterRandSource +//go:generate mockgen -destination ./redis/redis.go github.com/envoyproxy/ratelimit/src/redis Pool,Connection,Response +//go:generate mockgen -destination ./limiter/limiter.go github.com/envoyproxy/ratelimit/src/limiter RateLimitCache,TimeSource,JitterRandSource diff --git a/test/mocks/redis/redis.go b/test/mocks/redis/redis.go index df24212a2..d12ff0704 100644 --- a/test/mocks/redis/redis.go +++ b/test/mocks/redis/redis.go @@ -1,227 +1,151 @@ -// Automatically generated by MockGen. DO NOT EDIT! -// Source: github.com/envoyproxy/ratelimit/src/redis (interfaces: RateLimitCache,Pool,Connection,Response,TimeSource,JitterRandSource) +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/envoyproxy/ratelimit/src/redis (interfaces: Pool,Connection,Response) +// Package mock_redis is a generated GoMock package. package mock_redis import ( - ratelimit "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" - config "github.com/envoyproxy/ratelimit/src/config" redis "github.com/envoyproxy/ratelimit/src/redis" gomock "github.com/golang/mock/gomock" - context "golang.org/x/net/context" + reflect "reflect" ) -// Mock of RateLimitCache interface -type MockRateLimitCache struct { - ctrl *gomock.Controller - recorder *_MockRateLimitCacheRecorder -} - -// Recorder for MockRateLimitCache (not exported) -type _MockRateLimitCacheRecorder struct { - mock *MockRateLimitCache -} - -func NewMockRateLimitCache(ctrl *gomock.Controller) *MockRateLimitCache { - mock := &MockRateLimitCache{ctrl: ctrl} - mock.recorder = &_MockRateLimitCacheRecorder{mock} - return mock -} - -func (_m *MockRateLimitCache) EXPECT() *_MockRateLimitCacheRecorder { - return _m.recorder -} - -func (_m *MockRateLimitCache) DoLimit(_param0 context.Context, _param1 *ratelimit.RateLimitRequest, _param2 []*config.RateLimit) []*ratelimit.RateLimitResponse_DescriptorStatus { - ret := _m.ctrl.Call(_m, "DoLimit", _param0, _param1, _param2) - ret0, _ := ret[0].([]*ratelimit.RateLimitResponse_DescriptorStatus) - return ret0 -} - -func (_mr *_MockRateLimitCacheRecorder) DoLimit(arg0, arg1, arg2 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "DoLimit", arg0, arg1, arg2) -} - -// Mock of Pool interface +// MockPool is a mock of Pool interface type MockPool struct { ctrl *gomock.Controller - recorder *_MockPoolRecorder + recorder *MockPoolMockRecorder } -// Recorder for MockPool (not exported) -type _MockPoolRecorder struct { +// MockPoolMockRecorder is the mock recorder for MockPool +type MockPoolMockRecorder struct { mock *MockPool } +// NewMockPool creates a new mock instance func NewMockPool(ctrl *gomock.Controller) *MockPool { mock := &MockPool{ctrl: ctrl} - mock.recorder = &_MockPoolRecorder{mock} + mock.recorder = &MockPoolMockRecorder{mock} return mock } -func (_m *MockPool) EXPECT() *_MockPoolRecorder { - return _m.recorder +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockPool) EXPECT() *MockPoolMockRecorder { + return m.recorder } -func (_m *MockPool) Get() redis.Connection { - ret := _m.ctrl.Call(_m, "Get") +// Get mocks base method +func (m *MockPool) Get() redis.Connection { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Get") ret0, _ := ret[0].(redis.Connection) return ret0 } -func (_mr *_MockPoolRecorder) Get() *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "Get") +// Get indicates an expected call of Get +func (mr *MockPoolMockRecorder) Get() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockPool)(nil).Get)) } -func (_m *MockPool) Put(_param0 redis.Connection) { - _m.ctrl.Call(_m, "Put", _param0) +// Put mocks base method +func (m *MockPool) Put(arg0 redis.Connection) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Put", arg0) } -func (_mr *_MockPoolRecorder) Put(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "Put", arg0) +// Put indicates an expected call of Put +func (mr *MockPoolMockRecorder) Put(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Put", reflect.TypeOf((*MockPool)(nil).Put), arg0) } -// Mock of Connection interface +// MockConnection is a mock of Connection interface type MockConnection struct { ctrl *gomock.Controller - recorder *_MockConnectionRecorder + recorder *MockConnectionMockRecorder } -// Recorder for MockConnection (not exported) -type _MockConnectionRecorder struct { +// MockConnectionMockRecorder is the mock recorder for MockConnection +type MockConnectionMockRecorder struct { mock *MockConnection } +// NewMockConnection creates a new mock instance func NewMockConnection(ctrl *gomock.Controller) *MockConnection { mock := &MockConnection{ctrl: ctrl} - mock.recorder = &_MockConnectionRecorder{mock} + mock.recorder = &MockConnectionMockRecorder{mock} return mock } -func (_m *MockConnection) EXPECT() *_MockConnectionRecorder { - return _m.recorder +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockConnection) EXPECT() *MockConnectionMockRecorder { + return m.recorder } -func (_m *MockConnection) PipeAppend(_param0 string, _param1 ...interface{}) { - _s := []interface{}{_param0} - for _, _x := range _param1 { - _s = append(_s, _x) +// PipeAppend mocks base method +func (m *MockConnection) PipeAppend(arg0 string, arg1 ...interface{}) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0} + for _, a := range arg1 { + varargs = append(varargs, a) } - _m.ctrl.Call(_m, "PipeAppend", _s...) + m.ctrl.Call(m, "PipeAppend", varargs...) } -func (_mr *_MockConnectionRecorder) PipeAppend(arg0 interface{}, arg1 ...interface{}) *gomock.Call { - _s := append([]interface{}{arg0}, arg1...) - return _mr.mock.ctrl.RecordCall(_mr.mock, "PipeAppend", _s...) +// PipeAppend indicates an expected call of PipeAppend +func (mr *MockConnectionMockRecorder) PipeAppend(arg0 interface{}, arg1 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0}, arg1...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PipeAppend", reflect.TypeOf((*MockConnection)(nil).PipeAppend), varargs...) } -func (_m *MockConnection) PipeResponse() redis.Response { - ret := _m.ctrl.Call(_m, "PipeResponse") +// PipeResponse mocks base method +func (m *MockConnection) PipeResponse() redis.Response { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PipeResponse") ret0, _ := ret[0].(redis.Response) return ret0 } -func (_mr *_MockConnectionRecorder) PipeResponse() *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "PipeResponse") +// PipeResponse indicates an expected call of PipeResponse +func (mr *MockConnectionMockRecorder) PipeResponse() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PipeResponse", reflect.TypeOf((*MockConnection)(nil).PipeResponse)) } -// Mock of Response interface +// MockResponse is a mock of Response interface type MockResponse struct { ctrl *gomock.Controller - recorder *_MockResponseRecorder + recorder *MockResponseMockRecorder } -// Recorder for MockResponse (not exported) -type _MockResponseRecorder struct { +// MockResponseMockRecorder is the mock recorder for MockResponse +type MockResponseMockRecorder struct { mock *MockResponse } +// NewMockResponse creates a new mock instance func NewMockResponse(ctrl *gomock.Controller) *MockResponse { mock := &MockResponse{ctrl: ctrl} - mock.recorder = &_MockResponseRecorder{mock} + mock.recorder = &MockResponseMockRecorder{mock} return mock } -func (_m *MockResponse) EXPECT() *_MockResponseRecorder { - return _m.recorder +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockResponse) EXPECT() *MockResponseMockRecorder { + return m.recorder } -func (_m *MockResponse) Int() int64 { - ret := _m.ctrl.Call(_m, "Int") +// Int mocks base method +func (m *MockResponse) Int() int64 { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Int") ret0, _ := ret[0].(int64) return ret0 } -func (_mr *_MockResponseRecorder) Int() *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "Int") -} - -// Mock of TimeSource interface -type MockTimeSource struct { - ctrl *gomock.Controller - recorder *_MockTimeSourceRecorder -} - -// Recorder for MockTimeSource (not exported) -type _MockTimeSourceRecorder struct { - mock *MockTimeSource -} - -func NewMockTimeSource(ctrl *gomock.Controller) *MockTimeSource { - mock := &MockTimeSource{ctrl: ctrl} - mock.recorder = &_MockTimeSourceRecorder{mock} - return mock -} - -func (_m *MockTimeSource) EXPECT() *_MockTimeSourceRecorder { - return _m.recorder -} - -func (_m *MockTimeSource) UnixNow() int64 { - ret := _m.ctrl.Call(_m, "UnixNow") - ret0, _ := ret[0].(int64) - return ret0 -} - -func (_mr *_MockTimeSourceRecorder) UnixNow() *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "UnixNow") -} - -// Mock of JitterRandSource interface -type MockJitterRandSource struct { - ctrl *gomock.Controller - recorder *_MockJitterRandSourceRecorder -} - -// Recorder for MockJitterRandSource (not exported) -type _MockJitterRandSourceRecorder struct { - mock *MockJitterRandSource -} - -func NewMockJitterRandSource(ctrl *gomock.Controller) *MockJitterRandSource { - mock := &MockJitterRandSource{ctrl: ctrl} - mock.recorder = &_MockJitterRandSourceRecorder{mock} - return mock -} - -func (_m *MockJitterRandSource) EXPECT() *_MockJitterRandSourceRecorder { - return _m.recorder -} - -func (_m *MockJitterRandSource) Int63() int64 { - ret := _m.ctrl.Call(_m, "Int63") - ret0, _ := ret[0].(int64) - return ret0 -} - -func (_mr *_MockJitterRandSourceRecorder) Int63() *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "Int63") -} - -func (_m *MockJitterRandSource) Seed(_param0 int64) { - _m.ctrl.Call(_m, "Seed", _param0) -} - -func (_mr *_MockJitterRandSourceRecorder) Seed(arg0 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "Seed", arg0) +// Int indicates an expected call of Int +func (mr *MockResponseMockRecorder) Int() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Int", reflect.TypeOf((*MockResponse)(nil).Int)) } diff --git a/test/mocks/runtime/loader/loader.go b/test/mocks/runtime/loader/loader.go index 16bbc1099..da00c6498 100644 --- a/test/mocks/runtime/loader/loader.go +++ b/test/mocks/runtime/loader/loader.go @@ -35,16 +35,19 @@ func (m *MockIFace) EXPECT() *MockIFaceMockRecorder { // AddUpdateCallback mocks base method func (m *MockIFace) AddUpdateCallback(arg0 chan<- int) { + m.ctrl.T.Helper() m.ctrl.Call(m, "AddUpdateCallback", arg0) } // AddUpdateCallback indicates an expected call of AddUpdateCallback func (mr *MockIFaceMockRecorder) AddUpdateCallback(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddUpdateCallback", reflect.TypeOf((*MockIFace)(nil).AddUpdateCallback), arg0) } // Snapshot mocks base method func (m *MockIFace) Snapshot() snapshot.IFace { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Snapshot") ret0, _ := ret[0].(snapshot.IFace) return ret0 @@ -52,5 +55,6 @@ func (m *MockIFace) Snapshot() snapshot.IFace { // Snapshot indicates an expected call of Snapshot func (mr *MockIFaceMockRecorder) Snapshot() *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Snapshot", reflect.TypeOf((*MockIFace)(nil).Snapshot)) } diff --git a/test/mocks/runtime/snapshot/snapshot.go b/test/mocks/runtime/snapshot/snapshot.go index 432e34693..a56fe5a5b 100644 --- a/test/mocks/runtime/snapshot/snapshot.go +++ b/test/mocks/runtime/snapshot/snapshot.go @@ -36,6 +36,7 @@ func (m *MockIFace) EXPECT() *MockIFaceMockRecorder { // Entries mocks base method func (m *MockIFace) Entries() map[string]*entry.Entry { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Entries") ret0, _ := ret[0].(map[string]*entry.Entry) return ret0 @@ -43,11 +44,13 @@ func (m *MockIFace) Entries() map[string]*entry.Entry { // Entries indicates an expected call of Entries func (mr *MockIFaceMockRecorder) Entries() *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Entries", reflect.TypeOf((*MockIFace)(nil).Entries)) } // FeatureEnabled mocks base method func (m *MockIFace) FeatureEnabled(arg0 string, arg1 uint64) bool { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FeatureEnabled", arg0, arg1) ret0, _ := ret[0].(bool) return ret0 @@ -55,11 +58,13 @@ func (m *MockIFace) FeatureEnabled(arg0 string, arg1 uint64) bool { // FeatureEnabled indicates an expected call of FeatureEnabled func (mr *MockIFaceMockRecorder) FeatureEnabled(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FeatureEnabled", reflect.TypeOf((*MockIFace)(nil).FeatureEnabled), arg0, arg1) } // FeatureEnabledForID mocks base method func (m *MockIFace) FeatureEnabledForID(arg0 string, arg1 uint64, arg2 uint32) bool { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FeatureEnabledForID", arg0, arg1, arg2) ret0, _ := ret[0].(bool) return ret0 @@ -67,11 +72,13 @@ func (m *MockIFace) FeatureEnabledForID(arg0 string, arg1 uint64, arg2 uint32) b // FeatureEnabledForID indicates an expected call of FeatureEnabledForID func (mr *MockIFaceMockRecorder) FeatureEnabledForID(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FeatureEnabledForID", reflect.TypeOf((*MockIFace)(nil).FeatureEnabledForID), arg0, arg1, arg2) } // Get mocks base method func (m *MockIFace) Get(arg0 string) string { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Get", arg0) ret0, _ := ret[0].(string) return ret0 @@ -79,11 +86,13 @@ func (m *MockIFace) Get(arg0 string) string { // Get indicates an expected call of Get func (mr *MockIFaceMockRecorder) Get(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockIFace)(nil).Get), arg0) } // GetInteger mocks base method func (m *MockIFace) GetInteger(arg0 string, arg1 uint64) uint64 { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetInteger", arg0, arg1) ret0, _ := ret[0].(uint64) return ret0 @@ -91,11 +100,13 @@ func (m *MockIFace) GetInteger(arg0 string, arg1 uint64) uint64 { // GetInteger indicates an expected call of GetInteger func (mr *MockIFaceMockRecorder) GetInteger(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetInteger", reflect.TypeOf((*MockIFace)(nil).GetInteger), arg0, arg1) } // GetModified mocks base method func (m *MockIFace) GetModified(arg0 string) time.Time { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetModified", arg0) ret0, _ := ret[0].(time.Time) return ret0 @@ -103,11 +114,13 @@ func (m *MockIFace) GetModified(arg0 string) time.Time { // GetModified indicates an expected call of GetModified func (mr *MockIFaceMockRecorder) GetModified(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetModified", reflect.TypeOf((*MockIFace)(nil).GetModified), arg0) } // Keys mocks base method func (m *MockIFace) Keys() []string { + m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Keys") ret0, _ := ret[0].([]string) return ret0 @@ -115,15 +128,18 @@ func (m *MockIFace) Keys() []string { // Keys indicates an expected call of Keys func (mr *MockIFaceMockRecorder) Keys() *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Keys", reflect.TypeOf((*MockIFace)(nil).Keys)) } // SetEntry mocks base method func (m *MockIFace) SetEntry(arg0 string, arg1 *entry.Entry) { + m.ctrl.T.Helper() m.ctrl.Call(m, "SetEntry", arg0, arg1) } // SetEntry indicates an expected call of SetEntry func (mr *MockIFaceMockRecorder) SetEntry(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetEntry", reflect.TypeOf((*MockIFace)(nil).SetEntry), arg0, arg1) } diff --git a/test/redis/cache_impl_test.go b/test/redis/cache_impl_test.go index d4f94fc97..848f68e1b 100644 --- a/test/redis/cache_impl_test.go +++ b/test/redis/cache_impl_test.go @@ -7,12 +7,14 @@ import ( pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" "github.com/envoyproxy/ratelimit/src/config" + "github.com/envoyproxy/ratelimit/src/limiter" "github.com/envoyproxy/ratelimit/src/redis" stats "github.com/lyft/gostats" "math/rand" "github.com/envoyproxy/ratelimit/test/common" + mock_limiter "github.com/envoyproxy/ratelimit/test/mocks/limiter" mock_redis "github.com/envoyproxy/ratelimit/test/mocks/redis" "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" @@ -31,11 +33,11 @@ func testRedis(usePerSecondRedis bool) func(*testing.T) { pool := mock_redis.NewMockPool(controller) perSecondPool := mock_redis.NewMockPool(controller) - timeSource := mock_redis.NewMockTimeSource(controller) + timeSource := mock_limiter.NewMockTimeSource(controller) connection := mock_redis.NewMockConnection(controller) perSecondConnection := mock_redis.NewMockConnection(controller) response := mock_redis.NewMockResponse(controller) - var cache redis.RateLimitCache + var cache limiter.RateLimitCache if usePerSecondRedis { cache = redis.NewRateLimitCacheImpl(pool, perSecondPool, timeSource, rand.New(rand.NewSource(1)), 0, nil) } else { @@ -194,14 +196,14 @@ func TestOverLimitWithLocalCache(t *testing.T) { defer controller.Finish() pool := mock_redis.NewMockPool(controller) - timeSource := mock_redis.NewMockTimeSource(controller) + timeSource := mock_limiter.NewMockTimeSource(controller) connection := mock_redis.NewMockConnection(controller) response := mock_redis.NewMockResponse(controller) localCache := freecache.NewCache(100) cache := redis.NewRateLimitCacheImpl(pool, nil, timeSource, rand.New(rand.NewSource(1)), 0, localCache) sink := &common.TestStatSink{} statsStore := stats.NewStore(sink, true) - localCacheStats := redis.NewLocalCacheStats(localCache, statsStore.Scope("localcache")) + localCacheStats := limiter.NewLocalCacheStats(localCache, statsStore.Scope("localcache")) // Test Near Limit Stats. Under Near Limit Ratio pool.EXPECT().Get().Return(connection) @@ -305,7 +307,7 @@ func TestNearLimit(t *testing.T) { defer controller.Finish() pool := mock_redis.NewMockPool(controller) - timeSource := mock_redis.NewMockTimeSource(controller) + timeSource := mock_limiter.NewMockTimeSource(controller) connection := mock_redis.NewMockConnection(controller) response := mock_redis.NewMockResponse(controller) cache := redis.NewRateLimitCacheImpl(pool, nil, timeSource, rand.New(rand.NewSource(1)), 0, nil) @@ -502,10 +504,10 @@ func TestRedisWithJitter(t *testing.T) { defer controller.Finish() pool := mock_redis.NewMockPool(controller) - timeSource := mock_redis.NewMockTimeSource(controller) + timeSource := mock_limiter.NewMockTimeSource(controller) connection := mock_redis.NewMockConnection(controller) response := mock_redis.NewMockResponse(controller) - jitterSource := mock_redis.NewMockJitterRandSource(controller) + jitterSource := mock_limiter.NewMockJitterRandSource(controller) cache := redis.NewRateLimitCacheImpl(pool, nil, timeSource, rand.New(jitterSource), 3600, nil) statsStore := stats.NewStore(stats.NewNullSink(), false) diff --git a/test/service/ratelimit_test.go b/test/service/ratelimit_test.go index 57fa0a65d..c51bc7984 100644 --- a/test/service/ratelimit_test.go +++ b/test/service/ratelimit_test.go @@ -7,14 +7,14 @@ import ( pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2" "github.com/envoyproxy/ratelimit/src/config" "github.com/envoyproxy/ratelimit/src/redis" - "github.com/envoyproxy/ratelimit/src/service" + ratelimit "github.com/envoyproxy/ratelimit/src/service" "github.com/envoyproxy/ratelimit/test/common" - "github.com/envoyproxy/ratelimit/test/mocks/config" - "github.com/envoyproxy/ratelimit/test/mocks/redis" - "github.com/envoyproxy/ratelimit/test/mocks/runtime/loader" - "github.com/envoyproxy/ratelimit/test/mocks/runtime/snapshot" + mock_config "github.com/envoyproxy/ratelimit/test/mocks/config" + mock_limiter "github.com/envoyproxy/ratelimit/test/mocks/limiter" + mock_loader "github.com/envoyproxy/ratelimit/test/mocks/runtime/loader" + mock_snapshot "github.com/envoyproxy/ratelimit/test/mocks/runtime/snapshot" "github.com/golang/mock/gomock" - "github.com/lyft/gostats" + stats "github.com/lyft/gostats" "github.com/stretchr/testify/assert" "golang.org/x/net/context" ) @@ -51,7 +51,7 @@ type rateLimitServiceTestSuite struct { controller *gomock.Controller runtime *mock_loader.MockIFace snapshot *mock_snapshot.MockIFace - cache *mock_redis.MockRateLimitCache + cache *mock_limiter.MockRateLimitCache configLoader *mock_config.MockRateLimitConfigLoader config *mock_config.MockRateLimitConfig runtimeUpdateCallback chan<- int @@ -64,7 +64,7 @@ func commonSetup(t *testing.T) rateLimitServiceTestSuite { ret.controller = gomock.NewController(t) ret.runtime = mock_loader.NewMockIFace(ret.controller) ret.snapshot = mock_snapshot.NewMockIFace(ret.controller) - ret.cache = mock_redis.NewMockRateLimitCache(ret.controller) + ret.cache = mock_limiter.NewMockRateLimitCache(ret.controller) ret.configLoader = mock_config.NewMockRateLimitConfigLoader(ret.controller) ret.config = mock_config.NewMockRateLimitConfig(ret.controller) ret.statStore = stats.NewStore(stats.NewNullSink(), false)