Skip to content

Commit

Permalink
feat: support compression
Browse files Browse the repository at this point in the history
This commit adds the ability to enable compression for websockets.
The compression is disabled by default due to the fact that it is marked
as experimental in gorilla/websocket and there have been issues with
compression whenever Safari is enabled.
This commit partially addresses #30.
  • Loading branch information
alberts-s committed Jun 17, 2024
1 parent 10238cd commit 9e6c5de
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Support for websocket compression - disabled by default (#40)
### Changed
### Fixed
### Docs
Expand Down
1 change: 1 addition & 0 deletions config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ webserver:
domains_only_url: "/domains-only"
cert_path: ""
cert_key_path: ""
compression_enabled: false

prometheus:
enabled: true
Expand Down
20 changes: 16 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ type ServerConfig struct {

type Config struct {
Webserver struct {
ServerConfig `yaml:",inline"`
FullURL string `yaml:"full_url"`
LiteURL string `yaml:"lite_url"`
DomainsOnlyURL string `yaml:"domains_only_url"`
ServerConfig `yaml:",inline"`
FullURL string `yaml:"full_url"`
LiteURL string `yaml:"lite_url"`
DomainsOnlyURL string `yaml:"domains_only_url"`
CompressionEnabled *bool `yaml:"compression_enabled,omitempty"`
}
Prometheus struct {
ServerConfig `yaml:",inline"`
Expand Down Expand Up @@ -134,6 +135,17 @@ func validateConfig(config Config) bool {
return false
}

if config.Webserver.CompressionEnabled == nil {
// On one hand the Calidog certstream server does support compression by default, however
// the gorilla/websocket library has had issues with Safari clients.
// See: https://github.com/gorilla/websocket/issues/731
// Moreover according to gorilla/weboscket documentation, the support is experimental
// See: https://pkg.go.dev/github.com/gorilla/websocket#hdr-Compression_EXPERIMENTAL
// Thus lets default to false - i.e. disable compression by default
CompressionEnabled := false
config.Webserver.CompressionEnabled = &CompressionEnabled
}

if config.Webserver.FullURL == "" || !URLRegex.MatchString(config.Webserver.FullURL) {
log.Println("Webhook full URL is not set or does not match pattern '/...'")
config.Webserver.FullURL = "/full-stream"
Expand Down
9 changes: 7 additions & 2 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

var (
ClientHandler = BroadcastManager{}
upgrader = websocket.Upgrader{} // use default options
upgrader websocket.Upgrader
)

type WebServer struct {
Expand Down Expand Up @@ -248,7 +248,8 @@ func NewMetricsServer(networkIf string, port int, certPath, keyPath string) *Web
}

// NewWebsocketServer starts a new webserver and initialized it with the necessary routes.
// It also starts the broadcaster in ClientHandler as a background job.
// It also starts the broadcaster in ClientHandler as a background job and takes care of
// setting up websocket.Upgrader.
func NewWebsocketServer(networkIf string, port int, certPath, keyPath string) *WebServer {
server := &WebServer{
networkIf: networkIf,
Expand All @@ -258,6 +259,10 @@ func NewWebsocketServer(networkIf string, port int, certPath, keyPath string) *W
keyPath: keyPath,
}

upgrader = websocket.Upgrader{
EnableCompression: *config.AppConfig.Webserver.CompressionEnabled,
}

if config.AppConfig.Webserver.RealIP {
server.routes.Use(middleware.RealIP)
}
Expand Down

0 comments on commit 9e6c5de

Please sign in to comment.