Add production hardening: monitoring, backups, caching, and high-traffic resilience
- Recovery middleware (no more crash-on-panic), request metrics/monitoring store + admin API, in-memory TTL caching for read-heavy option/question/ category lookups, gzip + rate-limiting middleware, sanitized chat message encoding, and pg_dump-based database backups with an admin API. - Fix N+1 queries in the discover feed, candidate detail, and match list endpoints by batching selection/photo/user/last-message lookups into single queries instead of one-per-item loops. - Tune DB connection pooling and add HTTP server timeouts + graceful shutdown so the API degrades gracefully instead of falling over under load. - Fix a null (vs empty array) JSON response for candidates/matches without photos or interests, which crashed the web app's swipe card.
This commit is contained in:
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
// Package cache یک کش درونحافظهای ساده با انقضای زمانی (TTL) ارائه میده.
|
||||
// برای دادههایی مناسبه که زیاد خونده میشن ولی بهندرت تغییر میکنن (مثل
|
||||
// گزینههای دستهبندیها و سوالات پروفایل که فقط از پنل ادمین تغییر میکنن).
|
||||
package cache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type entry[V any] struct {
|
||||
value V
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type TTLCache[K comparable, V any] struct {
|
||||
mu sync.RWMutex
|
||||
ttl time.Duration
|
||||
m map[K]entry[V]
|
||||
}
|
||||
|
||||
func New[K comparable, V any](ttl time.Duration) *TTLCache[K, V] {
|
||||
return &TTLCache[K, V]{ttl: ttl, m: make(map[K]entry[V])}
|
||||
}
|
||||
|
||||
func (c *TTLCache[K, V]) Get(key K) (V, bool) {
|
||||
c.mu.RLock()
|
||||
e, ok := c.m[key]
|
||||
c.mu.RUnlock()
|
||||
|
||||
var zero V
|
||||
if !ok || time.Now().After(e.expiresAt) {
|
||||
return zero, false
|
||||
}
|
||||
return e.value, true
|
||||
}
|
||||
|
||||
func (c *TTLCache[K, V]) Set(key K, value V) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.m[key] = entry[V]{value: value, expiresAt: time.Now().Add(c.ttl)}
|
||||
}
|
||||
|
||||
func (c *TTLCache[K, V]) Delete(key K) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
delete(c.m, key)
|
||||
}
|
||||
|
||||
// Clear کل کش رو خالی میکنه؛ برای موقعی که نمیشه/نمیخوایم کلید دقیق رو حساب کنیم.
|
||||
func (c *TTLCache[K, V]) Clear() {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
c.m = make(map[K]entry[V])
|
||||
}
|
||||
Reference in New Issue
Block a user