glimit

package module
v0.0.0-...-0b17b27 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2017 License: MIT Imports: 3 Imported by: 0

README

Build Status Coverage Status Go Report Card

glimit

A Go Rate Limiter backed by gorm

caveats

  • It doesn't utilize a monotonic clock, so hopefully your system clock is relatively sane.
  • There's not much keeping you from "losing" a limiter and accumulating garbage, for example if you associated it with user sessions, you may want to do some housekeeping yourself.
  • I need to add an interface to use stores other than gorm
  • It does not supply a blocking function, as that could could cause problems. If you want one, I suggest you implement one yourself in your application with the required precision.
  • After a fair amount of testing I'm fairly comfortable with it's behavior in high load situations. However this may depend on your database.

docs

  • The comments and godoc should be enough to get you started

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrRateLimitExceeded Returned when a rate limit is exceeded
	ErrRateLimitExceeded = errors.New("Rate Limit Exceeded")
	//ErrInvalidID occurs when a limiter with an invalid ID is used.
	ErrInvalidID = errors.New("Invalid ID")
)

Functions

func CleanupAll

func CleanupAll(db *gorm.DB) error

CleanupAll removes all expired actions from all limiters in the database. possibly a very expensive call.

func DoMigrations

func DoMigrations(db *gorm.DB)

DoMigrations adds the models to the database. Should be run once, if you choose to use automigrations at all.

Types

type Action

type Action struct {
	Timestamp time.Time `gorm:"index"`
	ID        uint
	LimiterID uint `gorm:"index"`
}

Action represents the Action being ratelimited.

type Limiter

type Limiter struct {
	ID       uint
	Times    int
	Interval time.Duration
	// contains filtered or unexported fields
}

Limiter represents the rate limiter

func ByID

func ByID(ID uint, db *gorm.DB) (*Limiter, error)

ByID retrieves a limiter from it's ID

func NewLimiter

func NewLimiter(actions int, interval time.Duration, db *gorm.DB) (*Limiter, error)

NewLimiter Creates a new limiter with the specified arguments and saves it to the database.

func (*Limiter) Cleanup

func (l *Limiter) Cleanup() error

Cleanup deletes all "expired" actions associated with a limiter

func (*Limiter) Delete

func (l *Limiter) Delete() error

Delete deletes the limiter as well as all associated actions

func (*Limiter) Save

func (l *Limiter) Save() error

Save allows you to update the attributes of a limiter. If you make any changes to a limiter, simply Save() it, and it will go back to the database

func (*Limiter) Take

func (l *Limiter) Take() (int, error)

Take attempts to do an "action". if there is an error determining if an action

   is possible or writing the action to the database, the returned action count
	 will be 0. If the rate is exceeded, the total count will be returned,
	 as well ass ErrRateLimitExceeded. If the Take is successful, it will return
	 the number of actions and a nil error.

Jump to

Keyboard shortcuts

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