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:
@@ -0,0 +1,49 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type gzipWriter struct {
|
||||
gin.ResponseWriter
|
||||
writer io.Writer
|
||||
}
|
||||
|
||||
func (g *gzipWriter) Write(data []byte) (int, error) {
|
||||
return g.writer.Write(data)
|
||||
}
|
||||
|
||||
func (g *gzipWriter) WriteString(s string) (int, error) {
|
||||
return g.writer.Write([]byte(s))
|
||||
}
|
||||
|
||||
// Gzip پاسخهای JSON رو (وقتی کلاینت gzip رو توی Accept-Encoding پشتیبانی کنه)
|
||||
// فشرده میکنه؛ بدون هیچ وابستگی بیرونی، فقط با compress/gzip استاندارد گو.
|
||||
// برای مسیرهایی مثل فید دیسکاوری که پاسخ JSON نسبتاً بزرگی برمیگردونن، حجم
|
||||
// انتقال رو محسوس کم میکنه.
|
||||
func Gzip() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if !strings.Contains(c.GetHeader("Accept-Encoding"), "gzip") {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
// فایلهای عکسِ آپلودشده از قبل فشردهان (jpeg/png/webp)؛ دوباره gzip
|
||||
// کردنشون فقط CPU هدر میده بدون فایدهی محسوس.
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/uploads") {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
gz := gzip.NewWriter(c.Writer)
|
||||
defer gz.Close()
|
||||
|
||||
c.Header("Content-Encoding", "gzip")
|
||||
c.Header("Vary", "Accept-Encoding")
|
||||
c.Writer = &gzipWriter{ResponseWriter: c.Writer, writer: gz}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"dating-app/internal/models"
|
||||
"dating-app/internal/monitoring"
|
||||
)
|
||||
|
||||
// Metrics هر درخواست رو (متد، مسیر، کد پاسخ، مدت زمان) توی Store ثبت میکنه.
|
||||
// باید قبل از Recovery ثبت بشه (بیرونیتر) تا حتی درخواستهایی که پنیک میکنن
|
||||
// و توسط Recovery مهار میشن هم توی آمار لحاظ بشن.
|
||||
func Metrics(store *monitoring.Store) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
defer func() {
|
||||
path := c.FullPath()
|
||||
if path == "" {
|
||||
path = c.Request.URL.Path
|
||||
}
|
||||
|
||||
rec := monitoring.RequestRecord{
|
||||
Time: start,
|
||||
Method: c.Request.Method,
|
||||
Path: path,
|
||||
StatusCode: c.Writer.Status(),
|
||||
DurationMs: float64(time.Since(start).Microseconds()) / 1000.0,
|
||||
ClientIP: c.ClientIP(),
|
||||
}
|
||||
if userVal, exists := c.Get(ContextUserKey); exists {
|
||||
if user, ok := userVal.(*models.User); ok {
|
||||
id := user.ID
|
||||
rec.UserID = &id
|
||||
}
|
||||
}
|
||||
store.Record(rec)
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"dating-app/internal/utils"
|
||||
)
|
||||
|
||||
type bucket struct {
|
||||
tokens float64
|
||||
lastSeenAt time.Time
|
||||
}
|
||||
|
||||
// RateLimiter یک rate limiter سادهی token-bucket بهازای هر IP؛ برای جلوگیری
|
||||
// از سوءاستفاده از اندپوینتهای حساس مثل درخواست OTP (که هزینهی پیامک داره
|
||||
// و هدف مناسبی برای اسپم/brute-force هست). maxTokens یعنی سقف درخواست
|
||||
// پشتسرهم مجاز، refillPerSecond یعنی سرعت شارژ دوبارهی سطل.
|
||||
func RateLimiter(maxTokens, refillPerSecond float64) gin.HandlerFunc {
|
||||
var mu sync.Mutex
|
||||
buckets := make(map[string]*bucket)
|
||||
|
||||
// سطلهای راکد (IP هایی که مدتیه درخواست نزدن) رو دورهای پاک میکنه تا
|
||||
// حافظه با تعداد IP های دیدهشده بینهایت رشد نکنه.
|
||||
go func() {
|
||||
ticker := time.NewTicker(10 * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
cutoff := time.Now().Add(-10 * time.Minute)
|
||||
mu.Lock()
|
||||
for k, b := range buckets {
|
||||
if b.lastSeenAt.Before(cutoff) {
|
||||
delete(buckets, k)
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
|
||||
return func(c *gin.Context) {
|
||||
key := c.ClientIP()
|
||||
now := time.Now()
|
||||
|
||||
mu.Lock()
|
||||
b, ok := buckets[key]
|
||||
if !ok {
|
||||
b = &bucket{tokens: maxTokens, lastSeenAt: now}
|
||||
buckets[key] = b
|
||||
}
|
||||
elapsed := now.Sub(b.lastSeenAt).Seconds()
|
||||
b.tokens = min(maxTokens, b.tokens+elapsed*refillPerSecond)
|
||||
b.lastSeenAt = now
|
||||
|
||||
allowed := b.tokens >= 1
|
||||
if allowed {
|
||||
b.tokens--
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
if !allowed {
|
||||
utils.Fail(c, http.StatusTooManyRequests, "درخواستهای زیادی فرستادی؛ کمی صبر کن و دوباره امتحان کن", nil)
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func newTestEngine(mw gin.HandlerFunc) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
engine := gin.New()
|
||||
engine.GET("/x", mw, func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true})
|
||||
})
|
||||
return engine
|
||||
}
|
||||
|
||||
func TestRateLimiter_AllowsWithinLimit(t *testing.T) {
|
||||
engine := newTestEngine(RateLimiter(3, 1))
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req.RemoteAddr = "1.2.3.4:1111"
|
||||
w := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("request %d: expected 200, got %d", i+1, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimiter_BlocksOverLimit(t *testing.T) {
|
||||
engine := newTestEngine(RateLimiter(2, 0)) // بدون شارژ دوباره، فقط ۲ تا مجازه
|
||||
|
||||
for i := 0; i < 2; i++ {
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req.RemoteAddr = "5.6.7.8:2222"
|
||||
w := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("request %d: expected 200, got %d", i+1, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req.RemoteAddr = "5.6.7.8:2222"
|
||||
w := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusTooManyRequests {
|
||||
t.Fatalf("expected 429 on 3rd request, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRateLimiter_SeparateBucketsPerIP(t *testing.T) {
|
||||
engine := newTestEngine(RateLimiter(1, 0))
|
||||
|
||||
req1 := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req1.RemoteAddr = "9.9.9.9:1"
|
||||
w1 := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w1, req1)
|
||||
if w1.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200 for first IP, got %d", w1.Code)
|
||||
}
|
||||
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/x", nil)
|
||||
req2.RemoteAddr = "8.8.8.8:1"
|
||||
w2 := httptest.NewRecorder()
|
||||
engine.ServeHTTP(w2, req2)
|
||||
if w2.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200 for a different IP (separate bucket), got %d", w2.Code)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"dating-app/internal/utils"
|
||||
)
|
||||
|
||||
// Recovery از کرش کردن کل پروسه بهخاطر panic غیرمنتظرهی یک هندلر
|
||||
// (nil pointer، index out of range و...) جلوگیری میکنه: استکتریس رو لاگ
|
||||
// میکنه و بهجای متن سادهی پیشفرض Gin، همون envelope استاندارد JSON
|
||||
// پروژه (utils.Fail) رو برمیگردونه تا کلاینتها فرمت یکسانی ببینن.
|
||||
func Recovery() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("[PANIC RECOVERED] %v\n%s", r, debug.Stack())
|
||||
if !c.Writer.Written() {
|
||||
utils.Fail(c, http.StatusInternalServerError, "خطای غیرمنتظرهای رخ داد؛ لطفاً دوباره امتحان کن", nil)
|
||||
}
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user