diff options
author | Michael Muré <michael.mure@consensys.net> | 2019-03-31 21:44:14 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-04-01 14:03:13 +0200 |
commit | 15c258cdc4ba37820362a44dfc2636ed1ff92b4c (patch) | |
tree | 0790e36bf4846b12b367f12670540eec6ee023f3 /vendor/github.com/99designs/gqlgen/handler/websocket.go | |
parent | e028b895aa21553937d732044b7bb0a15c5276f6 (diff) | |
download | git-bug-15c258cdc4ba37820362a44dfc2636ed1ff92b4c.tar.gz |
graphql: expose allIdentities, identities and userIdentity in the repo
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/handler/websocket.go')
-rw-r--r-- | vendor/github.com/99designs/gqlgen/handler/websocket.go | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/vendor/github.com/99designs/gqlgen/handler/websocket.go b/vendor/github.com/99designs/gqlgen/handler/websocket.go index dae262bd..09800c17 100644 --- a/vendor/github.com/99designs/gqlgen/handler/websocket.go +++ b/vendor/github.com/99designs/gqlgen/handler/websocket.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "sync" + "time" "github.com/99designs/gqlgen/graphql" "github.com/gorilla/websocket" @@ -27,7 +28,7 @@ const ( dataMsg = "data" // Server -> Client errorMsg = "error" // Server -> Client completeMsg = "complete" // Server -> Client - //connectionKeepAliveMsg = "ka" // Server -> Client TODO: keepalives + connectionKeepAliveMsg = "ka" // Server -> Client ) type operationMessage struct { @@ -37,12 +38,13 @@ type operationMessage struct { } type wsConnection struct { - ctx context.Context - conn *websocket.Conn - exec graphql.ExecutableSchema - active map[string]context.CancelFunc - mu sync.Mutex - cfg *Config + ctx context.Context + conn *websocket.Conn + exec graphql.ExecutableSchema + active map[string]context.CancelFunc + mu sync.Mutex + cfg *Config + keepAliveTicker *time.Ticker initPayload InitPayload } @@ -109,6 +111,20 @@ func (c *wsConnection) write(msg *operationMessage) { } func (c *wsConnection) run() { + // We create a cancellation that will shutdown the keep-alive when we leave + // this function. + ctx, cancel := context.WithCancel(c.ctx) + defer cancel() + + // Create a timer that will fire every interval to keep the connection alive. + if c.cfg.connectionKeepAlivePingInterval != 0 { + c.mu.Lock() + c.keepAliveTicker = time.NewTicker(c.cfg.connectionKeepAlivePingInterval) + c.mu.Unlock() + + go c.keepAlive(ctx) + } + for { message := c.readOp() if message == nil { @@ -141,6 +157,18 @@ func (c *wsConnection) run() { } } +func (c *wsConnection) keepAlive(ctx context.Context) { + for { + select { + case <-ctx.Done(): + c.keepAliveTicker.Stop() + return + case <-c.keepAliveTicker.C: + c.write(&operationMessage{Type: connectionKeepAliveMsg}) + } + } +} + func (c *wsConnection) subscribe(message *operationMessage) bool { var reqParams params if err := jsonDecode(bytes.NewReader(message.Payload), &reqParams); err != nil { |