ratelimit

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2019 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FakeProvider added in v0.3.0

type FakeProvider struct {
	LimitHook func(context.Context, string, string, string) (RateLimit, merry.Error)
	AllowHook func(context.Context, string, string, string) (bool, time.Duration, merry.Error)
	CloseHook func()

	LimitCalls []*ProviderLimitInvocation
	AllowCalls []*ProviderAllowInvocation
	CloseCalls []*ProviderCloseInvocation
}

FakeProvider is a mock implementation of Provider for testing. Use it in your tests as in this example:

package example

func TestWithProvider(t *testing.T) {
	f := &ratelimit.FakeProvider{
		LimitHook: func(ctx context.Context, actor string, action string, path string) (ident1 RateLimit, ident2 merry.Error) {
			// ensure parameters meet expections, signal errors using t, etc
			return
		},
	}

	// test code goes here ...

	// assert state of FakeLimit ...
	f.AssertLimitCalledOnce(t)
}

Create anonymous function implementations for only those interface methods that should be called in the code under test. This will force a panic if any unexpected calls are made to FakeLimit.

func NewFakeProviderDefaultError added in v0.3.0

func NewFakeProviderDefaultError(t ProviderTestingT) *FakeProvider

NewFakeProviderDefaultError returns an instance of FakeProvider with all hooks configured to call t.Error

func NewFakeProviderDefaultFatal added in v0.3.0

func NewFakeProviderDefaultFatal(t ProviderTestingT) *FakeProvider

NewFakeProviderDefaultFatal returns an instance of FakeProvider with all hooks configured to call t.Fatal

func NewFakeProviderDefaultPanic added in v0.3.0

func NewFakeProviderDefaultPanic() *FakeProvider

NewFakeProviderDefaultPanic returns an instance of FakeProvider with all hooks configured to panic

func (*FakeProvider) Allow added in v0.3.0

func (_f11 *FakeProvider) Allow(ctx context.Context, actor string, action string, path string) (ident1 bool, ident2 time.Duration, ident3 merry.Error)

func (*FakeProvider) AllowCalled added in v0.3.0

func (f *FakeProvider) AllowCalled() bool

AllowCalled returns true if FakeProvider.Allow was called

func (*FakeProvider) AllowCalledN added in v0.3.0

func (f *FakeProvider) AllowCalledN(n int) bool

AllowCalledN returns true if FakeProvider.Allow was called at least n times

func (*FakeProvider) AllowCalledOnce added in v0.3.0

func (f *FakeProvider) AllowCalledOnce() bool

AllowCalledOnce returns true if FakeProvider.Allow was called exactly once

func (*FakeProvider) AllowCalledOnceWith added in v0.3.0

func (_f18 *FakeProvider) AllowCalledOnceWith(ctx context.Context, actor string, action string, path string) bool

AllowCalledOnceWith returns true if FakeProvider.Allow was called exactly once with the given values

func (*FakeProvider) AllowCalledWith added in v0.3.0

func (_f16 *FakeProvider) AllowCalledWith(ctx context.Context, actor string, action string, path string) (found bool)

AllowCalledWith returns true if FakeProvider.Allow was called with the given values

func (*FakeProvider) AllowNotCalled added in v0.3.0

func (f *FakeProvider) AllowNotCalled() bool

AllowNotCalled returns true if FakeProvider.Allow was not called

func (*FakeProvider) AllowResultsForCall added in v0.3.0

func (_f20 *FakeProvider) AllowResultsForCall(ctx context.Context, actor string, action string, path string) (ident1 bool, ident2 time.Duration, ident3 merry.Error, found bool)

AllowResultsForCall returns the result values for the first call to FakeProvider.Allow with the given values

func (*FakeProvider) AssertAllowCalled added in v0.3.0

func (f *FakeProvider) AssertAllowCalled(t ProviderTestingT)

AssertAllowCalled calls t.Error if FakeProvider.Allow was not called

func (*FakeProvider) AssertAllowCalledN added in v0.3.0

func (f *FakeProvider) AssertAllowCalledN(t ProviderTestingT, n int)

AssertAllowCalledN calls t.Error if FakeProvider.Allow was called less than n times

func (*FakeProvider) AssertAllowCalledOnce added in v0.3.0

func (f *FakeProvider) AssertAllowCalledOnce(t ProviderTestingT)

AssertAllowCalledOnce calls t.Error if FakeProvider.Allow was not called exactly once

func (*FakeProvider) AssertAllowCalledOnceWith added in v0.3.0

func (_f19 *FakeProvider) AssertAllowCalledOnceWith(t ProviderTestingT, ctx context.Context, actor string, action string, path string)

AssertAllowCalledOnceWith calls t.Error if FakeProvider.Allow was not called exactly once with the given values

func (*FakeProvider) AssertAllowCalledWith added in v0.3.0

