forked from mtusi2026/mtusi2026lab2
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package config
|
|
|
|
import "fmt"
|
|
|
|
// Memory defines logical capacity limits; MaxStateBytes is not a measurement of RSS.
|
|
type Memory struct {
|
|
// MaxUsers limits the number of registered users.
|
|
MaxUsers int
|
|
// MaxChats limits the number of immutable chats.
|
|
MaxChats int
|
|
// MaxMessages limits the total number of messages across all chats.
|
|
MaxMessages int
|
|
// MaxStateBytes limits the logical storage budget, including estimated record overhead.
|
|
MaxStateBytes int
|
|
}
|
|
|
|
// defaultMemory returns the default storage capacity limits.
|
|
func defaultMemory() *Memory {
|
|
return &Memory{
|
|
MaxUsers: 10000,
|
|
MaxChats: 10000,
|
|
MaxMessages: 100000,
|
|
MaxStateBytes: 32 << 20,
|
|
}
|
|
}
|
|
|
|
// loadEnvironment applies environment overrides to this configuration section.
|
|
func (cfg *Memory) loadEnvironment() error {
|
|
return loadIntegers(
|
|
integerSetting{"MAX_USERS", &cfg.MaxUsers},
|
|
integerSetting{"MAX_CHATS", &cfg.MaxChats},
|
|
integerSetting{"MAX_MESSAGES", &cfg.MaxMessages},
|
|
integerSetting{"MAX_STATE_BYTES", &cfg.MaxStateBytes},
|
|
)
|
|
}
|
|
|
|
// Validate rejects disabled, negative and excessively large storage limits.
|
|
func (cfg *Memory) Validate() error {
|
|
if cfg == nil {
|
|
return fmt.Errorf("Memory configuration is required")
|
|
}
|
|
settings := []integerSetting{
|
|
{"MAX_USERS", &cfg.MaxUsers},
|
|
{"MAX_CHATS", &cfg.MaxChats},
|
|
{"MAX_MESSAGES", &cfg.MaxMessages},
|
|
{"MAX_STATE_BYTES", &cfg.MaxStateBytes},
|
|
}
|
|
for _, setting := range settings {
|
|
if err := validatePositive(setting.name, *setting.target); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|