# RateMeter

Defined in: [packages/rate-limit/src/rate-limit.ts:79](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/rate-limit.ts#L79)

A rate-limit algorithm’s state for one key.

The same contract serves the two built-in-and-custom cases: a factory here builds one, and any object a caller supplies that meets these members works in a keyed table.

`take` and `peek` answer the same question and differ only in whether the weight is spent. Neither changes state on a denial.

## Properties

### kind

> `readonly` **kind**: `"custom"` | `"fixed-window"` | `"sliding-window-log"` | `"sliding-window-counter"` | `"token-bucket"`

Defined in: [packages/rate-limit/src/rate-limit.ts:81](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/rate-limit.ts#L81)

The algorithm this meter runs: a built-in kind, or `'custom'` for a caller-supplied one.

***

### capacity

> `readonly` **capacity**: `number`

Defined in: [packages/rate-limit/src/rate-limit.ts:86](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/rate-limit.ts#L86)

The most weight one instant can admit from a fresh state. It bounds a single request: a weight above it is `never-fits`, in every kind.

***

### take

> `readonly` **take**: (`weight`) => [`RateLimitDecision`](/reference/knotkit/rate-limit/type-aliases/ratelimitdecision/)

Defined in: [packages/rate-limit/src/rate-limit.ts:95](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/rate-limit.ts#L95)

Spend `weight` when the request is allowed, otherwise report why not.

#### Parameters

##### weight

`number`

#### Returns

[`RateLimitDecision`](/reference/knotkit/rate-limit/type-aliases/ratelimitdecision/)

#### Throws

With code `INVALID_WEIGHT` when `weight` is not a positive safe integer, or `INVALID_CLOCK_READING` when the injected clock returns a non-finite number. The meter is unchanged after either.

***

### peek

> `readonly` **peek**: (`weight`) => [`RateLimitDecision`](/reference/knotkit/rate-limit/type-aliases/ratelimitdecision/)

Defined in: [packages/rate-limit/src/rate-limit.ts:104](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/rate-limit.ts#L104)

Return the decision [take](/reference/knotkit/rate-limit/interfaces/ratemeter/#take) would return for `weight` without spending it.

#### Parameters

##### weight

`number`

#### Returns

[`RateLimitDecision`](/reference/knotkit/rate-limit/type-aliases/ratelimitdecision/)

#### Throws

With code `INVALID_WEIGHT` when `weight` is not a positive safe integer, or `INVALID_CLOCK_READING` when the injected clock returns a non-finite number.

***

### freshAt

> `readonly` **freshAt**: () => `number`

Defined in: [packages/rate-limit/src/rate-limit.ts:117](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/rate-limit/src/rate-limit.ts#L117)

The instant, in whole milliseconds of the injected clock, at which this meter’s state equals a fresh one’s, so dropping it cannot change a later decision.

It never decreases: a later call returns an instant at or after the one an earlier call returned. A keyed table relies on that, and keeps the instant it read when it created a key as a lower bound on every later one.

#### Returns

`number`

#### Throws

With code `INVALID_CLOCK_READING` when the injected clock returns a non-finite number.
