Skip to content

Commit

Permalink
feat: expose transport options on the graphqlws package (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored Apr 13, 2022
1 parent 0a02bd6 commit c0f9c99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
20 changes: 19 additions & 1 deletion graphqlws/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"time"

"github.com/gorilla/websocket"

Expand Down Expand Up @@ -54,6 +55,7 @@ type Option interface {

type options struct {
contextGenerators []ContextGenerator
connectOptions []connection.Option
}

type optionFunc func(*options)
Expand All @@ -70,6 +72,22 @@ func WithContextGenerator(f ContextGenerator) Option {
})
}

// WithReadLimit limits the maximum size of incoming messages
func WithReadLimit(limit int64) Option {
return optionFunc(func(o *options) {
connOpt := connection.ReadLimit(limit)
o.connectOptions = append(o.connectOptions, connOpt)
})
}

// WithWriteTimeout sets a timeout for outgoing messages
func WithWriteTimeout(d time.Duration) Option {
return optionFunc(func(o *options) {
connOpt := connection.WriteTimeout(d)
o.connectOptions = append(o.connectOptions, connOpt)
})
}

func applyOptions(opts ...Option) *options {
var o options

Expand Down Expand Up @@ -115,7 +133,7 @@ func (h *handler) NewHandlerFunc(svc connection.GraphQLService, httpHandler http
return
}

go connection.Connect(ctx, ws, svc)
go connection.Connect(ctx, ws, svc, o.connectOptions...)
return
}

Expand Down
12 changes: 7 additions & 5 deletions graphqlws/internal/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,31 @@ func (o *operationMap) delete(name string) {
o.mtx.Unlock()
}

type Option func(conn *connection)

// ReadLimit limits the maximum size of incoming messages
func ReadLimit(limit int64) func(conn *connection) {
func ReadLimit(limit int64) Option {
return func(conn *connection) {
conn.ws.SetReadLimit(limit)
}
}

// WriteTimeout sets a timeout for outgoing messages
func WriteTimeout(d time.Duration) func(conn *connection) {
func WriteTimeout(d time.Duration) Option {
return func(conn *connection) {
conn.writeTimeout = d
}
}

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

defaultOpts := []func(conn *connection){
defaultOpts := []Option{
ReadLimit(4096),
WriteTimeout(time.Second),
}
Expand Down Expand Up @@ -202,7 +204,7 @@ func (conn *connection) addSubscription(ctx context.Context,
case <-t:
// setup timed out
ops.delete(message.ID)
ep := errPayload(errors.New("subscription connect timeout"))
ep := errPayload(fmt.Errorf("server subscription connect timeout after %s", conn.writeTimeout))
send(message.ID, typeError, ep)
send(message.ID, typeComplete, nil)
kill <- true
Expand Down

0 comments on commit c0f9c99

Please sign in to comment.