feat: add 5 experiment

This commit is contained in:
TAskMAster339
2026-10-10 21:32:33 +03:00
parent 6892af8842
commit 715f6c4175
1397 changed files with 271651 additions and 56 deletions
+18
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"encoding/json"
"errors"
"log"
"messenger/src/backend/handlers"
@@ -11,6 +12,7 @@ import (
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
)
@@ -63,6 +65,22 @@ func main() {
log.Fatalf("database initialization failed: %v", err)
}
defer db.Close()
// Opt-in experiment telemetry goes to container logs, never a public port.
if os.Getenv("BENCH_POOL_STATS") == "1" {
go func() {
for range time.Tick(time.Second) {
s := db.Stat()
data, _ := json.Marshal(map[string]any{
"time": time.Now().UTC(), "acquired": s.AcquiredConns(),
"total": s.TotalConns(), "max": s.MaxConns(),
"gomaxprocs": runtime.GOMAXPROCS(0), "goroutines": runtime.NumGoroutine(),
"acquire_count": s.AcquireCount(), "acquire_duration_ns": s.AcquireDuration().Nanoseconds(),
"empty_acquire_count": s.EmptyAcquireCount(), "canceled_acquire_count": s.CanceledAcquireCount(),
})
log.Printf("benchmark_pool %s", data)
}
}()
}
redisAddress := os.Getenv("REDIS_ADDR")
if redisAddress == "" {
redisAddress = "redis:6379"
+8
View File
@@ -6,6 +6,7 @@ import (
"embed"
"errors"
"os"
"strconv"
"strings"
"time"
@@ -43,6 +44,13 @@ func OpenDatabase(ctx context.Context) (*pgxpool.Pool, error) {
return nil, err
}
cfg.MaxConns = 10
if value := os.Getenv("DATABASE_MAX_CONNS"); value != "" {
maxConns, parseErr := strconv.Atoi(value)
if parseErr != nil || maxConns < 1 || maxConns > 100 {
return nil, errors.New("DATABASE_MAX_CONNS must be between 1 and 100")
}
cfg.MaxConns = int32(maxConns)
}
cfg.ConnConfig.ConnectTimeout = 5 * time.Second
db, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
-27
View File
@@ -5,8 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"strconv"
"strings"
"unicode/utf8"
"messenger/src/backend/repositories"
)
@@ -24,31 +22,6 @@ func NewMessageService(messages *repositories.MessageStore) *MessageService {
return &MessageService{messages: messages}
}
func (s *MessageService) Create(ctx context.Context, userID string, chat repositories.Chat, text string) (repositories.Message, error) {
if !ChatHasMember(chat, userID) {
return repositories.Message{}, ErrChatNotFound
}
if strings.TrimSpace(text) == "" {
return repositories.Message{}, InvalidInput{"text must not be blank"}
}
if utf8.RuneCountInString(text) > 4000 {
return repositories.Message{}, InvalidInput{"text is too long"}
}
return s.messages.Create(ctx, chat.ID, userID, text)
}
func MessageLess(left, right repositories.Message) bool {
if left.CreatedAt.Equal(right.CreatedAt) {
return left.ID < right.ID
}
return left.CreatedAt.Before(right.CreatedAt)
}
func MessageAfterCursor(message repositories.Message, cursor repositories.MessageCursor) bool {
return message.CreatedAt.After(cursor.CreatedAt) ||
(message.CreatedAt.Equal(cursor.CreatedAt) && message.ID > cursor.MessageID)
}
func EncodeMessageCursor(cursor repositories.MessageCursor) (string, error) {
data, err := json.Marshal(cursor)
if err != nil {
-10
View File
@@ -2,22 +2,12 @@ package tests
import (
"context"
"messenger/src/backend/services"
"testing"
"time"
"github.com/google/uuid"
)
func TestMessageLessUsesIDForEqualTimestamps(t *testing.T) {
createdAt := time.Date(2026, 9, 18, 12, 0, 0, 0, time.UTC)
first := Message{ID: "a", CreatedAt: createdAt}
second := Message{ID: "b", CreatedAt: createdAt}
if !services.MessageLess(first, second) || services.MessageLess(second, first) {
t.Fatal("equal timestamps must be ordered by ID")
}
}
func TestListPageByChatUsesCursorAndLimit(t *testing.T) {
f := newTestFixture(t)
user, _ := createTestUser(t, f.users, "alice", "Алиса")
-1
View File
@@ -7,6 +7,5 @@ import (
type User = repositories.User
type Chat = repositories.Chat
type Message = repositories.Message
type messagePage = services.MessagePage
type messageCursor = repositories.MessageCursor