Skip to content

Commit c71e2a6

Browse files
authored
Merge pull request #58 from d-Rickyy-b/reduce-memory-footprint
feat: make buffer sizes configurable
2 parents 02e827e + c86cd4e commit c71e2a6

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99
### Added
10+
- New configuration for buffer sizes (#58)
1011
### Changed
1112
### Fixed
1213
### Docs
@@ -17,9 +18,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1718
- Support for non-browsers by implementing server initiated heartbeats (#39)
1819
- Start new ct-watchers as new ct logs become available (#42)
1920
- More logging to document currently watched logs (03d878e)
21+
2022
### Changed
2123
- Changed log output to be better grepable (5c055cc)
2224
- Update ct log update interval to once per hour instead of once per 6 hours as previously (9b6e77d)
25+
2326
### Fixed
2427
- Fixed a possible race condition when accessing metrics
2528

config.sample.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,12 @@ general:
2828
- url: https://dodo.ct.comodo.com/
2929
operator: "Comodo"
3030
description: "Comodo Dodo"
31+
# To optimize the performance of the server, you can overwrite the size of different buffers
32+
# For low CPU, low memory machines, you should reduce the buffer sizes to save memory in case the CPU is maxed.
33+
buffer_sizes:
34+
# Buffer for each websocket connection
35+
websocket: 300
36+
# Buffer for each CT log connection
37+
ctlog: 1000
38+
# Combined buffer for the broadcast manager
39+
broadcastmanager: 10000

internal/certificatetransparency/ct-watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (w *worker) runWorker(ctx context.Context) error {
238238
Matcher: scanner.MatchAll{},
239239
PrecertOnly: false,
240240
NumWorkers: 1,
241-
BufferSize: 1000,
241+
BufferSize: config.AppConfig.General.BufferSizes.CTLog,
242242
})
243243

244244
scanErr := certScanner.Scan(ctx, w.foundCertCallback, w.foundPrecertCallback)

internal/config/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ type LogConfig struct {
3131
Description string `yaml:"description"`
3232
}
3333

34+
type BufferSizes struct {
35+
Websocket int `yaml:"websocket"`
36+
CTLog int `yaml:"ctlog"`
37+
BroadcastManager int `yaml:"broadcastmanager"`
38+
}
39+
3440
type Config struct {
3541
Webserver struct {
3642
ServerConfig `yaml:",inline"`
@@ -47,6 +53,7 @@ type Config struct {
4753
}
4854
General struct {
4955
AdditionalLogs []LogConfig `yaml:"additional_logs"`
56+
BufferSizes BufferSizes `yaml:"buffer_sizes"`
5057
}
5158
}
5259

@@ -211,5 +218,17 @@ func validateConfig(config *Config) bool {
211218

212219
config.General.AdditionalLogs = validLogs
213220

221+
if config.General.BufferSizes.Websocket <= 0 {
222+
config.General.BufferSizes.Websocket = 300
223+
}
224+
225+
if config.General.BufferSizes.CTLog <= 0 {
226+
config.General.BufferSizes.CTLog = 1000
227+
}
228+
229+
if config.General.BufferSizes.BroadcastManager <= 0 {
230+
config.General.BufferSizes.BroadcastManager = 10000
231+
}
232+
214233
return true
215234
}

internal/web/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func upgradeConnection(w http.ResponseWriter, r *http.Request) (*websocket.Conn,
168168

169169
// setupClient initializes a client struct and starts the broadcastHandler and websocket listener.
170170
func setupClient(connection *websocket.Conn, subscriptionType SubscriptionType, name string) {
171-
c := newClient(connection, subscriptionType, name, 300)
171+
c := newClient(connection, subscriptionType, name, config.AppConfig.General.BufferSizes.Websocket)
172172
go c.broadcastHandler()
173173
go c.listenWebsocket()
174174

@@ -275,7 +275,7 @@ func NewWebsocketServer(networkIf string, port int, certPath, keyPath string) *W
275275
setupWebsocketRoutes(server.routes)
276276
server.initServer()
277277

278-
ClientHandler.Broadcast = make(chan models.Entry, 10_000)
278+
ClientHandler.Broadcast = make(chan models.Entry, config.AppConfig.General.BufferSizes.BroadcastManager)
279279
go ClientHandler.broadcaster()
280280

281281
return server

0 commit comments

Comments
 (0)