https://github.com/vydd/cl-throttle
https://github.com/vydd/cl-throttle
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/vydd/cl-throttle
- Owner: vydd
- Created: 2015-11-20T03:06:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-28T13:54:05.000Z (over 10 years ago)
- Last Synced: 2025-06-21T09:43:25.476Z (about 1 year ago)
- Language: Common Lisp
- Size: 3.91 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# cl-throttle
This library introduces throttles to Common Lisp.
Throttles solve the problem of maintaining state for some types of branching code and keeping track of function calls when rate limiting is needed.
## Examples (WORK IN PROGRESS)
### `(limit n)`, `(once)`
Making the code runnable for only a `LIMIT`ed number of times.
```lisp
...
(case genie-command
...
(:grant-a-wish (with-throttle (limit 3)
(close-eyes)
(speak-magic-words)
"Wish granted!")))
...)
...
```
For convenience, throttle `ONCE` is provided as well.
```lisp
(defun-throttled (once) contract-chickenpox ()
(cough)
(sneeze)
(scratch))
```
You can use `INTERVAL` to limit the rate of code execution.
```lisp
(defun-throttled (interval 1000) login (user password)
(if (auth user password)
"Welcome!"
"Go away."))
```
It's possible to `THROTTLE-AND` throttles.
```lisp
(defun-throttled (throttle-and (interval 1000) (limit 3)) strict-login (user password)
(if (auth user password)
"Welcome!"
"Careful."))
```
And also `THROTTLE-OR`.
```lisp
; can be executed 3 times a day
(defun-throttled (throttle-or (interval 1000) (limit 3)) send-email (recipient)
(email recipient "Hello"))
```
## Values returned by `WITH-THROTTLE`
If you need to know if throttled code was executed or not, you can use the second value returned by it (the first value is the value returned by your code).
It's possible to reset throttles, by providing the third value returned by `WITH-THROTTLE` to `RESET-THROTTLE-STATE`. This functionality is meant to be used sparingly in user programs, if not at all, but can be useful when extending `CL-THROTTLE`.
## License
MIT