-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgobwas_upgrader.go
50 lines (41 loc) · 1.38 KB
/
gobwas_upgrader.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
package wspubsub
import (
"net/http"
"time"
"github.com/gobwas/ws"
)
var _ WebsocketConnectionUpgrader = (*GobwasConnectionUpgrader)(nil)
// GobwasConnectionUpgrader is an implementation of WebsocketConnectionUpgrader.
type GobwasConnectionUpgrader struct {
logger Logger
options GobwasConnectionUpgraderOptions
}
// GobwasConnectionUpgrader upgrades HTTP connection to the WebSocket connection.
func (u *GobwasConnectionUpgrader) Upgrade(w http.ResponseWriter, r *http.Request) (WebsocketConnection, error) {
if u.options.IsDebug {
now := time.Now()
defer func() {
end := time.Since(now)
if end > u.options.DebugFuncTimeLimit {
u.logger.Warnf("gobwas.connection_upgrader.upgrader: took=%s", end)
}
}()
}
connection, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
return nil, err
}
gobwasConnection := &GobwasConnection{
conn: connection,
logger: u.logger,
readTimeout: u.options.ReadTimout,
wrightTimout: u.options.WriteTimout,
IsDebug: u.options.IsDebug,
DebugFuncTimeLimit: u.options.DebugFuncTimeLimit,
}
return gobwasConnection, nil
}
// NewGobwasConnectionUpgrader initializes a new GobwasConnectionUpgrader.
func NewGobwasConnectionUpgrader(options GobwasConnectionUpgraderOptions, logger Logger) *GobwasConnectionUpgrader {
return &GobwasConnectionUpgrader{options: options, logger: logger}
}