gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit477d7bdc477d7bdcMerge branch 'p50'mre477d7bdc/lib/limits.hl

1.5 KB

  1. // lib/limits.hl — THE RATE LIMIT OF AN APP (gitoria#47; contract docs/apps-api.md §6): per app id, per minute, 300 reads (GET) and 60 writes (POST / PUT)
  2. // over every apps API route. A fixed window: the first request of an app opens a window of one minute, the count restarts when it is over.
  3. // The counts live IN THIS PROCESS (a static map, not a table): a restart or a deploy swap starts every app at zero again, two processes count apart.
  4. // GITORIA_LIMIT_READS / GITORIA_LIMIT_WRITES / GITORIA_LIMIT_WINDOW_MS are GATE-ONLY knobs (like GITORIA_EVENTS_RETRY_MS); the defaults are the contract's.
  5. // limitCheck(appId, method) → 0 when the request may go on, else the seconds the app has to wait (the Retry-After value)
  6. import { now } from 'hl:time'
  7. import { envOr, idiv } from './util.hl'
  8. static counts = {} // "<appId> r" / "<appId> w" → { start (ms), n }
  9. static numberEnv = (name, fallback) => {
  10. n = toNumber(envOr(name, '' + fallback))
  11. return n != null && n >= 1 ? n : fallback
  12. }
  13. static readLimit = numberEnv('GITORIA_LIMIT_READS', 300)
  14. static writeLimit = numberEnv('GITORIA_LIMIT_WRITES', 60)
  15. static windowMs = numberEnv('GITORIA_LIMIT_WINDOW_MS', 60000)
  16. static limitCheck = (appId, method) => {
  17. isRead = method == 'GET'
  18. key = appId + (isRead ? ' r' : ' w')
  19. cap = isRead ? readLimit : writeLimit
  20. t = now()
  21. row = counts[key]
  22. if (row == null || t - row.start >= windowMs) {
  23. counts[key] = { start = t n = 1 }
  24. return 0
  25. }
  26. if (row.n >= cap) {
  27. left = idiv(row.start + windowMs - t + 999, 1000)
  28. return left < 1 ? 1 : left
  29. }
  30. row.n = row.n + 1
  31. counts[key] = row
  32. return 0
  33. }

Branches

Latest commits

  • 477d7bdcMerge branch 'p50'mre
  • 00efc06fP50 reportmre
  • e3f85afblog: commit mention slipmre
  • 4d1a4cf4log: #25 numbersmre
  • 1af0a60ddocs: H25 run() correctionmre
  • eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
  • 8c8ba2f1log: PR flow announcedmre
  • fa00cd35log: Kunibert pings provenmre
  • 3a68cfaclog: components decisionmre
  • 87de8e31log: Kunibert connected to 9 app reposmre
  • c22aff69log: PR-only rollout startedmre
  • 25309981log: STOP and paused workers 2026-11-08mre
  • 5286803alog: worker briefs and reports of 2026-11-08mre
  • 85c86605log: 2026-11-08 Arachne on list shapemre
  • cb634512log: 2026-11-08 keyed lists livemre
  • 07c98d4aLists: keyed rows with @id, remove(id) and one whole-list assignment instead of hand-rebuilt loopsmre
  • 0db0d81elog: 2026-11-08 day52 live, W30 startedmre
  • b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmre
  • 1e8f4f0avendor: Hybriel set-day52 (fault-path leak fix #335, keyed rows #331)mre
  • f5f0fb7blog: 2026-11-08 day50/#29 live, H25 correction, V52 startedmre