Faucet: Limit usage to once per time window, increase quantity, and return the remaining wait time in case of a rate limit error.#727
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
apps/faucet/src/errors.ts
Outdated
| @@ -1,4 +1,5 @@ | |||
| import type { ContentfulStatusCode } from "hono/utils/http-status" | |||
| import ms from "ms" | |||
There was a problem hiding this comment.
cute little package! claude is telling me that this code will cover "most" cases though:
const unit = {
ms: 1,
s: 1e3,
m: 60e3,
h: 3_600e3,
d: 86_400e3,
} as const;
export function formatMs(ms: number, long = false): string {
const abs = Math.abs(ms);
for (const [abbr, size] of Object.entries(unit).reverse()) {
if (abs >= size) {
const val = Math.round(ms / size);
return long ? `${val} ${abbr}${Math.abs(val) !== 1 ? 's' : ''}` : `${val}${abbr}`;
}
}
return `${ms}ms`;
}
so im not sure if its worth the import? security risks (minimized by bun to be fair), 4kb package bloat (lol) to a backend service. not a hill i will die on
There was a problem hiding this comment.
changed 👍
| return err(new FaucetRateLimitError()) | ||
| if (faucetUsageResult.value.length >= 1) { | ||
| const lastRequest = faucetUsageResult.value[0] | ||
| const timeToWait = |
There was a problem hiding this comment.
is there a way this can be negative?
There was a problem hiding this comment.
i think we might be missing an if(timeToWait>0)... ?
There was a problem hiding this comment.
It cannot be negative because, if it were, that would mean the request occurred before the time window, and we're only processing requests within the time window
There was a problem hiding this comment.
So the reason is that the DB doesn't store uses longer than the window? This definitely could use a comment, and to be honest, a check wouldn't be a bad idea anyway just in case. e.g. if the service crashes, stays down for some time, then loads before the first prune can go through, the scenario of a negative number seems possible.
There was a problem hiding this comment.
done
c7c4802 to
bf2f73b
Compare
4109094 to
efddb9b
Compare
bf2f73b to
7fef662
Compare
efddb9b to
7fff5cd
Compare
7fef662 to
0ad0f49
Compare
7fff5cd to
adc389f
Compare
0ad0f49 to
8a3cf6b
Compare
adc389f to
85978f5
Compare
85978f5 to
eea769e
Compare

Description
Faucet: Limit usage to once per time window, increase quantity, and return the remaining wait time in case of a rate limit error
Toggle Checklist
Checklist
Basics
norswap/build-system-caching).Reminder: PR review guidelines
Correctness
testnet, mainnet, standalone wallet, ...).
< INDICATE BROWSER, DEMO APP & OTHER ENV DETAILS USED FOR TESTING HERE >
< INDICATE TESTED SCENARIOS (USER INTERFACE INTERACTION, CODE FLOWS) HERE >
and have updated the code & comments accordingly.
Architecture & Documentation
(2) commenting these boundaries correctly, (3) adding inline comments for context when needed.
Public APIS and meaningful (non-local) internal APIs are properly documented in code comments.
in a Markdown document.
make changesetforbreaking and meaningful changes in packages (not required for cleanups & refactors).