Skip to content
This repository was archived by the owner on Apr 28, 2024. It is now read-only.

Commit 90ca14a

Browse files
committed
made changes, updated websocket transport lib
1 parent 84fdb09 commit 90ca14a

File tree

3 files changed

+41
-62
lines changed

3 files changed

+41
-62
lines changed

go.mod

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ module go.neonxp.dev/jsonrpc2
33
go 1.18
44

55
require (
6-
github.com/gorilla/websocket v1.5.0
6+
github.com/gobwas/ws v1.1.0
77
github.com/qri-io/jsonschema v0.2.1
88
golang.org/x/sync v0.1.0
99
)
1010

11-
require github.com/qri-io/jsonpointer v0.1.1 // indirect
11+
require (
12+
github.com/gobwas/httphead v0.1.0 // indirect
13+
github.com/gobwas/pool v0.2.1 // indirect
14+
github.com/qri-io/jsonpointer v0.1.1 // indirect
15+
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d // indirect
16+
)

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
3-
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
2+
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
3+
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
4+
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
5+
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
6+
github.com/gobwas/ws v1.1.0 h1:7RFti/xnNkMJnrK7D1yQ/iCIB5OrrY/54/H930kIbHA=
7+
github.com/gobwas/ws v1.1.0/go.mod h1:nzvNcVha5eUziGrbxFCo6qFIojQHjJV5cLYIbezhfL0=
48
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
59
github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA=
610
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
@@ -12,3 +16,5 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
1216
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1317
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
1418
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19+
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d h1:MiWWjyhUzZ+jvhZvloX6ZrUsdEghn8a64Upd8EMHglE=
20+
golang.org/x/sys v0.0.0-20201207223542-d4d67f95c62d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

transport/websockets.go

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,98 +20,67 @@
2020
package transport
2121

2222
import (
23-
"bytes"
2423
"context"
2524
"crypto/tls"
26-
"errors"
27-
"log"
2825
"net"
2926
"net/http"
3027
"time"
3128

32-
"github.com/gorilla/websocket"
29+
websocket "github.com/gobwas/ws"
30+
"github.com/gobwas/ws/wsutil"
3331
)
3432

35-
const (
36-
// Time allowed to write a message to the peer.
37-
writeWait = 10 * time.Second
38-
// Time allowed to read the next pong message from the peer.
39-
pongWait = 60 * time.Second
40-
41-
// Send pings to peer with this period. Must be less than pongWait.
42-
pingPeriod = (pongWait * 9) / 10
43-
44-
// Maximum message size allowed from peer.
45-
maxMessageSize = 512
46-
)
47-
48-
var (
49-
newline = []byte{'\n'}
50-
space = []byte{' '}
51-
errUpgradingConn = errors.New("encountered error upgrading connection to websocket protocol")
52-
errStartingServer = errors.New("encountered error starting http server")
53-
)
54-
55-
var upgrader = websocket.Upgrader{
56-
ReadBufferSize: 1024,
57-
WriteBufferSize: 1024,
58-
CheckOrigin: func(r *http.Request) bool { return true },
59-
}
60-
6133
type WebSocket struct {
62-
Bind string
63-
TLS *tls.Config
64-
CORSOrigin string
65-
Parallel bool
34+
Bind string
35+
TLS *tls.Config
36+
CORSOrigin string
37+
Parallel bool
38+
ReadDeadline, WriteDeadline time.Duration //Set custom timeout for future read and write calls
6639
}
6740

41+
func (ws *WebSocket) WithReadDealine() bool { return ws.ReadDeadline != 0 }
42+
func (ws *WebSocket) WithWriteDealine() bool { return ws.WriteDeadline != 0 }
43+
6844
func (ws *WebSocket) Run(ctx context.Context, resolver Resolver) error {
6945
srv := http.Server{
7046
Addr: ws.Bind,
7147
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
72-
73-
wsconn, err := upgrader.Upgrade(w, r, nil)
48+
wsconn, _, _, err := websocket.UpgradeHTTP(r, w)
7449
if err != nil {
75-
log.Println(err)
7650
return
7751
}
7852

79-
log.Println("successfully upgraded connection")
80-
8153
defer func() {
8254
wsconn.Close()
8355
}()
8456

85-
wsconn.SetReadLimit(maxMessageSize)
86-
wsconn.SetReadDeadline(time.Now().Add(pongWait))
87-
wsconn.SetPongHandler(func(string) error {
88-
wsconn.SetReadDeadline(time.Now().Add(pongWait))
89-
return nil
90-
})
57+
if ws.WithReadDealine() {
58+
wsconn.SetReadDeadline(time.Now().Add(ws.ReadDeadline * time.Second))
59+
}
60+
61+
if ws.WithWriteDealine() {
62+
wsconn.SetWriteDeadline(time.Now().Add(ws.WriteDeadline * time.Second))
63+
}
9164

9265
for {
66+
9367
// read message from connection
94-
messageType, message, err := wsconn.ReadMessage()
68+
_, reader, err := wsutil.NextReader(wsconn, websocket.StateServerSide)
9569
if err != nil {
96-
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
97-
log.Printf("error: %v", err)
98-
}
99-
break
70+
return
10071
}
10172

102-
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1))
73+
// create writer object that implements io.WriterCloser interface
74+
writer := wsutil.NewWriter(wsconn, websocket.StateServerSide, websocket.OpText)
10375

104-
wsconn.SetWriteDeadline(time.Now().Add(writeWait))
76+
resolver.Resolve(ctx, reader, writer, ws.Parallel)
10577

106-
// create writer object that implements io.WriterCloser interface
107-
// messageType is same as the messageType recieved from the connection
108-
w, err := wsconn.NextWriter(messageType)
109-
if err != nil {
78+
if err := writer.Flush(); err != nil {
11079
return
11180
}
11281

113-
resolver.Resolve(ctx, bytes.NewBuffer(message), w, ws.Parallel)
11482
}
83+
11584
}),
11685

11786
BaseContext: func(l net.Listener) context.Context {
@@ -125,7 +94,6 @@ func (ws *WebSocket) Run(ctx context.Context, resolver Resolver) error {
12594
}()
12695

12796
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
128-
log.Println(err)
12997
return err
13098
}
13199
return nil

0 commit comments

Comments
 (0)