Skip to content

Commit 9f4d617

Browse files
committed
Configurable chat settings
1 parent c9210a0 commit 9f4d617

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

.env.development

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ SSL_KEY=
77

88
# /etc/letsencrypt/live/<your-domain-name>/fullchain.pem
99
SSL_CERT=
10+
11+
CHAT_MAX_HISTORY=10000
12+
CHAT_DEFAULT_TTL=72h
13+
CHAT_CLEANUP_INTERVAL=1h

.env.production

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ SSL_KEY=
77

88
# /etc/letsencrypt/live/<your-domain-name>/fullchain.pem
99
SSL_CERT=
10+
11+
CHAT_MAX_HISTORY=10000
12+
CHAT_DEFAULT_TTL=72h
13+
CHAT_CLEANUP_INTERVAL=1h

internal/chat/chat.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package chat
22

33
import (
44
"fmt"
5+
"os"
6+
"strconv"
57
"sync"
68
"time"
79

810
"github.com/google/uuid"
911
)
1012

1113
const (
12-
MaxHistory = 10000
13-
DefaultTTL = 72 * time.Hour
14-
CleanupInterval = 1 * time.Hour
14+
DefaultMaxHistory = 10000
15+
DefaultTTL = 72 * time.Hour
16+
DefaultCleanupInterval = 1 * time.Hour
1517

1618
EventTypeMessage = "message"
1719
EventTypeConnected = "connected"
@@ -53,12 +55,40 @@ type Manager struct {
5355
mu sync.RWMutex
5456
rooms map[string]*Room
5557
sessions map[string]*Session
58+
59+
maxHistory int
60+
defaultTTL time.Duration
61+
cleanupInterval time.Duration
5662
}
5763

5864
func NewManager() *Manager {
65+
maxHistory := DefaultMaxHistory
66+
if val := os.Getenv("CHAT_MAX_HISTORY"); val != "" {
67+
if i, err := strconv.Atoi(val); err == nil {
68+
maxHistory = i
69+
}
70+
}
71+
72+
defaultTTL := DefaultTTL
73+
if val := os.Getenv("CHAT_DEFAULT_TTL"); val != "" {
74+
if d, err := time.ParseDuration(val); err == nil {
75+
defaultTTL = d
76+
}
77+
}
78+
79+
cleanupInterval := DefaultCleanupInterval
80+
if val := os.Getenv("CHAT_CLEANUP_INTERVAL"); val != "" {
81+
if d, err := time.ParseDuration(val); err == nil {
82+
cleanupInterval = d
83+
}
84+
}
85+
5986
m := &Manager{
60-
rooms: make(map[string]*Room),
61-
sessions: make(map[string]*Session),
87+
rooms: make(map[string]*Room),
88+
sessions: make(map[string]*Session),
89+
maxHistory: maxHistory,
90+
defaultTTL: defaultTTL,
91+
cleanupInterval: cleanupInterval,
6292
}
6393
go m.cleanupLoop()
6494
return m
@@ -80,7 +110,7 @@ func (m *Manager) Connect(streamKey string) string {
80110
m.rooms[streamKey] = &Room{
81111
streamKey: streamKey,
82112
subscribers: make(map[string]*subscriber),
83-
history: make([]Event, 0, MaxHistory),
113+
history: make([]Event, 0, m.maxHistory),
84114
nextEventID: 1,
85115
lastActivity: now,
86116
}
@@ -198,7 +228,7 @@ func (m *Manager) Send(sessionID string, text string, displayName string) error
198228
}
199229
room.nextEventID++
200230

201-
if len(room.history) >= MaxHistory {
231+
if len(room.history) >= m.maxHistory {
202232
room.history = append(room.history[1:], event)
203233
} else {
204234
room.history = append(room.history, event)
@@ -216,7 +246,7 @@ func (m *Manager) Send(sessionID string, text string, displayName string) error
216246
}
217247

218248
func (m *Manager) cleanupLoop() {
219-
ticker := time.NewTicker(CleanupInterval)
249+
ticker := time.NewTicker(m.cleanupInterval)
220250
for range ticker.C {
221251
m.cleanup()
222252
}
@@ -228,14 +258,14 @@ func (m *Manager) cleanup() {
228258

229259
now := time.Now()
230260
for id, s := range m.sessions {
231-
if now.Sub(s.LastActivity) > DefaultTTL {
261+
if now.Sub(s.LastActivity) > m.defaultTTL {
232262
delete(m.sessions, id)
233263
}
234264
}
235265

236266
for key, r := range m.rooms {
237267
r.mu.Lock()
238-
if len(r.subscribers) == 0 && now.Sub(r.lastActivity) > DefaultTTL {
268+
if len(r.subscribers) == 0 && now.Sub(r.lastActivity) > m.defaultTTL {
239269
delete(m.rooms, key)
240270
}
241271
r.mu.Unlock()

0 commit comments

Comments
 (0)