# fixedWindowRateLimiter

> **fixedWindowRateLimiter**(`options`): [`RateMeter`](/reference/knotkit/rate-limit/interfaces/ratemeter/)

Defined in: [packages/rate-limit/src/fixed-window.ts:58](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/fixed-window.ts#L58)

Create a [RateMeter](/reference/knotkit/rate-limit/interfaces/ratemeter/) that counts weight in fixed windows anchored at clock zero.

Windows sit on one grid, `windowIndex = Math.floor(now / windowDuration)`, so every client’s allowance resets at the same instant and the whole state is that index plus the weight spent in it. The promise is exactly what that grid can keep: a request is admitted whenever the window it lands in has room, so a client can be admitted up to `2 × capacity` within one trailing window of that width — a full capacity on each side of a boundary — while every window of the grid itself admits `capacity` and no more.

Choose it when the reset has to be a clock face everyone can predict — `capacity` per minute, per day — when the boundary burst is acceptable or cannot happen because traffic is paced, and when the memory per key has to stay at one index and one counter. Choose `slidingWindowLogRateLimiter` instead when the trailing window itself is the contract, and `slidingWindowCounterRateLimiter` when a smoothed estimate of it is enough for less memory than the log needs.

`peek` answers with the decision `take` would, and neither a `denied` nor a `never-fits` answer spends weight, so a caller can ask before it acts. `take` and `peek` read the injected clock, which the meter clamps at its highest reading, so a reading taken now is never below one this meter already saw.

`take`, `peek`, and `freshAt` run in O(1) time, and the meter holds O(1) state.

## Parameters

### options

[`RateLimiterOptions`](/reference/knotkit/rate-limit/interfaces/ratelimiteroptions/)

The capacity, the width of the anchored window, and the clock every reading comes from.

## Returns

[`RateMeter`](/reference/knotkit/rate-limit/interfaces/ratemeter/)

## Throws

With code `INVALID_CAPACITY` when `options.capacity` is not a positive safe integer, `INVALID_WINDOW_DURATION` when `options.windowDuration` is not one, or `PARAMETERS_TOO_LARGE` when three times `capacity x windowDuration` would stop being exact.

## Throws

With code `INVALID_WEIGHT` from `take` and `peek` when the weight is not a positive safe integer, and `INVALID_CLOCK_READING` from `take`, `peek`, and `freshAt` when the injected clock returns a number that is not finite.

## Example

```ts
const time = scriptedClock()
const meter = fixedWindowRateLimiter({ capacity: 2, windowDuration: 1_000, clock: time.clock })

meter.take(2) // { outcome: 'allowed' }
meter.take(1) // { outcome: 'denied', retryAfter: 1_000 }
time.advance(1_000)
meter.take(1) // { outcome: 'allowed' }
```
