@thi.ng/leaky-bucket 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.md +125 -0
- package/index.d.ts +139 -0
- package/index.js +142 -0
- package/package.json +83 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
- **Last updated**: 2025-03-09T19:21:53Z
|
|
4
|
+
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
|
+
|
|
6
|
+
All notable changes to this project will be documented in this file.
|
|
7
|
+
Only versions published since **2022-01-01** are listed here.
|
|
8
|
+
Please consult the Git history for older version information.
|
|
9
|
+
See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
|
|
10
|
+
|
|
11
|
+
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
12
|
+
and/or version bumps of transitive dependencies.
|
|
13
|
+
|
|
14
|
+
## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/leaky-bucket@0.1.0) (2025-03-09)
|
|
15
|
+
|
|
16
|
+
#### 🚀 Features
|
|
17
|
+
|
|
18
|
+
- import as new pkg ([bdfd5ed](https://github.com/thi-ng/umbrella/commit/bdfd5ed))
|
|
19
|
+
- add support for per-bucket capacities ([fddb130](https://github.com/thi-ng/umbrella/commit/fddb130))
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
|
+
<!-- Please see: https://github.com/thi-ng/umbrella/blob/develop/CONTRIBUTING.md#changes-to-readme-files -->
|
|
3
|
+
# 
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@thi.ng/leaky-bucket)
|
|
6
|
+

|
|
7
|
+
[](https://mastodon.thi.ng/@toxi)
|
|
8
|
+
|
|
9
|
+
> [!NOTE]
|
|
10
|
+
> This is one of 203 standalone projects, maintained as part
|
|
11
|
+
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
|
+
> and anti-framework.
|
|
13
|
+
>
|
|
14
|
+
> 🚀 Please help me to work full-time on these projects by [sponsoring me on
|
|
15
|
+
> GitHub](https://github.com/sponsors/postspectacular). Thank you! ❤️
|
|
16
|
+
|
|
17
|
+
- [About](#about)
|
|
18
|
+
- [Status](#status)
|
|
19
|
+
- [Installation](#installation)
|
|
20
|
+
- [Dependencies](#dependencies)
|
|
21
|
+
- [API](#api)
|
|
22
|
+
- [Authors](#authors)
|
|
23
|
+
- [License](#license)
|
|
24
|
+
|
|
25
|
+
## About
|
|
26
|
+
|
|
27
|
+
Configurable, counter-based Leaky Bucket abstractions.
|
|
28
|
+
|
|
29
|
+
Reference:
|
|
30
|
+
|
|
31
|
+
- https://en.wikipedia.org/wiki/Leaky_bucket
|
|
32
|
+
|
|
33
|
+
## Status
|
|
34
|
+
|
|
35
|
+
**ALPHA** - bleeding edge / work-in-progress
|
|
36
|
+
|
|
37
|
+
[Search or submit any issues for this package](https://github.com/thi-ng/umbrella/issues?q=%5Bleaky-bucket%5D+in%3Atitle)
|
|
38
|
+
|
|
39
|
+
## Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
yarn add @thi.ng/leaky-bucket
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
ESM import:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import * as lb from "@thi.ng/leaky-bucket";
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Browser ESM import:
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
<script type="module" src="https://esm.run/@thi.ng/leaky-bucket"></script>
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
[JSDelivr documentation](https://www.jsdelivr.com/)
|
|
58
|
+
|
|
59
|
+
For Node.js REPL:
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const lb = await import("@thi.ng/leaky-bucket");
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 591 bytes
|
|
66
|
+
|
|
67
|
+
## Dependencies
|
|
68
|
+
|
|
69
|
+
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
70
|
+
|
|
71
|
+
Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)
|
|
72
|
+
|
|
73
|
+
## API
|
|
74
|
+
|
|
75
|
+
[Generated API docs](https://docs.thi.ng/umbrella/leaky-bucket/)
|
|
76
|
+
|
|
77
|
+
```ts tangle:export/readme-1.ts
|
|
78
|
+
import { LeakyBucketMap } from "@thi.ng/leaky-bucket";
|
|
79
|
+
|
|
80
|
+
const buckets = new LeakyBucketMap({
|
|
81
|
+
maxBuckets: 2,
|
|
82
|
+
capacity: 3,
|
|
83
|
+
leakInterval: 1000,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
buckets.update("a") //true
|
|
87
|
+
buckets.update("a") //true
|
|
88
|
+
buckets.update("a") //true
|
|
89
|
+
|
|
90
|
+
// max capacity=3 reached
|
|
91
|
+
buckets.update("a"); // false
|
|
92
|
+
|
|
93
|
+
// another bucket
|
|
94
|
+
buckets.update("b"); // true
|
|
95
|
+
|
|
96
|
+
// max buckets=2 reached
|
|
97
|
+
buckets.update("c"); // false
|
|
98
|
+
|
|
99
|
+
// wait > 1000ms, buckets have leaked...
|
|
100
|
+
|
|
101
|
+
// bucket A has capacity again
|
|
102
|
+
buckets.update("a"); // true
|
|
103
|
+
|
|
104
|
+
// bucket B has been removed (since emtpy)
|
|
105
|
+
buckets.has("b"); // false
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Authors
|
|
109
|
+
|
|
110
|
+
- [Karsten Schmidt](https://thi.ng)
|
|
111
|
+
|
|
112
|
+
If this project contributes to an academic publication, please cite it as:
|
|
113
|
+
|
|
114
|
+
```bibtex
|
|
115
|
+
@misc{thing-leaky-bucket,
|
|
116
|
+
title = "@thi.ng/leaky-bucket",
|
|
117
|
+
author = "Karsten Schmidt",
|
|
118
|
+
note = "https://thi.ng/leaky-bucket",
|
|
119
|
+
year = 2025
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
© 2025 Karsten Schmidt // Apache License 2.0
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { Fn, IRelease, Maybe } from "@thi.ng/api";
|
|
2
|
+
/**
|
|
3
|
+
* Behavior options for a {@link LeakyBucket}. The default config provides an
|
|
4
|
+
* average rate of 1 Hz, burstable to 60Hz.
|
|
5
|
+
*/
|
|
6
|
+
export interface LeakyBucketOpts {
|
|
7
|
+
/**
|
|
8
|
+
* Counter value per bucket at which overflow occurs. The counter is
|
|
9
|
+
* increased each time {@link LeakyBucket.update} or
|
|
10
|
+
* {@link LeakyBucketMap.update} is called.
|
|
11
|
+
*
|
|
12
|
+
* @defaultValue 60
|
|
13
|
+
*/
|
|
14
|
+
capacity: number;
|
|
15
|
+
/**
|
|
16
|
+
* If true (default), the buckets will be automatically updated every
|
|
17
|
+
* {@link LeakyBucketOpts.leakInterval} milliseconds. If false, the user
|
|
18
|
+
* must regularly call {@link LeakyBucket.leak} or
|
|
19
|
+
* {@link LeakyBucketMap.leak}.
|
|
20
|
+
*
|
|
21
|
+
* @defaultValue true
|
|
22
|
+
*/
|
|
23
|
+
autoLeak: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Time (in milliseconds) at which all active buckets are leaking (i.e.
|
|
26
|
+
* decrementing their counter).
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* Even if {@link LeakyBucket.leak} {@link LeakyBucketMap.leak} is called
|
|
30
|
+
* directly (e.g. if {@link LeakyBucketOpts.autoLeak} is disabled), a bucket
|
|
31
|
+
* is only leaking after the given time has passed since its last update
|
|
32
|
+
* (via {@link LeakyBucketMap.update}) and since {@link LeakyBucketMap.leak}
|
|
33
|
+
* was called last.
|
|
34
|
+
*
|
|
35
|
+
* @defaultValue 1000
|
|
36
|
+
*/
|
|
37
|
+
leakInterval: number;
|
|
38
|
+
}
|
|
39
|
+
export interface LeakyBucketMapOpts<T> extends LeakyBucketOpts {
|
|
40
|
+
/**
|
|
41
|
+
* Max number of active buckets. Empty buckets are automatically removed
|
|
42
|
+
* (via {@link LeakyBucketMap.leak}).
|
|
43
|
+
*
|
|
44
|
+
* @defaultValue 1000
|
|
45
|
+
*/
|
|
46
|
+
maxBuckets: number;
|
|
47
|
+
/**
|
|
48
|
+
* Callback when a bucket leaked and becomes empty. The function receives
|
|
49
|
+
* the bucket's key as argument.
|
|
50
|
+
*/
|
|
51
|
+
onEmpty: Fn<T, void>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Groups multiple {@link LeakyBucket}s in an ES6-Map and centrally manages
|
|
55
|
+
* them. All buckets share the same config options given to the ctor of this
|
|
56
|
+
* class.
|
|
57
|
+
*
|
|
58
|
+
* @remarks
|
|
59
|
+
* Reference:
|
|
60
|
+
* https://en.wikipedia.org/wiki/Leaky_bucket
|
|
61
|
+
*/
|
|
62
|
+
export declare class LeakyBucketMap<K> implements IRelease {
|
|
63
|
+
opts: Partial<LeakyBucketMapOpts<K>>;
|
|
64
|
+
buckets: Map<K, LeakyBucket>;
|
|
65
|
+
maxBuckets: number;
|
|
66
|
+
leakInterval: number;
|
|
67
|
+
lastLeak: number;
|
|
68
|
+
timer: any;
|
|
69
|
+
constructor(opts?: Partial<LeakyBucketMapOpts<K>>);
|
|
70
|
+
has(key: K): boolean;
|
|
71
|
+
get(key: K): LeakyBucket | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Calls {@link LeakyBucket.update} for given bucket ID and returns its
|
|
74
|
+
* result. Creates a new bucket if no bucket yet exist. Returns true if the
|
|
75
|
+
* bucket still had capacity or false if capacity had already been reached.
|
|
76
|
+
*
|
|
77
|
+
* @remarks
|
|
78
|
+
* If `capacity` is given, it's used to define a custom per-bucket capacity,
|
|
79
|
+
* but will only be used for bucket creation, i.e. if no active bucket for
|
|
80
|
+
* the given `key` already exists. If no `capacity` is given, the bucket
|
|
81
|
+
* will use the capacity configured for this {@link LeakyBucketMap}.
|
|
82
|
+
*
|
|
83
|
+
* @param key
|
|
84
|
+
* @param capacity
|
|
85
|
+
*/
|
|
86
|
+
update(key: K, capacity?: number | undefined): boolean;
|
|
87
|
+
/**
|
|
88
|
+
* Leaks all bucket counters (taking into account current/given timestamp)
|
|
89
|
+
* and removes those which emptied. If {@link LeakyBucketMapOpts.onEmpty} is
|
|
90
|
+
* defined, also calls that function before removing the bucket.
|
|
91
|
+
*
|
|
92
|
+
* @remarks
|
|
93
|
+
* The function is a no-op if called within fewer than
|
|
94
|
+
* {@link LeakyBucketOpts.leakInterval} milliseconds since the last leak.
|
|
95
|
+
*/
|
|
96
|
+
leak(now?: number): void;
|
|
97
|
+
/**
|
|
98
|
+
* Cancels/removes leak interval timer (if any) and removes all buckets.
|
|
99
|
+
*/
|
|
100
|
+
release(): boolean;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A single counter-based leaky bucket with configurable capacity & leak
|
|
104
|
+
* interval/behavior. See {@link LeakyBucketOpts} for details.
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* Reference: https://en.wikipedia.org/wiki/Leaky_bucket
|
|
108
|
+
*/
|
|
109
|
+
export declare class LeakyBucket implements IRelease {
|
|
110
|
+
level: number;
|
|
111
|
+
capacity: number;
|
|
112
|
+
lastLeak: number;
|
|
113
|
+
leakInterval: number;
|
|
114
|
+
timer: any;
|
|
115
|
+
constructor(opts?: Partial<LeakyBucketOpts>, initialLevel?: number);
|
|
116
|
+
/**
|
|
117
|
+
* Updates bucket's counter/level and returns true if successful or false if
|
|
118
|
+
* capacity had already been reached.
|
|
119
|
+
*/
|
|
120
|
+
update(): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Leaks bucket's counter (taking into account current/given timestamp) and
|
|
123
|
+
* then returns one of the following values:
|
|
124
|
+
*
|
|
125
|
+
* - `undefined` if bucket is already empty or if this function was called
|
|
126
|
+
* within fewer than {@link LeakyBucketOpts.leakInterval} milliseconds
|
|
127
|
+
* since the last leak
|
|
128
|
+
* - `false` if bucket leaked and is now empty
|
|
129
|
+
* - `true` if bucket leaked and still non-empty
|
|
130
|
+
*
|
|
131
|
+
* @param now
|
|
132
|
+
*/
|
|
133
|
+
leak(now?: number): Maybe<boolean>;
|
|
134
|
+
/**
|
|
135
|
+
* Cancels/removes leak interval timer (if any).
|
|
136
|
+
*/
|
|
137
|
+
release(): boolean;
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
class LeakyBucketMap {
|
|
2
|
+
constructor(opts = {}) {
|
|
3
|
+
this.opts = opts;
|
|
4
|
+
this.buckets = /* @__PURE__ */ new Map();
|
|
5
|
+
this.maxBuckets = opts.maxBuckets ?? 1e3;
|
|
6
|
+
this.leakInterval = opts.leakInterval ?? 1e3;
|
|
7
|
+
if (opts.autoLeak !== false) {
|
|
8
|
+
this.timer = setInterval(this.leak.bind(this), this.leakInterval);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
buckets;
|
|
12
|
+
maxBuckets;
|
|
13
|
+
leakInterval;
|
|
14
|
+
lastLeak = -1;
|
|
15
|
+
timer;
|
|
16
|
+
has(key) {
|
|
17
|
+
return this.buckets.has(key);
|
|
18
|
+
}
|
|
19
|
+
get(key) {
|
|
20
|
+
return this.buckets.get(key);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Calls {@link LeakyBucket.update} for given bucket ID and returns its
|
|
24
|
+
* result. Creates a new bucket if no bucket yet exist. Returns true if the
|
|
25
|
+
* bucket still had capacity or false if capacity had already been reached.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* If `capacity` is given, it's used to define a custom per-bucket capacity,
|
|
29
|
+
* but will only be used for bucket creation, i.e. if no active bucket for
|
|
30
|
+
* the given `key` already exists. If no `capacity` is given, the bucket
|
|
31
|
+
* will use the capacity configured for this {@link LeakyBucketMap}.
|
|
32
|
+
*
|
|
33
|
+
* @param key
|
|
34
|
+
* @param capacity
|
|
35
|
+
*/
|
|
36
|
+
update(key, capacity = this.opts.capacity) {
|
|
37
|
+
const bucket = this.buckets.get(key);
|
|
38
|
+
if (bucket) {
|
|
39
|
+
return bucket.update();
|
|
40
|
+
} else {
|
|
41
|
+
if (this.buckets.size >= this.maxBuckets) return false;
|
|
42
|
+
this.buckets.set(
|
|
43
|
+
key,
|
|
44
|
+
new LeakyBucket({ ...this.opts, capacity, autoLeak: false }, 1)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Leaks all bucket counters (taking into account current/given timestamp)
|
|
51
|
+
* and removes those which emptied. If {@link LeakyBucketMapOpts.onEmpty} is
|
|
52
|
+
* defined, also calls that function before removing the bucket.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* The function is a no-op if called within fewer than
|
|
56
|
+
* {@link LeakyBucketOpts.leakInterval} milliseconds since the last leak.
|
|
57
|
+
*/
|
|
58
|
+
leak(now = Date.now()) {
|
|
59
|
+
if (now - this.lastLeak < this.leakInterval) return;
|
|
60
|
+
for (let [key, bucket] of this.buckets) {
|
|
61
|
+
if (bucket.leak(now) === false) {
|
|
62
|
+
this.opts.onEmpty?.(key);
|
|
63
|
+
this.buckets.delete(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
this.lastLeak = now;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Cancels/removes leak interval timer (if any) and removes all buckets.
|
|
70
|
+
*/
|
|
71
|
+
release() {
|
|
72
|
+
if (this.timer != null) {
|
|
73
|
+
clearInterval(this.timer);
|
|
74
|
+
this.timer = null;
|
|
75
|
+
}
|
|
76
|
+
this.buckets.clear();
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
class LeakyBucket {
|
|
81
|
+
level;
|
|
82
|
+
capacity;
|
|
83
|
+
lastLeak;
|
|
84
|
+
leakInterval;
|
|
85
|
+
timer;
|
|
86
|
+
constructor(opts, initialLevel = 0) {
|
|
87
|
+
this.capacity = opts?.capacity ?? 60;
|
|
88
|
+
this.level = Math.min(initialLevel, this.capacity);
|
|
89
|
+
this.leakInterval = opts?.leakInterval ?? 1e3;
|
|
90
|
+
this.lastLeak = Date.now();
|
|
91
|
+
if (opts?.autoLeak !== false) {
|
|
92
|
+
this.timer = setInterval(this.leak.bind(this), this.leakInterval);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Updates bucket's counter/level and returns true if successful or false if
|
|
97
|
+
* capacity had already been reached.
|
|
98
|
+
*/
|
|
99
|
+
update() {
|
|
100
|
+
if (this.level >= this.capacity) return false;
|
|
101
|
+
this.level++;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Leaks bucket's counter (taking into account current/given timestamp) and
|
|
106
|
+
* then returns one of the following values:
|
|
107
|
+
*
|
|
108
|
+
* - `undefined` if bucket is already empty or if this function was called
|
|
109
|
+
* within fewer than {@link LeakyBucketOpts.leakInterval} milliseconds
|
|
110
|
+
* since the last leak
|
|
111
|
+
* - `false` if bucket leaked and is now empty
|
|
112
|
+
* - `true` if bucket leaked and still non-empty
|
|
113
|
+
*
|
|
114
|
+
* @param now
|
|
115
|
+
*/
|
|
116
|
+
leak(now = Date.now()) {
|
|
117
|
+
const delta = now - this.lastLeak;
|
|
118
|
+
if (delta < this.leakInterval || !this.level) return;
|
|
119
|
+
const loss = Math.floor(delta / this.leakInterval);
|
|
120
|
+
if (this.level <= loss) {
|
|
121
|
+
this.level = 0;
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
this.level -= loss;
|
|
125
|
+
this.lastLeak = now;
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Cancels/removes leak interval timer (if any).
|
|
130
|
+
*/
|
|
131
|
+
release() {
|
|
132
|
+
if (this.timer != null) {
|
|
133
|
+
clearInterval(this.timer);
|
|
134
|
+
this.timer = null;
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
LeakyBucket,
|
|
141
|
+
LeakyBucketMap
|
|
142
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thi.ng/leaky-bucket",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Configurable, counter-based Leaky Bucket abstractions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"typings": "./index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://thi.ng/leaky-bucket",
|
|
14
|
+
"funding": [
|
|
15
|
+
{
|
|
16
|
+
"type": "github",
|
|
17
|
+
"url": "https://github.com/sponsors/postspectacular"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://patreon.com/thing_umbrella"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"type": "liberapay",
|
|
25
|
+
"url": "https://liberapay.com/thi.ng"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"author": "Karsten Schmidt (https://thi.ng)",
|
|
29
|
+
"license": "Apache-2.0",
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
32
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
33
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
34
|
+
"clean": "bun ../../tools/src/clean-package.ts",
|
|
35
|
+
"doc": "typedoc --options ../../typedoc.json --out doc src/index.ts",
|
|
36
|
+
"doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
|
|
37
|
+
"pub": "yarn npm publish --access public",
|
|
38
|
+
"test": "bun test",
|
|
39
|
+
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@thi.ng/api": "^8.11.22"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"esbuild": "^0.25.0",
|
|
46
|
+
"typedoc": "^0.27.7",
|
|
47
|
+
"typescript": "^5.7.3"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"associative",
|
|
51
|
+
"bucket",
|
|
52
|
+
"counter",
|
|
53
|
+
"datastructure",
|
|
54
|
+
"map",
|
|
55
|
+
"rate-limit",
|
|
56
|
+
"time",
|
|
57
|
+
"typescript"
|
|
58
|
+
],
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"browser": {
|
|
63
|
+
"process": false,
|
|
64
|
+
"setTimeout": false
|
|
65
|
+
},
|
|
66
|
+
"engines": {
|
|
67
|
+
"node": ">=18"
|
|
68
|
+
},
|
|
69
|
+
"files": [
|
|
70
|
+
"./*.js",
|
|
71
|
+
"./*.d.ts"
|
|
72
|
+
],
|
|
73
|
+
"exports": {
|
|
74
|
+
".": {
|
|
75
|
+
"default": "./index.js"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
"thi.ng": {
|
|
79
|
+
"status": "alpha",
|
|
80
|
+
"year": 2025
|
|
81
|
+
},
|
|
82
|
+
"gitHead": "55987d581e4985b1c3091ba6be3f9b53a0c5eeea\n"
|
|
83
|
+
}
|