websocket/buffer: make size configurable

This commit is contained in:
Tulir Asokan 2024-12-07 01:30:42 +02:00
parent 894fcb3fa0
commit 149114354a
2 changed files with 12 additions and 6 deletions

View file

@ -48,7 +48,7 @@ func NewEventBuffer(maxSize int) *EventBuffer {
websocketClosers: make(map[uint64]WebsocketCloseFunc), websocketClosers: make(map[uint64]WebsocketCloseFunc),
lastAckedID: make(map[uint64]int64), lastAckedID: make(map[uint64]int64),
eventListeners: make(map[uint64]func(*hicli.JSONCommand)), eventListeners: make(map[uint64]func(*hicli.JSONCommand)),
buf: make([]*hicli.JSONCommand, 0, maxSize*2), buf: make([]*hicli.JSONCommand, 0, 32),
MaxSize: maxSize, MaxSize: maxSize,
minID: -1, minID: -1,
} }

View file

@ -42,11 +42,12 @@ type MatrixConfig struct {
} }
type WebConfig struct { type WebConfig struct {
ListenAddress string `yaml:"listen_address"` ListenAddress string `yaml:"listen_address"`
Username string `yaml:"username"` Username string `yaml:"username"`
PasswordHash string `yaml:"password_hash"` PasswordHash string `yaml:"password_hash"`
TokenKey string `yaml:"token_key"` TokenKey string `yaml:"token_key"`
DebugEndpoints bool `yaml:"debug_endpoints"` DebugEndpoints bool `yaml:"debug_endpoints"`
EventBufferSize int `yaml:"event_buffer_size"`
} }
var defaultFileWriter = zeroconfig.WriterConfig{ var defaultFileWriter = zeroconfig.WriterConfig{
@ -115,12 +116,17 @@ func (gmx *Gomuks) LoadConfig() error {
gmx.Config.Web.PasswordHash = string(hash) gmx.Config.Web.PasswordHash = string(hash)
changed = true changed = true
} }
if gmx.Config.Web.EventBufferSize <= 0 {
gmx.Config.Web.EventBufferSize = 512
changed = true
}
if changed { if changed {
err = gmx.SaveConfig() err = gmx.SaveConfig()
if err != nil { if err != nil {
return fmt.Errorf("failed to save config: %w", err) return fmt.Errorf("failed to save config: %w", err)
} }
} }
gmx.EventBuffer = NewEventBuffer(gmx.Config.Web.EventBufferSize)
return nil return nil
} }