Skip to content

Commit

Permalink
Merge 47c84b5 into cb5ce1f
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Aug 5, 2019
2 parents cb5ce1f + 47c84b5 commit e8210ad
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
3 changes: 3 additions & 0 deletions main.go
Expand Up @@ -36,6 +36,8 @@ Server Options:
Common Options:
-h, --help Show this message
Configuration Documentation: https://resgate.io/docs/get-started/configuration/
`

// Config holds server configuration
Expand Down Expand Up @@ -90,6 +92,7 @@ func (c *Config) Init(fs *flag.FlagSet, args []string) error {
fs.StringVar(&c.APIEncoding, "apiencoding", "", "Encoding for web resources.")
fs.IntVar(&c.RequestTimeout, "r", 0, "Timeout in milliseconds for NATS requests.")
fs.IntVar(&c.RequestTimeout, "reqtimeout", 0, "Timeout in milliseconds for NATS requests.")
fs.BoolVar(&c.Debug, "debug", false, "Enable debugging.")

if err := fs.Parse(args); err != nil {
printAndDie(fmt.Sprintf("error parsing arguments: %s", err.Error()), true)
Expand Down
2 changes: 2 additions & 0 deletions server/config.go
Expand Up @@ -21,6 +21,8 @@ type Config struct {
TLSCert string `json:"certFile"`
TLSKey string `json:"keyFile"`

WSCompression bool `json:"wsCompression"`

NoHTTP bool `json:"-"` // Disable start of the HTTP server. Used for testing

scheme string
Expand Down
6 changes: 4 additions & 2 deletions server/service.go
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"sync"

"github.com/gorilla/websocket"
"github.com/resgateio/resgate/logger"
"github.com/resgateio/resgate/server/mq"
"github.com/resgateio/resgate/server/rescache"
Expand All @@ -26,8 +27,9 @@ type Service struct {
enc APIEncoder

// wsListener/wsConn
conns map[string]*wsConn // Connections by wsConn Id's
wg sync.WaitGroup // Wait for all connections to be disconnected
upgrader websocket.Upgrader
conns map[string]*wsConn // Connections by wsConn Id's
wg sync.WaitGroup // Wait for all connections to be disconnected
}

// NewService creates a new Service
Expand Down
18 changes: 9 additions & 9 deletions server/wsHandler.go
Expand Up @@ -8,17 +8,17 @@ import (
"github.com/gorilla/websocket"
)

var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}

var wsDisconnectTimeout = 3 * time.Second

func (s *Service) initWSHandler() error {
s.upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
EnableCompression: s.cfg.WSCompression,
}
s.conns = make(map[string]*wsConn)
return nil
}
Expand All @@ -31,7 +31,7 @@ func (s *Service) GetWSHandlerFunc() http.Handler {

func (s *Service) wsHandler(w http.ResponseWriter, r *http.Request) {
// Upgrade to gorilla websocket
ws, err := upgrader.Upgrade(w, r, nil)
ws, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
s.Debugf("Failed to upgrade connection from %s: %s", r.RemoteAddr, err.Error())
return
Expand Down
17 changes: 17 additions & 0 deletions test/19websocket_compression_test.go
@@ -0,0 +1,17 @@
package test

import (
"testing"

"github.com/resgateio/resgate/server"
)

// Test subscribing to a resource with WebSocket compression enabled
func TestWebSocketCompressionEnabled(t *testing.T) {
runTest(t, func(s *Session) {
c := s.Connect()
subscribeToTestModel(t, s, c)
}, func(c *server.Config) {
c.WSCompression = true
})
}

0 comments on commit e8210ad

Please sign in to comment.