From 18b11707381b1462b4761f7c3f4b421fc6fad745 Mon Sep 17 00:00:00 2001 From: Philipp Seith <6890503+philippseith@users.noreply.github.com> Date: Mon, 30 Aug 2021 19:02:58 +0200 Subject: [PATCH] new: Hub.Context / HubContext.Context By giving access to HubConnection.Context in Hub, derived hubs can cancel goroutines when the connection has been closed. --- hub.go | 10 +++++++++- hubcontext.go | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hub.go b/hub.go index a9836d43..d8a61858 100644 --- a/hub.go +++ b/hub.go @@ -1,6 +1,9 @@ package signalr -import "sync" +import ( + "context" + "sync" +) // HubInterface is a hubs interface type HubInterface interface { @@ -42,6 +45,11 @@ func (h *Hub) ConnectionID() string { return h.context.ConnectionID() } +// Context is the context.Context of the current connection +func (h *Hub) Context() context.Context { + return h.context.Context() +} + // Abort aborts the current connection func (h *Hub) Abort() { h.context.Abort() diff --git a/hubcontext.go b/hubcontext.go index 49d76a6b..0a4f977b 100644 --- a/hubcontext.go +++ b/hubcontext.go @@ -17,6 +17,7 @@ type HubContext interface { Groups() GroupManager Items() *sync.Map ConnectionID() string + Context() context.Context Abort() Logger() (info StructuredLogger, dbg StructuredLogger) } @@ -46,6 +47,10 @@ func (c *connectionHubContext) ConnectionID() string { return c.connection.ConnectionID() } +func (c *connectionHubContext) Context() context.Context { + return c.connection.Context() +} + func (c *connectionHubContext) Abort() { c.abort() }