-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
74 lines (63 loc) · 2.19 KB
/
proxy.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
package interruptible_websocket_proxy
import (
"fmt"
"github.com/google/uuid"
"golang.org/x/net/websocket"
"strings"
)
// InterruptibleWebsocketProxyHandler Wrapped [Golang websocket server](https://pkg.go.dev/golang.org/x/net/websocket#Server)
type InterruptibleWebsocketProxyHandler struct {
websocket.Server
*WebsocketPipeManager
}
// HandlerConfig Configuration for the proxy and websocket handler
type HandlerConfig struct {
MaxIdleConnCount int64
MaxAllowedErrorCountPerConn int64
InterruptMemoryLimitPerConnInBytes int
ClientIdExtractFunc func(conn *websocket.Conn) (uuid.UUID, error)
}
// NewInterruptibleWebsocketProxyHandler Returns a readily configured websocket handler. The returned handler can be
// directly registered to a http web server.
//
// E.g:
// mux := http.NewServeMux()
// mux.Handle("/", websocketServer)
//
// // Start webserver
// err = http.ListenAndServe(":8080", mux)
// if err != nil {
// lgr.Error("error starting websocket server", err)
// return
// }
func NewInterruptibleWebsocketProxyHandler(wsConfig websocket.Config,
handlerConfig HandlerConfig, logger logger) *InterruptibleWebsocketProxyHandler {
pool := NewBackendConnPool(handlerConfig.MaxIdleConnCount, handlerConfig.MaxAllowedErrorCountPerConn, logger)
pipeManager := NewWebsocketPipeManager(pool, handlerConfig.InterruptMemoryLimitPerConnInBytes, logger)
var proxyWSHandler = websocket.Handler(func(conn *websocket.Conn) {
defer conn.Close()
var clientId uuid.UUID
var err error
if handlerConfig.ClientIdExtractFunc != nil {
clientId, err = handlerConfig.ClientIdExtractFunc(conn)
} else {
clientIdString := strings.TrimPrefix(conn.Request().URL.Path, "/")
clientId, err = uuid.Parse(clientIdString)
}
if err != nil {
logger.Error(fmt.Sprintf("error extracting clientId"), err)
return
}
// Create persistent pipe, this is a blocking call
err = pipeManager.CreatePipe(clientId, conn)
if err != nil {
logger.Error("error creating persistent pipe", err)
return
}
})
websocketServer := websocket.Server{
Config: wsConfig,
Handler: proxyWSHandler,
}
return &InterruptibleWebsocketProxyHandler{websocketServer, pipeManager}
}