---
title: RateLimiter
summary: RateLimiter combines a RateCounter and a PenaltyBox to provide an easy way to check whether a given entry should be rate limited given a rate window and upper limit.
url: https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/struct.RateLimiter/
---

**Kind:** Struct  
**Module:** [erl](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/)  
**Package:** compute-sdk-go v1.11.0  
**Source:** https://github.com/fastly/compute-sdk-go/blob/d6bf71b04516ed72d6018cab6fff701a865f86e5/erl/erl.go#L207

```go
type RateLimiter struct {
	RateCounter *RateCounter
	PenaltyBox  *PenaltyBox
}
```

RateLimiter combines a [RateCounter](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/struct.RateCounter/) and a [PenaltyBox](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/struct.PenaltyBox/) to provide an
easy way to check whether a given entry should be rate limited given
a rate window and upper limit.

## Fields

### RateCounter

**Type:** `*RateCounter`

### PenaltyBox

**Type:** `*PenaltyBox`

## Methods

### CheckRate

**Source:** https://github.com/fastly/compute-sdk-go/blob/d6bf71b04516ed72d6018cab6fff701a865f86e5/erl/erl.go#L229

```go
func (erl *RateLimiter) CheckRate(entry string, delta uint32, policy *Policy) (bool, error)
```

CheckRate increments an entry's rate counter by the delta value
([RateCounter.Increment](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/struct.RateCounter/#method-Increment)), and checks it against the provided
[Policy](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/struct.Policy/).  If the count after increment exceeds the policy's MaxRate
over the RateWindow, it will add the entry to the penalty box for the
policy's PenaltyBoxDuration.  It returns true if the entry is in the
penalty box.

The limits for the delta value are the same as
[RateCounter.Increment](https://www.fastly.com/documentation/reference/compute/sdks/go/compute-sdk-go/erl/struct.RateCounter/#method-Increment).

**Parameters**

- `entry`: `string`
- `delta`: `uint32`
- `policy`: `*Policy`

**Returns**

`(bool, error)`

```go
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
	limiter := erl.NewRateLimiter(
		erl.OpenRateCounter("requests"),
		erl.OpenPenaltyBox("bad_ips"),
	)

	block, err := limiter.CheckRate(
		r.RemoteAddr,
		1,
		&erl.Policy{
			erl.RateWindow10s,
			100,
			time.Minute,
		},
	)
	if err != nil {

		log.Println("error during limit.CheckRate:", err)
		w.WriteHeader(fsthttp.StatusInternalServerError)
		return
	} else if block {

		w.WriteHeader(fsthttp.StatusTooManyRequests)
		return
	}

})
```
