Skip to content

Commit c0f9c99

Browse files
authored
feat: expose transport options on the graphqlws package (#37)
1 parent 0a02bd6 commit c0f9c99

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

graphqlws/http.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"time"
78

89
"github.com/gorilla/websocket"
910

@@ -54,6 +55,7 @@ type Option interface {
5455

5556
type options struct {
5657
contextGenerators []ContextGenerator
58+
connectOptions []connection.Option
5759
}
5860

5961
type optionFunc func(*options)
@@ -70,6 +72,22 @@ func WithContextGenerator(f ContextGenerator) Option {
7072
})
7173
}
7274

75+
// WithReadLimit limits the maximum size of incoming messages
76+
func WithReadLimit(limit int64) Option {
77+
return optionFunc(func(o *options) {
78+
connOpt := connection.ReadLimit(limit)
79+
o.connectOptions = append(o.connectOptions, connOpt)
80+
})
81+
}
82+
83+
// WithWriteTimeout sets a timeout for outgoing messages
84+
func WithWriteTimeout(d time.Duration) Option {
85+
return optionFunc(func(o *options) {
86+
connOpt := connection.WriteTimeout(d)
87+
o.connectOptions = append(o.connectOptions, connOpt)
88+
})
89+
}
90+
7391
func applyOptions(opts ...Option) *options {
7492
var o options
7593

@@ -115,7 +133,7 @@ func (h *handler) NewHandlerFunc(svc connection.GraphQLService, httpHandler http
115133
return
116134
}
117135

118-
go connection.Connect(ctx, ws, svc)
136+
go connection.Connect(ctx, ws, svc, o.connectOptions...)
119137
return
120138
}
121139

graphqlws/internal/connection/connection.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,31 @@ func (o *operationMap) delete(name string) {
9393
o.mtx.Unlock()
9494
}
9595

96+
type Option func(conn *connection)
97+
9698
// ReadLimit limits the maximum size of incoming messages
97-
func ReadLimit(limit int64) func(conn *connection) {
99+
func ReadLimit(limit int64) Option {
98100
return func(conn *connection) {
99101
conn.ws.SetReadLimit(limit)
100102
}
101103
}
102104

103105
// WriteTimeout sets a timeout for outgoing messages
104-
func WriteTimeout(d time.Duration) func(conn *connection) {
106+
func WriteTimeout(d time.Duration) Option {
105107
return func(conn *connection) {
106108
conn.writeTimeout = d
107109
}
108110
}
109111

110112
// Connect implements the apollographql subscriptions-transport-ws [email protected]
111113
// https://github.com/apollographql/subscriptions-transport-ws/blob/v0.9.4/PROTOCOL.md
112-
func Connect(ctx context.Context, ws wsConnection, service GraphQLService, options ...func(conn *connection)) func() {
114+
func Connect(ctx context.Context, ws wsConnection, service GraphQLService, options ...Option) func() {
113115
conn := &connection{
114116
service: service,
115117
ws: ws,
116118
}
117119

118-
defaultOpts := []func(conn *connection){
120+
defaultOpts := []Option{
119121
ReadLimit(4096),
120122
WriteTimeout(time.Second),
121123
}
@@ -202,7 +204,7 @@ func (conn *connection) addSubscription(ctx context.Context,
202204
case <-t:
203205
// setup timed out
204206
ops.delete(message.ID)
205-
ep := errPayload(errors.New("subscription connect timeout"))
207+
ep := errPayload(fmt.Errorf("server subscription connect timeout after %s", conn.writeTimeout))
206208
send(message.ID, typeError, ep)
207209
send(message.ID, typeComplete, nil)
208210
kill <- true

0 commit comments

Comments
 (0)