fix: review findings (async SQS dispatcher, backoff, batch insert, shutdown order, pagination, HMAC) + loadtest fixes; v0.1.6

This commit is contained in:
“Naeel”
2026-08-16 18:24:09 +04:00
parent 090cedca67
commit 3fedf9317c
27 changed files with 581 additions and 122 deletions
+15 -5
View File
@@ -29,12 +29,22 @@ VALUES ($1, $2, $3, $4, $5, $6)`,
return nil
}
// List возвращает устройства namespace (без сортировки по паролям — пароли на месте, но API их не отдаёт).
func (s *DeviceStore) List(ctx context.Context, namespace string) ([]Device, error) {
rows, err := s.db.QueryContext(ctx, `
SELECT namespace, name, device_id, enabled, mqtt_password, metadata, phase,
// List возвращает устройства namespace (без паролей — API их не отдаёт).
// limit <= 0 — без ограничения; offset — смещение (пагинация API).
func (s *DeviceStore) List(ctx context.Context, namespace string, limit, offset int) ([]Device, error) {
query := `SELECT namespace, name, device_id, enabled, mqtt_password, metadata, phase,
last_connected, created_at
FROM iot_devices WHERE namespace = $1 ORDER BY name`, namespace)
FROM iot_devices WHERE namespace = $1 ORDER BY name`
args := []any{namespace}
if limit > 0 {
query += fmt.Sprintf(" LIMIT $%d", len(args)+1)
args = append(args, limit)
}
if offset > 0 {
query += fmt.Sprintf(" OFFSET $%d", len(args)+1)
args = append(args, offset)
}
rows, err := s.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("store: list devices: %w", err)
}
+11 -1
View File
@@ -4,6 +4,8 @@ import (
"context"
"database/sql"
"fmt"
"os"
"strconv"
"time"
)
@@ -39,7 +41,15 @@ func Open(ctx context.Context, dsn string) (*DeviceStore, error) {
if err != nil {
return nil, fmt.Errorf("store: open DB: %w", err)
}
db.SetMaxOpenConns(5)
// Пул устройств: дефолт 20 (ревью 2026-08-16: 5 мало для API+MQTT auth),
// переопределяется IOT_PG_MAX_CONNS.
maxConns := 20
if v := os.Getenv("IOT_PG_MAX_CONNS"); v != "" {
if n, convErr := strconv.Atoi(v); convErr == nil && n > 0 {
maxConns = n
}
}
db.SetMaxOpenConns(maxConns)
db.SetMaxIdleConns(2)
db.SetConnMaxLifetime(5 * time.Minute)