> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man9/ratecheck.9.md).

# ratecheck.9

`ratecheck` — 事件速率限制

## 名称

`ratecheck`, `ppsratecheck`

## 概要

```c
#include <sys/time.h>
```

```c
int
ratecheck(struct timeval *lasttime, const struct timeval *mininterval)

int
ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps)
```

## 描述

`ratecheck` 和 `ppsratecheck` 函数便于对任意事件进行速率限制。前者强制事件之间的最小间隔，而后者强制每秒最大事件数。

`ratecheck` 函数将当前时间与 `lasttime` 所指向的值进行比较。如果差值等于或大于 `mininterval`，它返回非零值并将 `lasttime` 更新为当前时间。否则，它返回零。

`ppsratecheck` 函数首先将当前时间与 `lasttime` 进行比较。如果至少已过整整一秒，`curpps` 参数所指向的值将重置为 1，`lasttime` 更新为当前时间。否则，`curpps` 递增，`lasttime` 保持不变。在任一情况下，当且仅当更新后的 `curpps` 小于或等于 `maxpps` 或 `maxpps` 为负时，`ppsratecheck` 返回非零值。

## 参见

[counter(9)](/man/man9/counter.9.md)

## 历史

`ratecheck` 和 `ppsratecheck` 函数首次出现在 FreeBSD 5.1 中。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man9/ratecheck.9.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
