calcomblock/ratelimit.go
Alex Dunmow 18b7825592 Extract calcomblock into a standalone wasm plugin repo
Convert the bundled backend/internal/plugins/calcomblock package into a
standalone reactor-mode wasm plugin, dropping every git.dev.alexdunmow.com/block/cms
import (forbidden in standalone plugins):

- package calcomblock -> package main; add wasip1 main.go with
  wasmguest.Serve(Registration). go.mod module
  git.dev.alexdunmow.com/block/calcomblock, block/core v0.18.2, no replace
  directives.
- Settings persistence: replace the pool-backed poolQuerier (direct SQL against
  the public-schema `settings` table, which a sandboxed plugin role cannot read)
  with capabilityQuerier over the SDK settings.Settings / settings.Updater
  capabilities. GetSiteTimezone now resolves via GetSiteSettings host-side.
- Vendor the small CMS-internal helpers the plugin used into internal/helpers
  (GetRealIP, StartCleanupLoop, MaskSecret, GetStringOr, the PluginSettingsQuerier
  settings helpers, PluginCrypto for tests) and internal/db (minimal Setting /
  UpsertSettingParams), each with a provenance header.
- Inline the captcha widget: vendor blocks.CaptchaWidget as a local
  CaptchaWidget templ (captcha_widget.templ) and regenerate templ; drop the
  block/cms/blocks import from booking.templ.
- blockConfig (server-authoritative captcha requirement via a
  page_block_snapshots scan) has no wasm-ABI capability, so it is left nil:
  honeypot + per-IP rate limit still apply. Documented as a follow-up.
- web: @block-ninja/ui workspace:* -> ^0.1.0 registry version; eslint.config.js
  repointed at ../../../cms/web/eslint.config.js; rebuild web/dist.
- Rewrite CLAUDE.md for the standalone wasm reality; add .gitignore.

Full test suite preserved and passing; builds to calcomblock-2.0.0.bnp;
check-safety passes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-05 03:04:18 +08:00

62 lines
1.5 KiB
Go

package main
import (
"sync"
"time"
)
// RateLimiter is a per-IP fixed-window counter used to throttle the public
// /book endpoint. It is intentionally simple — in-memory, single-process —
// matching the single-tenant deployment model. A multi-replica deployment
// would need a shared store (Redis or DB) before this limiter is meaningful.
type RateLimiter struct {
mu sync.Mutex
buckets map[string]*rlBucket
perHour int
}
type rlBucket struct {
count int
resetsAt time.Time
}
// NewRateLimiter returns a limiter that allows perHour requests per IP in any
// rolling 1-hour window (resetting after the first request in a fresh window).
func NewRateLimiter(perHour int) *RateLimiter {
return &RateLimiter{
buckets: make(map[string]*rlBucket),
perHour: perHour,
}
}
// Allow consumes one allowance for ip; returns false when the window's quota
// is exhausted.
func (r *RateLimiter) Allow(ip string) bool {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
b := r.buckets[ip]
if b == nil || now.After(b.resetsAt) {
r.buckets[ip] = &rlBucket{count: 1, resetsAt: now.Add(time.Hour)}
return true
}
if b.count >= r.perHour {
return false
}
b.count++
return true
}
// SweepStale drops buckets whose window has elapsed. Driven by
// helpers.StartCleanupLoop on a periodic schedule.
func (r *RateLimiter) SweepStale() {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
for ip, b := range r.buckets {
if now.After(b.resetsAt) {
delete(r.buckets, ip)
}
}
}