-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
185 lines (155 loc) · 4.91 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package fibergun
import (
"sync"
"time"
"github.com/gofiber/fiber/v2"
)
// Config defines the config for GunDB middleware
type Config struct {
// Next defines a function to skip this middleware when returned true
Next func(c *fiber.Ctx) bool
// WebSocketEndpoint is the endpoint where GunDB websocket connections will be handled
WebSocketEndpoint string
// StaticPath is the path to serve the GunDB client files
StaticPath string
// HeartbeatInterval is the interval for sending heartbeat pings
HeartbeatInterval time.Duration
// PeerTimeout is the duration after which a peer is considered inactive
PeerTimeout time.Duration
// MaxMessageSize is the maximum size of a WebSocket message in bytes
MaxMessageSize int64
// EnableCompression enables WebSocket compression
EnableCompression bool
// BufferSize sets the read/write buffer size for WebSocket connections
BufferSize int
// Debug enables detailed logging
Debug bool
// ReconnectAttempts is the number of times to attempt reconnection
ReconnectAttempts int
// ReconnectInterval is the time to wait between reconnection attempts
ReconnectInterval time.Duration
// DataReplication configures how data is replicated between peers
DataReplication DataReplicationConfig
// PeerList stores active peer connections (internal use)
peers *sync.Map
SharedStore *sync.Map
}
// DataReplicationConfig defines how data is replicated between peers
type DataReplicationConfig struct {
// Enabled determines if data should be replicated between peers
Enabled bool
// SyncInterval is how often to sync data between peers
SyncInterval time.Duration
// MaxRetries is the maximum number of sync retries
MaxRetries int
// BatchSize is the maximum number of items to sync at once
BatchSize int
}
// defaultConfig is the default config (private)
var defaultConfig = &Config{
WebSocketEndpoint: "/gun",
StaticPath: "./public",
HeartbeatInterval: 15 * time.Second,
PeerTimeout: 60 * time.Second,
MaxMessageSize: 1024 * 1024, // 1MB
EnableCompression: true,
BufferSize: 1024 * 16, // 16KB
Debug: false,
ReconnectAttempts: 5,
ReconnectInterval: 2 * time.Second,
DataReplication: DataReplicationConfig{
Enabled: true,
SyncInterval: 30 * time.Second,
MaxRetries: 3,
BatchSize: 100,
},
peers: &sync.Map{},
}
// NewConfig creates a new config with default values
func NewConfig() *Config {
return &Config{
WebSocketEndpoint: defaultConfig.WebSocketEndpoint,
StaticPath: defaultConfig.StaticPath,
HeartbeatInterval: defaultConfig.HeartbeatInterval,
PeerTimeout: defaultConfig.PeerTimeout,
MaxMessageSize: defaultConfig.MaxMessageSize,
EnableCompression: defaultConfig.EnableCompression,
BufferSize: defaultConfig.BufferSize,
Debug: defaultConfig.Debug,
ReconnectAttempts: defaultConfig.ReconnectAttempts,
ReconnectInterval: defaultConfig.ReconnectInterval,
DataReplication: defaultConfig.DataReplication,
peers: &sync.Map{},
}
}
func ConfigDefault() *Config {
return &Config{
WebSocketEndpoint: "/gun",
StaticPath: "./public",
HeartbeatInterval: 15 * time.Second,
PeerTimeout: 60 * time.Second,
peers: &sync.Map{},
SharedStore: &sync.Map{}, // Add this
}
}
// Validate checks and corrects the configuration values
func (c *Config) Validate() *Config {
if c.WebSocketEndpoint == "" {
c.WebSocketEndpoint = defaultConfig.WebSocketEndpoint
}
if c.StaticPath == "" {
c.StaticPath = defaultConfig.StaticPath
}
if c.HeartbeatInterval <= 0 {
c.HeartbeatInterval = defaultConfig.HeartbeatInterval
}
if c.PeerTimeout <= 0 {
c.PeerTimeout = defaultConfig.PeerTimeout
}
if c.MaxMessageSize <= 0 {
c.MaxMessageSize = defaultConfig.MaxMessageSize
}
if c.BufferSize <= 0 {
c.BufferSize = defaultConfig.BufferSize
}
if c.ReconnectAttempts <= 0 {
c.ReconnectAttempts = defaultConfig.ReconnectAttempts
}
if c.ReconnectInterval <= 0 {
c.ReconnectInterval = defaultConfig.ReconnectInterval
}
if c.peers == nil {
c.peers = &sync.Map{}
}
return c
}
// WithHeartbeat sets the heartbeat interval
func (c *Config) WithHeartbeat(interval time.Duration) *Config {
c.HeartbeatInterval = interval
return c
}
// WithTimeout sets the peer timeout
func (c *Config) WithTimeout(timeout time.Duration) *Config {
c.PeerTimeout = timeout
return c
}
// WithDebug enables or disables debug logging
func (c *Config) WithDebug(enabled bool) *Config {
c.Debug = enabled
return c
}
// WithCompression enables or disables WebSocket compression
func (c *Config) WithCompression(enabled bool) *Config {
c.EnableCompression = enabled
return c
}
// WithBufferSize sets the WebSocket buffer size
func (c *Config) WithBufferSize(size int) *Config {
c.BufferSize = size
return c
}
// WithDataReplication configures data replication
func (c *Config) WithDataReplication(config DataReplicationConfig) *Config {
c.DataReplication = config
return c
}