func (_f17 *FakeProvider) AssertAllowCalledWith(t ProviderTestingT, ctx context.Context, actor string, action string, path string)

AssertAllowCalledWith calls t.Error if FakeProvider.Allow was not called with the given values

func (*FakeProvider) AssertAllowNotCalled added in v0.3.0

func (f *FakeProvider) AssertAllowNotCalled(t ProviderTestingT)

AssertAllowNotCalled calls t.Error if FakeProvider.Allow was called

func (*FakeProvider) AssertCloseCalled added in v0.3.0

func (f *FakeProvider) AssertCloseCalled(t ProviderTestingT)

AssertCloseCalled calls t.Error if FakeProvider.Close was not called

func (*FakeProvider) AssertCloseCalledN added in v0.3.0

func (f *FakeProvider) AssertCloseCalledN(t ProviderTestingT, n int)

AssertCloseCalledN calls t.Error if FakeProvider.Close was called less than n times

func (*FakeProvider) AssertCloseCalledOnce added in v0.3.0

func (f *FakeProvider) AssertCloseCalledOnce(t ProviderTestingT)

AssertCloseCalledOnce calls t.Error if FakeProvider.Close was not called exactly once

func (*FakeProvider) AssertCloseNotCalled added in v0.3.0

func (f *FakeProvider) AssertCloseNotCalled(t ProviderTestingT)

AssertCloseNotCalled calls t.Error if FakeProvider.Close was called

func (*FakeProvider) AssertLimitCalled added in v0.3.0

func (f *FakeProvider) AssertLimitCalled(t ProviderTestingT)

AssertLimitCalled calls t.Error if FakeProvider.Limit was not called

func (*FakeProvider) AssertLimitCalledN added in v0.3.0

func (f *FakeProvider) AssertLimitCalledN(t ProviderTestingT, n int)

AssertLimitCalledN calls t.Error if FakeProvider.Limit was called less than n times

func (*FakeProvider) AssertLimitCalledOnce added in v0.3.0

func (f *FakeProvider) AssertLimitCalledOnce(t ProviderTestingT)

AssertLimitCalledOnce calls t.Error if FakeProvider.Limit was not called exactly once

func (*FakeProvider) AssertLimitCalledOnceWith added in v0.3.0

func (_f9 *FakeProvider) AssertLimitCalledOnceWith(t ProviderTestingT, ctx context.Context, actor string, action string, path string)

AssertLimitCalledOnceWith calls t.Error if FakeProvider.Limit was not called exactly once with the given values

func (*FakeProvider) AssertLimitCalledWith added in v0.3.0

func (_f7 *FakeProvider) AssertLimitCalledWith(t ProviderTestingT, ctx context.Context, actor string, action string, path string)

AssertLimitCalledWith calls t.Error if FakeProvider.Limit was not called with the given values

func (*FakeProvider) AssertLimitNotCalled added in v0.3.0

func (f *FakeProvider) AssertLimitNotCalled(t ProviderTestingT)

AssertLimitNotCalled calls t.Error if FakeProvider.Limit was called

func (*FakeProvider) Close added in v0.3.0

func (_f21 *FakeProvider) Close()

func (*FakeProvider) CloseCalled added in v0.3.0

func (f *FakeProvider) CloseCalled() bool

CloseCalled returns true if FakeProvider.Close was called

func (*FakeProvider) CloseCalledN added in v0.3.0

func (f *FakeProvider) CloseCalledN(n int) bool

CloseCalledN returns true if FakeProvider.Close was called at least n times

func (*FakeProvider) CloseCalledOnce added in v0.3.0

func (f *FakeProvider) CloseCalledOnce() bool

CloseCalledOnce returns true if FakeProvider.Close was called exactly once

func (*FakeProvider) CloseNotCalled added in v0.3.0

func (f *FakeProvider) CloseNotCalled() bool

CloseNotCalled returns true if FakeProvider.Close was not called

func (*FakeProvider) Limit added in v0.3.0

func (_f1 *FakeProvider) Limit(ctx context.Context, actor string, action string, path string) (ident1 RateLimit, ident2 merry.Error)

func (*FakeProvider) LimitCalled added in v0.3.0

func (f *FakeProvider) LimitCalled() bool

LimitCalled returns true if FakeProvider.Limit was called

func (*FakeProvider) LimitCalledN added in v0.3.0

func (f *FakeProvider) LimitCalledN(n int) bool

LimitCalledN returns true if FakeProvider.Limit was called at least n times

func (*FakeProvider) LimitCalledOnce added in v0.3.0

func (f *FakeProvider) LimitCalledOnce() bool

LimitCalledOnce returns true if FakeProvider.Limit was called exactly once

func (*FakeProvider) LimitCalledOnceWith added in v0.3.0

func (_f8 *FakeProvider) LimitCalledOnceWith(ctx context.Context, actor string, action string, path string) bool

LimitCalledOnceWith returns true if FakeProvider.Limit was called exactly once with the given values

