-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
43 lines (33 loc) · 1.2 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package sse
import "time"
// Config defines the configuration settings for the Server-Sent Events (SSE) handler.
type Config[T any] struct {
// How often to flush the events to the connected clients. Default is 256ms
FlushFrequency time.Duration
// How long to wait for all connected client to gracefully close. Default is 30s
CloseTimeout time.Duration
// Replay events when a client connects
Replay struct {
// How many events to send in chunks, when a client connects. Default is 256 events
Initial int
// How many events to keep in memory. Default is 2048 events
Maximum int
// How long an event should be kept in memory for. Default is 30s
Expiry time.Duration
}
// Events encoder function, which returns a slice of bytes that will then be converted to a string. Default is json.Marshal()
Encoder func([]T) ([]byte, error)
}
// NewConfig initializes a configuration instance with reasonable defaults.
func NewConfig[T any]() *Config[T] {
cfg := &Config[T]{
FlushFrequency: 256 * time.Millisecond,
CloseTimeout: 30 * time.Second,
// Use the default encoder
Encoder: nil,
}
cfg.Replay.Initial = 256
cfg.Replay.Maximum = 2048
cfg.Replay.Expiry = 30 * time.Second
return cfg
}