Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

833 lines
35 KiB

  1. # bottleneck
  2. [![Downloads][npm-downloads]][npm-url]
  3. [![version][npm-version]][npm-url]
  4. [![License][npm-license]][license-url]
  5. Bottleneck is a lightweight and efficient Task Scheduler and Rate Limiter for Node.js and the browser.
  6. Bottleneck is an easy solution as it adds very little complexity to your code. It is battle-hardened, reliable and production-ready and used on a large scale in private companies and open source software.
  7. It supports **Clustering**: it can rate limit jobs across multiple Node.js instances. It uses Redis and strictly atomic operations to stay reliable in the presence of unreliable clients and networks. It also supports *Redis Cluster* and *Redis Sentinel*.
  8. **[Upgrading from version 1?](#upgrading-to-v2)**
  9. ## Install
  10. ```
  11. npm install --save bottleneck
  12. ```
  13. ## Quick Start
  14. **Note:** To support older browsers and Node <6.0, you must import the ES5 bundle instead.
  15. ```js
  16. import Bottleneck from "bottleneck/es5";
  17. ```
  18. ### Step 1 of 3
  19. Most APIs have a rate limit. For example, to execute 3 requests per second:
  20. ```js
  21. import Bottleneck from "bottleneck";
  22. const limiter = new Bottleneck({
  23. minTime: 333
  24. });
  25. ```
  26. If there's a chance some requests might take longer than 333ms and you want to prevent more than 1 request from running at a time, add `maxConcurrent: 1`:
  27. ```js
  28. const limiter = new Bottleneck({
  29. maxConcurrent: 1,
  30. minTime: 333
  31. });
  32. ```
  33. **Sometimes rate limits instead take the form of "X requests every Y seconds".** In this example, we throttle to 100 requests every 60 seconds:
  34. ```js
  35. const limiter = new Bottleneck({
  36. reservoir: 100, // initial value
  37. reservoirRefreshAmount: 100,
  38. reservoirRefreshInterval: 60 * 1000 // must be divisible by 250
  39. });
  40. ```
  41. `reservoir` is a counter decremented every time a job is launched, we set its initial value to 100. Then, every `reservoirRefreshInterval` (60000 ms), `reservoir` is automatically reset to `reservoirRefreshAmount` (100).
  42. **You should** still use `minTime` and/or `maxConcurrent` to spread out the load since running 100 requests in parallel might not be a good idea!
  43. ### Step 2 of 3
  44. #### ➤ Using callbacks?
  45. Instead of this:
  46. ```js
  47. someAsyncCall(arg1, arg2, callback);
  48. ```
  49. Do this:
  50. ```js
  51. limiter.submit(someAsyncCall, arg1, arg2, callback);
  52. ```
  53. #### ➤ Using promises?
  54. Instead of this:
  55. ```js
  56. myFunction(arg1, arg2)
  57. .then((result) => {
  58. /* handle result */
  59. });
  60. ```
  61. Do this:
  62. ```js
  63. limiter.schedule(() => myFunction(arg1, arg2))
  64. .then((result) => {
  65. /* handle result */
  66. });
  67. ```
  68. Or this:
  69. ```js
  70. const wrapped = limiter.wrap(myFunction);
  71. wrapped(arg1, arg2)
  72. .then((result) => {
  73. /* handle result */
  74. });
  75. ```
  76. #### ➤ Using async/await?
  77. Instead of this:
  78. ```js
  79. const result = await myFunction(arg1, arg2);
  80. ```
  81. Do this:
  82. ```js
  83. const result = await limiter.schedule(() => myFunction(arg1, arg2));
  84. ```
  85. Or this:
  86. ```js
  87. const wrapped = limiter.wrap(myFunction);
  88. const result = await wrapped(arg1, arg2);
  89. ```
  90. ### Step 3 of 3
  91. Remember...
  92. Bottleneck builds a queue of jobs and executes them as soon as possible. By default, the jobs will be executed in the order they were received.
  93. **Read the 'Gotchas' and you're good to go**. Or keep reading to learn about all the fine tuning and advanced options available. If your rate limits need to be enforced across a cluster of computers, read the [Clustering](#Clustering) docs.
  94. [Need help debugging your application?](#debugging-your-application).
  95. Instead of throttling maybe [you want to batch up requests](#batching) into fewer calls?
  96. #### Gotchas
  97. * Bottleneck requires Node 6+ to function. However, an ES5 build is included: `import Bottleneck from "bottleneck/es5";`.
  98. * Make sure you're catching `"error"` events emitted by your limiters!
  99. * Consider setting a `maxConcurrent` value instead of leaving it `null`. This can help your application's performance, especially if you think the limiter's queue might become very long.
  100. * **When using `submit()`**, if a callback isn't necessary, you must pass `null` or an empty function instead. It will not work otherwise.
  101. * **When using `submit()`**, make sure all the jobs will eventually complete by calling their callback, or set an [`expiration`](#job-options). Even if you submitted your job with a `null` callback , it still needs to call its callback. This is particularly important if you are using a `maxConcurrent` value that isn't `null` (unlimited), otherwise those not completed jobs will be clogging up the limiter and no new jobs will be allowed to run. It's safe to call the callback more than once, subsequent calls are ignored.
  102. ## Docs
  103. ### Constructor
  104. ```js
  105. const limiter = new Bottleneck({/* options */});
  106. ```
  107. Basic options:
  108. | Option | Default | Description |
  109. |--------|---------|-------------|
  110. | `maxConcurrent` | `null` (unlimited) | How many jobs can be executing at the same time. Consider setting a value instead of leaving it `null`, it can help your application's performance, especially if you think the limiter's queue might get very long. |
  111. | `minTime` | `0` ms | How long to wait after launching a job before launching another one. |
  112. | `highWater` | `null` (unlimited) | How long can the queue be? When the queue length exceeds that value, the selected `strategy` is executed to shed the load. |
  113. | `strategy` | `Bottleneck.strategy.LEAK` | Which strategy to use when the queue gets longer than the high water mark. [Read about strategies](#strategies). Strategies are never executed if `highWater` is `null`. |
  114. | `penalty` | `15 * minTime`, or `5000` when `minTime` is `0` | The `penalty` value used by the `BLOCK` strategy. |
  115. | `reservoir` | `null` (unlimited) | How many jobs can be executed before the limiter stops executing jobs. If `reservoir` reaches `0`, no jobs will be executed until it is no longer `0`. New jobs will still be queued up. |
  116. | `reservoirRefreshInterval` | `null` (disabled) | Every `reservoirRefreshInterval` milliseconds, the `reservoir` value will be automatically reset to `reservoirRefreshAmount`. The `reservoirRefreshInterval` value should be a [multiple of 250 (5000 for Clustering)](https://github.com/SGrondin/bottleneck/issues/88). |
  117. | `reservoirRefreshAmount` | `null` (disabled) | The value to reset `reservoir` to when `reservoirRefreshInterval` is in use. |
  118. | `Promise` | `Promise` (built-in) | This lets you override the Promise library used by Bottleneck. |
  119. ### submit()
  120. Adds a job to the queue. This is the callback version of `schedule()`.
  121. ```js
  122. limiter.submit(someAsyncCall, arg1, arg2, callback);
  123. ```
  124. You can pass `null` instead of an empty function if there is no callback, but `someAsyncCall` still needs to call **its** callback to let the limiter know it has completed its work.
  125. `submit()` can also accept [advanced options](#job-options).
  126. ### schedule()
  127. Adds a job to the queue. This is the Promise and async/await version of `submit()`.
  128. ```js
  129. const fn = function(arg1, arg2) {
  130. return httpGet(arg1, arg2); // Here httpGet() returns a promise
  131. };
  132. limiter.schedule(fn, arg1, arg2)
  133. .then((result) => {
  134. /* ... */
  135. });
  136. ```
  137. In other words, `schedule()` takes a function **fn** and a list of arguments. `schedule()` returns a promise that will be executed according to the rate limits.
  138. `schedule()` can also accept [advanced options](#job-options).
  139. Here's another example:
  140. ```js
  141. // suppose that `client.get(url)` returns a promise
  142. const url = "https://wikipedia.org";
  143. limiter.schedule(() => client.get(url))
  144. .then(response => console.log(response.body));
  145. ```
  146. ### wrap()
  147. Takes a function that returns a promise. Returns a function identical to the original, but rate limited.
  148. ```js
  149. const wrapped = limiter.wrap(fn);
  150. wrapped()
  151. .then(function (result) {
  152. /* ... */
  153. })
  154. .catch(function (error) {
  155. // Bottleneck might need to fail the job even if the original function can never fail.
  156. // For example, your job is taking longer than the `expiration` time you've set.
  157. });
  158. ```
  159. ### Job Options
  160. `submit()`, `schedule()`, and `wrap()` all accept advanced options.
  161. ```js
  162. // Submit
  163. limiter.submit({/* options */}, someAsyncCall, arg1, arg2, callback);
  164. // Schedule
  165. limiter.schedule({/* options */}, fn, arg1, arg2);
  166. // Wrap
  167. const wrapped = limiter.wrap(fn);
  168. wrapped.withOptions({/* options */}, arg1, arg2);
  169. ```
  170. | Option | Default | Description |
  171. |--------|---------|-------------|
  172. | `priority` | `5` | A priority between `0` and `9`. A job with a priority of `4` will be queued ahead of a job with a priority of `5`. **Important:** You must set a low `maxConcurrent` value for priorities to work, otherwise there is nothing to queue because jobs will be be scheduled immediately! |
  173. | `weight` | `1` | Must be an integer equal to or higher than `0`. The `weight` is what increases the number of running jobs (up to `maxConcurrent`) and decreases the `reservoir` value. |
  174. | `expiration` | `null` (unlimited) | The number of milliseconds a job is given to complete. Jobs that execute for longer than `expiration` ms will be failed with a `BottleneckError`. |
  175. | `id` | `<no-id>` | You should give an ID to your jobs, it helps with [debugging](#debugging-your-application). |
  176. ### Strategies
  177. A strategy is a simple algorithm that is executed every time adding a job would cause the number of queued jobs to exceed `highWater`. Strategies are never executed if `highWater` is `null`.
  178. #### Bottleneck.strategy.LEAK
  179. When adding a new job to a limiter, if the queue length reaches `highWater`, drop the oldest job with the lowest priority. This is useful when jobs that have been waiting for too long are not important anymore. If all the queued jobs are more important (based on their `priority` value) than the one being added, it will not be added.
  180. #### Bottleneck.strategy.OVERFLOW_PRIORITY
  181. Same as `LEAK`, except it will only drop jobs that are *less important* than the one being added. If all the queued jobs are as or more important than the new one, it will not be added.
  182. #### Bottleneck.strategy.OVERFLOW
  183. When adding a new job to a limiter, if the queue length reaches `highWater`, do not add the new job. This strategy totally ignores priority levels.
  184. #### Bottleneck.strategy.BLOCK
  185. When adding a new job to a limiter, if the queue length reaches `highWater`, the limiter falls into "blocked mode". All queued jobs are dropped and no new jobs will be accepted until the limiter unblocks. It will unblock after `penalty` milliseconds have passed without receiving a new job. `penalty` is equal to `15 * minTime` (or `5000` if `minTime` is `0`) by default. This strategy is ideal when bruteforce attacks are to be expected. This strategy totally ignores priority levels.
  186. ### Jobs lifecycle
  187. 1. **Received**. You new job has been added to your limiter. Bottleneck needs to check whether if can be accepted into the queue.
  188. 2. **Queued**. Bottleneck has accepted your job, but it can not tell at what exact timestamp it will run yet, because it is dependent on previous jobs.
  189. 3. **Running**. Your job is not in the queue anymore, it will be executed after a delay that was computed according to your `minTime` setting.
  190. 4. **Executing**. Your job is executing its code.
  191. 5. **Done**. Your job has completed.
  192. **Note:** By default, Bottleneck does not keep track of DONE jobs, to save memory. You can enable this feature by passing `trackDoneStatus: true` as an option when creating a limiter.
  193. #### counts()
  194. ```js
  195. const counts = limiter.counts();
  196. console.log(counts);
  197. /*
  198. {
  199. RECEIVED: 0,
  200. QUEUED: 0,
  201. RUNNING: 0,
  202. EXECUTING: 0,
  203. DONE: 0
  204. }
  205. */
  206. ```
  207. Returns an object with the current number of jobs per status in the limiter.
  208. #### jobStatus()
  209. ```js
  210. console.log(limiter.jobStatus("some-job-id"));
  211. // Example: QUEUED
  212. ```
  213. Returns the status of the job with the provided job id **in the limiter**. Returns `null` if no job with that id exist.
  214. #### jobs()
  215. ```js
  216. console.log(limiter.jobs("RUNNING"));
  217. // Example: ['id1', 'id2']
  218. ```
  219. Returns an array of all the job ids with the specified status **in the limiter**. Not passing a status string returns all the known ids.
  220. #### queued()
  221. ```js
  222. const count = limiter.queued(priority);
  223. console.log(count);
  224. ```
  225. `priority` is optional. Returns the number of `QUEUED` jobs with the given `priority` level. Omitting the `priority` argument returns the total number of queued jobs **in the limiter**.
  226. #### empty()
  227. ```js
  228. if (limiter.empty()) {
  229. // do something...
  230. }
  231. ```
  232. Returns a boolean which indicates whether there are any `RECEIVED` or `QUEUED` jobs **in the limiter**.
  233. #### running()
  234. ```js
  235. limiter.running()
  236. .then((count) => console.log(count));
  237. ```
  238. Returns a promise that returns the **total weight** of the `RUNNING` and `EXECUTING` jobs **in the Cluster**.
  239. #### done()
  240. ```js
  241. limiter.done()
  242. .then((count) => console.log(count));
  243. ```
  244. Returns a promise that returns the **total weight** of `DONE` jobs **in the Cluster**. Does not require passing the `trackDoneStatus: true` option.
  245. #### check()
  246. ```js
  247. limiter.check()
  248. .then((wouldRunNow) => console.log(wouldRunNow));
  249. ```
  250. Checks if a new job would be executed immediately if it was submitted now. Returns a promise that returns a boolean.
  251. ### Events
  252. Event names: `"error"`, `"empty"`, `"idle"`, `"dropped"`, `"depleted"` and `"debug"`.
  253. __error__
  254. ```js
  255. limiter.on("error", function (error) {
  256. /* handle errors here */
  257. });
  258. ```
  259. By far the most common source of errors is uncaught exceptions in your application code. If the jobs you add to Bottleneck don't catch their own exceptions, the limiter will emit an `"error"` event.
  260. If using Clustering, errors thrown by the Redis client will emit an `"error"` event.
  261. __empty__
  262. ```js
  263. limiter.on("empty", function () {
  264. // This will be called when `limiter.empty()` becomes true.
  265. });
  266. ```
  267. __idle__
  268. ```js
  269. limiter.on("idle", function () {
  270. // This will be called when `limiter.empty()` is `true` and `limiter.running()` is `0`.
  271. });
  272. ```
  273. __dropped__
  274. ```js
  275. limiter.on("dropped", function (dropped) {
  276. // This will be called when a strategy was triggered.
  277. // The dropped request is passed to this event listener.
  278. });
  279. ```
  280. __depleted__
  281. ```js
  282. limiter.on("depleted", function (empty) {
  283. // This will be called every time the reservoir drops to 0.
  284. // The `empty` (boolean) argument indicates whether `limiter.empty()` is currently true.
  285. });
  286. ```
  287. __debug__
  288. ```js
  289. limiter.on("debug", function (message, data) {
  290. // Useful to figure out what the limiter is doing in real time
  291. // and to help debug your application
  292. });
  293. ```
  294. Use `removeAllListeners()` with an optional event name as first argument to remove listeners.
  295. Use `.once()` instead of `.on()` to only receive a single event.
  296. ### updateSettings()
  297. ```js
  298. limiter.updateSettings(options);
  299. ```
  300. The options are the same as the [limiter constructor](#constructor).
  301. **Note:** Changes don't affect `SCHEDULED` jobs.
  302. ### incrementReservoir()
  303. ```js
  304. limiter.incrementReservoir(incrementBy);
  305. ```
  306. Returns a promise that returns the new reservoir value.
  307. ### currentReservoir()
  308. ```js
  309. limiter.currentReservoir()
  310. .then((reservoir) => console.log(reservoir));
  311. ```
  312. Returns a promise that returns the current reservoir value.
  313. ### stop()
  314. The `stop()` method is used to safely shutdown a limiter. It prevents any new jobs from being added to the limiter and waits for all `EXECUTING` jobs to complete.
  315. ```js
  316. limiter.stop(options)
  317. .then(() => {
  318. console.log("Shutdown completed!")
  319. });
  320. ```
  321. `stop()` returns a promise that resolves once all the `EXECUTING` jobs have completed and, if desired, once all non-`EXECUTING` jobs have been dropped.
  322. | Option | Default | Description |
  323. |--------|---------|-------------|
  324. | `dropWaitingJobs` | `true` | When `true`, drop all the `RECEIVED`, `QUEUED` and `RUNNING` jobs. When `false`, allow those jobs to complete before resolving the Promise returned by this method. |
  325. | `dropErrorMessage` | `This limiter has been stopped.` | The error message used to drop jobs when `dropWaitingJobs` is `true`. |
  326. | `enqueueErrorMessage` | `This limiter has been stopped and cannot accept new jobs.` | The error message used to reject a job added to the limiter after `stop()` has been called. |
  327. ### chain()
  328. Tasks that are ready to be executed will be added to that other limiter. Suppose you have 2 types of tasks, A and B. They both have their own limiter with their own settings, but both must also follow a global limiter G:
  329. ```js
  330. const limiterA = new Bottleneck( /* some settings */ );
  331. const limiterB = new Bottleneck( /* some different settings */ );
  332. const limiterG = new Bottleneck( /* some global settings */ );
  333. limiterA.chain(limiterG);
  334. limiterB.chain(limiterG);
  335. // Requests added to limiterA must follow the A and G rate limits.
  336. // Requests added to limiterB must follow the B and G rate limits.
  337. // Requests added to limiterG must follow the G rate limits.
  338. ```
  339. To unchain, call `limiter.chain(null);`.
  340. ## Group
  341. The `Group` feature of Bottleneck manages many limiters automatically for you. It creates limiters dynamically and transparently.
  342. Let's take a DNS server as an example of how Bottleneck can be used. It's a service that sees a lot of abuse and where incoming DNS requests need to be rate limited. Bottleneck is so tiny, it's acceptable to create one limiter for each origin IP, even if it means creating thousands of limiters. The `Group` feature is perfect for this use case. Create one Group and use the origin IP to rate limit each IP independently. Each call with the same key (IP) will be routed to the same underlying limiter. A Group is created like a limiter:
  343. ```js
  344. const group = new Bottleneck.Group(options);
  345. ```
  346. The `options` object will be used for every limiter created by the Group.
  347. The Group is then used with the `.key(str)` method:
  348. ```js
  349. // In this example, the key is an IP
  350. group.key("77.66.54.32").submit(someAsyncCall, arg1, arg2, cb);
  351. ```
  352. __key()__
  353. * `str` : The key to use. All jobs added with the same key will use the same underlying limiter. *Default: `""`*
  354. The return value of `.key(str)` is a limiter. If it doesn't already exist, it is generated for you. Calling `key()` is how limiters are created inside a Group.
  355. Limiters that have been idle for longer than 5 minutes are deleted to avoid memory leaks, this value can be changed by passing a different `timeout` option, in milliseconds.
  356. __on("created")__
  357. ```js
  358. group.on("created", (limiter, key) => {
  359. console.log("A new limiter was created for key: " + key)
  360. // Prepare the limiter, for example we'll want to listen to its "error" events!
  361. limiter.on("error", (err) => {
  362. // Handle errors here
  363. })
  364. });
  365. ```
  366. Listening for the `"created"` event is the recommended way to set up a new limiter. Your event handler is executed before `key()` returns the newly created limiter.
  367. __updateSettings()__
  368. ```js
  369. const group = new Bottleneck.Group({ maxConcurrent: 2, minTime: 250 });
  370. group.updateSettings({ minTime: 500 });
  371. ```
  372. After executing the above commands, **new limiters** will be created with `{ maxConcurrent: 2, minTime: 500 }`.
  373. __deleteKey()__
  374. * `str`: The key for the limiter to delete.
  375. Manually deletes the limiter at the specified key. This can be useful when the auto cleanup is turned off.
  376. __keys()__
  377. Returns an array containing all the keys in the Group.
  378. __limiters()__
  379. ```js
  380. const limiters = group.limiters();
  381. console.log(limiters);
  382. // [ { key: "some key", limiter: <limiter> }, { key: "some other key", limiter: <some other limiter> } ]
  383. ```
  384. ## Batching
  385. Some APIs can accept multiple operations in a single call. Bottleneck's Batching feature helps you take advantage of those APIs:
  386. ```js
  387. const batcher = new Bottleneck.Batcher({
  388. maxTime: 1000,
  389. maxSize: 10
  390. });
  391. batcher.on("batch", (batch) => {
  392. console.log(batch); // ["some-data", "some-other-data"]
  393. // Handle batch here
  394. });
  395. batcher.add("some-data");
  396. batcher.add("some-other-data");
  397. ```
  398. `batcher.add()` returns a Promise that resolves once the request has been flushed to a `"batch"` event.
  399. | Option | Default | Description |
  400. |--------|---------|-------------|
  401. | `maxTime` | `null` (unlimited) | Maximum acceptable time (in milliseconds) a request can have to wait before being flushed to the `"batch"` event. |
  402. | `maxSize` | `null` (unlimited) | Maximum number of requests in a batch. |
  403. Batching doesn't throttle requests, it only groups them up optimally according to your `maxTime` and `maxSize` settings.
  404. ## Clustering
  405. Clustering lets many limiters access the same shared state, stored in Redis. Changes to the state are Atomic, Consistent and Isolated (and fully [ACID](https://en.wikipedia.org/wiki/ACID) with the right [Durability](https://redis.io/topics/persistence) configuration), to eliminate any chances of race conditions or state corruption. Your settings, such as `maxConcurrent`, `minTime`, etc., are shared across the whole cluster, which means —for example— that `{ maxConcurrent: 5 }` guarantees no more than 5 jobs can ever run at a time in the entire cluster of limiters. 100% of Bottleneck's features are supported in Clustering mode. Enabling Clustering is as simple as changing a few settings. It's also a convenient way to store or export state for later use.
  406. ### Enabling Clustering
  407. First, add `redis` or `ioredis` to your application's dependencies:
  408. ```bash
  409. # NodeRedis (https://github.com/NodeRedis/node_redis)
  410. npm install --save redis
  411. # or ioredis (https://github.com/luin/ioredis)
  412. npm install --save ioredis
  413. ```
  414. Then create a limiter or a Group:
  415. ```js
  416. const limiter = new Bottleneck({
  417. /* Some basic options */
  418. maxConcurrent: 5,
  419. minTime: 500
  420. id: "my-super-app" // All limiters with the same id will be clustered together
  421. /* Clustering options */
  422. datastore: "redis", // or "ioredis"
  423. clearDatastore: false,
  424. clientOptions: {
  425. host: "127.0.0.1",
  426. port: 6379
  427. // Redis client options
  428. // Using NodeRedis? See https://github.com/NodeRedis/node_redis#options-object-properties
  429. // Using ioredis? See https://github.com/luin/ioredis/blob/master/API.md#new-redisport-host-options
  430. }
  431. });
  432. ```
  433. | Option | Default | Description |
  434. |--------|---------|-------------|
  435. | `datastore` | `"local"` | Where the limiter stores its internal state. The default (`"local"`) keeps the state in the limiter itself. Set it to `"redis"` or `"ioredis"` to enable Clustering. |
  436. | `clearDatastore` | `false` | When set to `true`, on initial startup, the limiter will wipe any existing Bottleneck state data on the Redis db. |
  437. | `clientOptions` | `{}` | This object is passed directly to the redis client library you've selected. |
  438. | `clusterNodes` | `null` | **ioredis only.** When `clusterNodes` is not null, the client will be instantiated by calling `new Redis.Cluster(clusterNodes, clientOptions)` instead of `new Redis(clientOptions)`. |
  439. | `timeout` | `null` (no TTL) | The Redis TTL in milliseconds ([TTL](https://redis.io/commands/ttl)) for the keys created by the limiter. When `timeout` is set, the limiter's state will be automatically removed from Redis after `timeout` milliseconds of inactivity. |
  440. **Note: When using Groups**, the `timeout` option has a default of `300000` milliseconds and the generated limiters automatically receive an `id` with the pattern `${group.id}-${KEY}`.
  441. ### Important considerations when Clustering
  442. The first limiter connecting to Redis will store its [constructor options](#constructor) on Redis and all subsequent limiters will be using those settings. You can alter the constructor options used by all the connected limiters by calling `updateSettings()`. The `clearDatastore` option instructs a new limiter to wipe any previous Bottleneck data (for that `id`), including previously stored settings.
  443. Queued jobs are **NOT** stored on Redis. They are local to each limiter. Exiting the Node.js process will lose those jobs. This is because Bottleneck has no way to propagate the JS code to run a job across a different Node.js process than the one it originated on. Bottleneck doesn't keep track of the queue contents of the limiters on a cluster for performance and reliability reasons. You can use something like [`BeeQueue`](https://github.com/bee-queue/bee-queue) to get around this limitation.
  444. Due to the above, functionality relying on the queue length happens purely locally:
  445. - Priorities are local. A higher priority job will run before a lower priority job **on the same limiter**. Another limiter on the cluster might run a lower priority job before our higher priority one.
  446. - Assuming constant priority levels, Bottleneck guarantees that jobs will be run in the order they were received **on the same limiter**. Another limiter on the cluster might run a job received later before ours runs.
  447. - `highWater` and load shedding ([strategies](#strategies)) are per limiter. However, one limiter entering Blocked mode will put the entire cluster in Blocked mode until `penalty` milliseconds have passed. See [Strategies](#strategies).
  448. - The `"empty"` event is triggered when the (local) queue is empty.
  449. - The `"idle"` event is triggered when the (local) queue is empty *and* no jobs are currently running anywhere in the cluster.
  450. You must work around these limitations in your application code if they are an issue to you. The `publish()` method could be useful here.
  451. The current design guarantees reliability, is highly performant and lets limiters come and go. Your application can scale up or down, and clients can be disconnected at any time without issues.
  452. It is **strongly recommended** that you give an `id` to every limiter and Group since it is used to build the name of your limiter's Redis keys! Limiters with the same `id` inside the same Redis db will be sharing the same datastore.
  453. It is **strongly recommended** that you set an `expiration` (See [Job Options](#job-options)) *on every job*, since that lets the cluster recover from crashed or disconnected clients. Otherwise, a client crashing while executing a job would not be able to tell the cluster to decrease its number of "running" jobs. By using expirations, those lost jobs are automatically cleared after the specified time has passed. Using expirations is essential to keeping a cluster reliable in the face of unpredictable application bugs, network hiccups, and so on.
  454. Network latency between Node.js and Redis is not taken into account when calculating timings (such as `minTime`). To minimize the impact of latency, Bottleneck performs the absolute minimum number of state accesses. Keeping the Redis server close to your limiters will help you get a more consistent experience. Keeping the clients' OS time consistent will also help.
  455. It is **strongly recommended** to [set up an `"error"` listener](#events) on all your limiters and on your Groups.
  456. Bottleneck does not guarantee that the concurrency will be spread evenly across limiters. With `{ maxConcurrent: 5 }`, it's absolutely possible for a single limiter to end up running 5 jobs simultaneously while the other limiters in the cluster sit idle. To spread the load, use the `.chain()` method:
  457. ```js
  458. const clusterLimiter = new Bottleneck({ maxConcurrent: 5, datastore: 'redis' });
  459. const limiter = new Bottleneck({ maxConcurrent: 1 });
  460. limiter.chain(clusterLimiter);
  461. clusterLimiter.ready()
  462. .then(() => {
  463. // Any Node process can only run one job at a time.
  464. // Across the whole cluster, up to 5 jobs can run simultaneously.
  465. limiter.schedule( /* ... */ )
  466. })
  467. .catch((error) => { /* ... */ });
  468. ```
  469. ### Clustering Methods
  470. The `ready()`, `publish()` and `clients()` methods also exist when using the `local` datastore, for code compatibility reasons: code written for `redis`/`ioredis` won't break with `local`.
  471. #### ready()
  472. This method returns a promise that resolves once the limiter is connected to Redis.
  473. As of v2.9.0, it's no longer necessary to wait for `.ready()` to resolve before issuing commands to a limiter. The commands will be queued until the limiter successfully connects. Make sure to listen to the `"error"` event to handle connection errors.
  474. ```js
  475. const limiter = new Bottleneck({/* options */});
  476. limiter.on("error", (err) => {
  477. // handle network errors
  478. });
  479. limiter.ready()
  480. .then(() => {
  481. // The limiter is ready
  482. });
  483. ```
  484. #### publish(message)
  485. This method broadcasts the `message` string to every limiter in the Cluster. It returns a promise.
  486. ```js
  487. const limiter = new Bottleneck({/* options */});
  488. limiter.on("message", (msg) => {
  489. console.log(msg); // prints "this is a string"
  490. });
  491. limiter.publish("this is a string");
  492. ```
  493. To send objects, stringify them first:
  494. ```js
  495. limiter.on("message", (msg) => {
  496. console.log(JSON.parse(msg).hello) // prints "world"
  497. });
  498. limiter.publish(JSON.stringify({ hello: "world" }));
  499. ```
  500. #### clients()
  501. If you need direct access to the redis clients, use `.clients()`:
  502. ```js
  503. console.log(limiter.clients());
  504. // { client: <Redis Client>, subscriber: <Redis Client> }
  505. ```
  506. ### Additional Clustering information
  507. - Bottleneck is compatible with [Redis Clusters](https://redis.io/topics/cluster-tutorial), but you must use the `ioredis` datastore and the `clusterNodes` option.
  508. - Bottleneck is compatible with Redis Sentinel, but you must use the `ioredis` datastore.
  509. - Bottleneck's data is stored in Redis keys starting with `b_`. It also uses pubsub channels starting with `b_` It will not interfere with any other data stored on the server.
  510. - Bottleneck loads a few Lua scripts on the Redis server using the `SCRIPT LOAD` command. These scripts only take up a few Kb of memory. Running the `SCRIPT FLUSH` command will cause any connected limiters to experience critical errors until a new limiter connects to Redis and loads the scripts again.
  511. - The Lua scripts are highly optimized and designed to use as few resources as possible.
  512. ### Managing Redis Connections
  513. Bottleneck needs to create 2 Redis Clients to function, one for normal operations and one for pubsub subscriptions. These 2 clients are kept in a `Bottleneck.RedisConnection` (NodeRedis) or a `Bottleneck.IORedisConnection` (ioredis) object, referred to as the Connection object.
  514. By default, every Group and every standalone limiter (a limiter not created by a Group) will create their own Connection object, but it is possible to manually control this behavior. In this example, every Group and limiter is sharing the same Connection object and therefore the same 2 clients:
  515. ```js
  516. const connection = new Bottleneck.RedisConnection({
  517. clientOptions: {/* NodeRedis/ioredis options */}
  518. // ioredis also accepts `clusterNodes` here
  519. });
  520. const limiter = new Bottleneck({ connection: connection });
  521. const group = new Bottleneck.Group({ connection: connection });
  522. ```
  523. You can access and reuse the Connection object of any Group or limiter:
  524. ```js
  525. const group = new Bottleneck.Group({ connection: limiter.connection });
  526. ```
  527. When a Connection object is created manually, the connectivity `"error"` events are emitted on the Connection itself.
  528. ```js
  529. connection.on("error", (err) => { /* handle connectivity errors here */ });
  530. ```
  531. If you already have a NodeRedis/ioredis client, you can ask Bottleneck to reuse it, although currently the Connection object will still create a second client for pubsub operations:
  532. ```js
  533. import Redis from "redis";
  534. const client = new Redis.createClient({/* options */});
  535. const connection = new Bottleneck.RedisConnection({
  536. // `clientOptions` and `clusterNodes` will be ignored since we're passing a raw client
  537. client: client
  538. });
  539. const limiter = new Bottleneck({ connection: connection });
  540. const group = new Bottleneck.Group({ connection: connection });
  541. ```
  542. Depending on your application, using more clients can improve performance.
  543. Use the `disconnect(flush)` method to close the Redis clients.
  544. ```js
  545. limiter.disconnect();
  546. group.disconnect();
  547. ```
  548. If you created the Connection object manually, you need to call `connection.disconnect()` instead, for safety reasons.
  549. ## Debugging your application
  550. Debugging complex scheduling logic can be difficult, especially when priorities, weights, and network latency all interact with one another.
  551. If your application is not behaving as expected, start by making sure you're catching `"error"` [events emitted](#events) by your limiters and your Groups. Those errors are most likely uncaught exceptions from your application code.
  552. Make sure you've read the ['Gotchas'](#gotchas) section.
  553. To see exactly what a limiter is doing in real time, listen to the `"debug"` event. It contains detailed information about how the limiter is executing your code. Adding [job IDs](#job-options) to all your jobs makes the debug output more readable.
  554. When Bottleneck has to fail one of your jobs, it does so by using `BottleneckError` objects. This lets you tell those errors apart from your own code's errors:
  555. ```js
  556. limiter.schedule(fn)
  557. .then((result) => { /* ... */ } )
  558. .catch((error) => {
  559. if (error instanceof Bottleneck.BottleneckError) {
  560. /* ... */
  561. }
  562. });
  563. ```
  564. ## Upgrading to v2
  565. The internal algorithms essentially haven't changed from v1, but many small changes to the interface were made to introduce new features.
  566. All the breaking changes:
  567. - Bottleneck v2 requires Node 6+ or a modern browser. Use `require("bottleneck/es5")` if you need ES5 support in v2. Bottleneck v1 will continue to use ES5 only.
  568. - The Bottleneck constructor now takes an options object. See [Constructor](#constructor).
  569. - The `Cluster` feature is now called `Group`. This is to distinguish it from the new v2 [Clustering](#clustering) feature.
  570. - The `Group` constructor takes an options object to match the limiter constructor.
  571. - Jobs take an optional options object. See [Job options](#job-options).
  572. - Removed `submitPriority()`, use `submit()` with an options object instead.
  573. - Removed `schedulePriority()`, use `schedule()` with an options object instead.
  574. - The `rejectOnDrop` option is now `true` by default. It can be set to `false` if you wish to retain v1 behavior. However this option is left undocumented as enabling it is considered to be a poor practice.
  575. - Use `null` instead of `0` to indicate an unlimited `maxConcurrent` value.
  576. - Use `null` instead of `-1` to indicate an unlimited `highWater` value.
  577. - Renamed `changeSettings()` to `updateSettings()`, it now returns a promise to indicate completion. It takes the same options object as the constructor.
  578. - Renamed `nbQueued()` to `queued()`.
  579. - Renamed `nbRunning` to `running()`, it now returns its result using a promise.
  580. - Removed `isBlocked()`.
  581. - Changing the Promise library is now done through the options object like any other limiter setting.
  582. - Removed `changePenalty()`, it is now done through the options object like any other limiter setting.
  583. - Removed `changeReservoir()`, it is now done through the options object like any other limiter setting.
  584. - Removed `stopAll()`. Use the new `stop()` method.
  585. - `check()` now accepts an optional `weight` argument, and returns its result using a promise.
  586. - Removed the `Group` `changeTimeout()` method. Instead, pass a `timeout` option when creating a Group.
  587. Version 2 is more user-friendly and powerful.
  588. After upgrading your code, please take a minute to read the [Debugging your application](#debugging-your-application) chapter.
  589. ## Contributing
  590. This README is always in need of improvements. If wording can be clearer and simpler, please consider forking this repo and submitting a Pull Request, or simply opening an issue.
  591. Suggestions and bug reports are also welcome.
  592. To work on the Bottleneck code, simply clone the repo, makes your changes to the files located in `src/` only, then run `./scripts/build.sh && npm test` to ensure that everything is set up correctly.
  593. To speed up compilation time during development, run `./scripts/build.sh dev` instead. Make sure to build and test without `dev` before submitting a PR.
  594. The tests must also pass in Clustering mode and using the ES5 bundle. You'll need a Redis server running on `127.0.0.1:6379`, then run `./scripts/build.sh && npm run test-all`.
  595. All contributions are appreciated and will be considered.
  596. [license-url]: https://github.com/SGrondin/bottleneck/blob/master/LICENSE
  597. [npm-url]: https://www.npmjs.com/package/bottleneck
  598. [npm-license]: https://img.shields.io/npm/l/bottleneck.svg?style=flat
  599. [npm-version]: https://img.shields.io/npm/v/bottleneck.svg?style=flat
  600. [npm-downloads]: https://img.shields.io/npm/dm/bottleneck.svg?style=flat