func (*FakeProvider) LimitCalledWith added in v0.3.0

func (_f6 *FakeProvider) LimitCalledWith(ctx context.Context, actor string, action string, path string) (found bool)

LimitCalledWith returns true if FakeProvider.Limit was called with the given values

func (*FakeProvider) LimitNotCalled added in v0.3.0

func (f *FakeProvider) LimitNotCalled() bool

LimitNotCalled returns true if FakeProvider.Limit was not called

func (*FakeProvider) LimitResultsForCall added in v0.3.0

func (_f10 *FakeProvider) LimitResultsForCall(ctx context.Context, actor string, action string, path string) (ident1 RateLimit, ident2 merry.Error, found bool)

LimitResultsForCall returns the result values for the first call to FakeProvider.Limit with the given values

func (*FakeProvider) Reset added in v0.3.0

func (f *FakeProvider) Reset()

func (*FakeProvider) SetAllowInvocation added in v0.3.1

func (_f13 *FakeProvider) SetAllowInvocation(calls_f14 []*ProviderAllowInvocation, fallback_f15 func() (bool, time.Duration, merry.Error))

SetAllowInvocation configures Provider.Allow to return the given results when called with the given parameters If no match is found for an invocation the result(s) of the fallback function are returned

func (*FakeProvider) SetAllowStub added in v0.3.1

func (_f12 *FakeProvider) SetAllowStub(ident1 bool, ident2 time.Duration, ident3 merry.Error)

SetAllowStub configures Provider.Allow to always return the given values

func (*FakeProvider) SetLimitInvocation added in v0.3.1

func (_f3 *FakeProvider) SetLimitInvocation(calls_f4 []*ProviderLimitInvocation, fallback_f5 func() (RateLimit, merry.Error))

SetLimitInvocation configures Provider.Limit to return the given results when called with the given parameters If no match is found for an invocation the result(s) of the fallback function are returned

func (*FakeProvider) SetLimitStub added in v0.3.1

func (_f2 *FakeProvider) SetLimitStub(ident1 RateLimit, ident2 merry.Error)

SetLimitStub configures Provider.Limit to always return the given values

type Provider

type Provider interface {
	// Limit returns the policy based rate limit for the given actor performing the action on the path.
	Limit(ctx context.Context, actor, action, path string) (RateLimit, merry.Error)
	// Allow returns true if the rate limit policy allows the given actor to perform the action on the path.
	// If the rate limit policy disallows the action, the cooldown duration is also returned. Allow only
	// returns an error due to internal failure.
	Allow(ctx context.Context, actor, action, path string) (bool, time.Duration, merry.Error)
	Close()
}

Provider is an interface providing a means to limiting requests based on actor/action/path parameters.

type ProviderAllowInvocation added in v0.3.0

type ProviderAllowInvocation struct {
	Parameters struct {
		Ctx    context.Context
		Actor  string
		Action string
		Path   string
	}
	Results struct {
		Ident1 bool
		Ident2 time.Duration
		Ident3 merry.Error
	}
}

ProviderAllowInvocation represents a single call of FakeProvider.Allow

func NewProviderAllowInvocation added in v0.3.1

func NewProviderAllowInvocation(ctx context.Context, actor string, action string, path string, ident1 bool, ident2 time.Duration, ident3 merry.Error) *ProviderAllowInvocation

NewProviderAllowInvocation creates a new instance of ProviderAllowInvocation

type ProviderCloseInvocation added in v0.3.0

type ProviderCloseInvocation struct {
}

ProviderCloseInvocation represents a single call of FakeProvider.Close

type ProviderLimitInvocation added in v0.3.0

type ProviderLimitInvocation struct {
	Parameters struct {
		Ctx    context.Context
		Actor  string
		Action string
		Path   string
	}
	Results struct {
		Ident1 RateLimit
		Ident2 merry.Error
	}
}

ProviderLimitInvocation represents a single call of FakeProvider.Limit

func NewProviderLimitInvocation added in v0.3.1

func NewProviderLimitInvocation(ctx context.Context, actor string, action string, path string, ident1 RateLimit, ident2 merry.Error) *ProviderLimitInvocation

NewProviderLimitInvocation creates a new instance of ProviderLimitInvocation

type ProviderTestingT added in v0.3.0

type ProviderTestingT interface {
	Error(...interface{})
	Errorf(string, ...interface{})
	Fatal(...interface{})
	Helper()
}

ProviderTestingT represents the methods of "testing".T used by charlatan Fakes. It avoids importing the testing package.

type RateLimit

type RateLimit struct {
	Limit  int
	Period time.Duration
}

RateLimit encodes a maximum number of repititions allowed over a time interval.

func FromString

func FromString(value string) (r RateLimit, merr merry.Error)

FromString parses a rate limit string in the form of "<limit>/<duration>".

func (RateLimit) String

func (r RateLimit) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL