Files
job-search/internal/config/conf_server_websocket.go
T
bwilliams 0b5fde9563 Config changes
- a source's interval must be greater than 0: a source is turned off with
  enabled, not with a zero interval, so JSCSourcesSource rejects an interval of
  0 or less (its validate is promoted to each source's section);
- max_message_size rejects -1: JSCServerWebSocket rejects a max_message_size
  of -1 (util.ByteSizeNoLimit), as a client could then exhaust the server's
  memory;
- every setting documents what 0 or empty means (zero/none, no limit or a
  default).
2026-10-15 16:00:48 +01:00

62 lines
2.1 KiB
Go

package config
import (
"errors"
"reflect"
"time"
"git.avsdev.uk/Go/job-search/internal/util"
)
// JSCKServerWebSocket is the websocket section's configuration key.
const JSCKServerWebSocket string = "websocket"
// JSCServerWebSocket configures the server's websocket connections.
// MaxMessageSize is the largest message read from a client; 0 means the
// default (32KiB), and -1 (no limit) is rejected (see validate). A
// WriteTimeout or PongTimeout of 0 or less means no limit, and a
// PingInterval of 0 or less turns pings off.
type JSCServerWebSocket struct {
EnableCompression bool `cfg:"enable_compression" default:"false"`
MaxMessageSize util.ByteSize `cfg:"max_message_size" default:"32KiB"`
ConnectionLimit JSCRateLimitRule `cfg:"connection_limit"`
MessageRateLimit JSCRateLimitRule `cfg:"message_rate_limit"`
WriteTimeout time.Duration `cfg:"write_timeout" default:"10s"`
PingInterval time.Duration `cfg:"ping_interval" default:"30s"`
PongTimeout time.Duration `cfg:"pong_timeout" default:"10s"`
}
// ---------- Section methods (see Section) ----------
func (c JSCServerWebSocket) Map() map[string]any { return mapOf(c) }
func (c JSCServerWebSocket) Flatten() map[string]any {
return flattenOf(JSCKServerWebSocket, c)
}
func (c JSCServerWebSocket) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
func (c JSCServerWebSocket) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
func (c *JSCServerWebSocket) SetValue(key string, value any) error {
return setValue(c, key, value)
}
// ---------- Section specific methods ----------
// validate checks values before they are stored (see validator).
// max_message_size can't be -1 (no limit): a client could then send a
// message of any size, and the server holds each message in memory.
func (c *JSCServerWebSocket) validate(key string, value any) error {
if key == "max_message_size" && value.(util.ByteSize) == util.ByteSizeNoLimit {
return errors.New(
"max_message_size can't be -1 (no limit): set an explicit size, or 0 for the default",
)
}
return nil
}