gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit536ca6f9536ca6f9Allow repository members to read pull workflow APIs with user tokensmre536ca6f9/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

  • 536ca6f9Allow repository members to read pull workflow APIs with user tokensmre
  • 705dd834W49 missionmre
  • b554cd5flogmre
  • f4c9440ddeploy.sh: tar --hard-dereference for the live data backupmre
  • 1a687b15Merge branch 'w48-member-merge'mre
  • 166c775fW48 report, logmre
  • 1b4623f0Members merge pull requests: API route, button on /pulls and /pulls/N, one shared merge pathmre
  • 1075c38cW48 mission, logmre
  • 17bdd0d6V37/V39 missions, logmre
  • 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
  • e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
  • 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
  • 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
  • c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
  • 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
  • 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
  • 149dd512Rewrite W39 reportmre
  • e5385075Expect 401 for POST /api/repos without a tokenmre
  • 1a76055cDocument repository creation APImre
  • 4d779b3bAdd create-api gatemre