-
Notifications
You must be signed in to change notification settings - Fork 151
feat: improved subscription heartbeats #1269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b95182e
90aebcf
d295b5f
b6522eb
73c50f5
c461a8a
fadc6bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,6 @@ const ( | |
DefaultHeartbeatInterval = 5 * time.Second | ||
) | ||
|
||
var ( | ||
multipartHeartbeat = []byte("{}") | ||
) | ||
|
||
// ConnectionIDs is used to create unique connection IDs for each subscription | ||
// Whenever a new connection is created, use this to generate a new ID | ||
// It is public because it can be used in more high level packages to instantiate a new connection | ||
|
@@ -69,7 +65,7 @@ type Resolver struct { | |
|
||
propagateSubgraphErrors bool | ||
propagateSubgraphStatusCodes bool | ||
// Multipart heartbeat interval | ||
// Subscription heartbeat interval | ||
heartbeatInterval time.Duration | ||
// maxSubscriptionFetchTimeout defines the maximum time a subscription fetch can take before it is considered timed out | ||
maxSubscriptionFetchTimeout time.Duration | ||
|
@@ -143,8 +139,8 @@ type ResolverOptions struct { | |
ResolvableOptions ResolvableOptions | ||
// AllowedCustomSubgraphErrorFields defines which fields are allowed in the subgraph error when in passthrough mode | ||
AllowedSubgraphErrorFields []string | ||
// MultipartSubHeartbeatInterval defines the interval in which a heartbeat is sent to all multipart subscriptions | ||
MultipartSubHeartbeatInterval time.Duration | ||
// SubscriptionHeartbeatInterval defines the interval in which a heartbeat is sent to all subscriptions (whether or not this does anything is determined by the subscription response writer) | ||
SubscriptionHeartbeatInterval time.Duration | ||
// MaxSubscriptionFetchTimeout defines the maximum time a subscription fetch can take before it is considered timed out | ||
MaxSubscriptionFetchTimeout time.Duration | ||
// ApolloRouterCompatibilitySubrequestHTTPError is a compatibility flag for Apollo Router, it is used to handle HTTP errors in subrequests differently | ||
|
@@ -158,8 +154,8 @@ func New(ctx context.Context, options ResolverOptions) *Resolver { | |
options.MaxConcurrency = 32 | ||
} | ||
|
||
if options.MultipartSubHeartbeatInterval <= 0 { | ||
options.MultipartSubHeartbeatInterval = DefaultHeartbeatInterval | ||
if options.SubscriptionHeartbeatInterval <= 0 { | ||
options.SubscriptionHeartbeatInterval = DefaultHeartbeatInterval | ||
} | ||
|
||
// We transform the allowed fields into a map for faster lookups | ||
|
@@ -202,7 +198,7 @@ func New(ctx context.Context, options ResolverOptions) *Resolver { | |
triggerUpdateBuf: bytes.NewBuffer(make([]byte, 0, 1024)), | ||
allowedErrorExtensionFields: allowedExtensionFields, | ||
allowedErrorFields: allowedErrorFields, | ||
heartbeatInterval: options.MultipartSubHeartbeatInterval, | ||
heartbeatInterval: options.SubscriptionHeartbeatInterval, | ||
maxSubscriptionFetchTimeout: options.MaxSubscriptionFetchTimeout, | ||
} | ||
resolver.maxConcurrency = make(chan struct{}, options.MaxConcurrency) | ||
|
@@ -310,8 +306,8 @@ func (s *sub) startWorker() { | |
s.startWorkerWithoutHeartbeat() | ||
} | ||
|
||
// startWorkerWithHeartbeat is similar to startWorker but sends heartbeats to the client when | ||
// subscription over multipart is used. It sends a heartbeat to the client every heartbeatInterval. | ||
// startWorkerWithHeartbeat is similar to startWorker but sends heartbeats to the client when enabled. | ||
// It sends a heartbeat to the client every heartbeatInterval. Heartbeats are handled by the SubscriptionResponseWriter interface. | ||
// TODO: Implement a shared timer implementation to avoid creating a new ticker for each subscription. | ||
func (s *sub) startWorkerWithHeartbeat() { | ||
heartbeatTicker := time.NewTicker(s.resolver.heartbeatInterval) | ||
|
@@ -330,7 +326,7 @@ func (s *sub) startWorkerWithHeartbeat() { | |
|
||
return | ||
case <-heartbeatTicker.C: | ||
s.resolver.handleHeartbeat(s, multipartHeartbeat) | ||
s.resolver.handleHeartbeat(s) | ||
case work := <-s.workChan: | ||
work.fn() | ||
|
||
|
@@ -501,7 +497,7 @@ func (r *Resolver) handleEvent(event subscriptionEvent) { | |
} | ||
|
||
// handleHeartbeat sends a heartbeat to the client. It needs to be executed on the same goroutine as the writer. | ||
func (r *Resolver) handleHeartbeat(sub *sub, data []byte) { | ||
func (r *Resolver) handleHeartbeat(sub *sub) { | ||
if r.options.Debug { | ||
fmt.Printf("resolver:heartbeat\n") | ||
} | ||
|
@@ -518,24 +514,16 @@ func (r *Resolver) handleHeartbeat(sub *sub, data []byte) { | |
fmt.Printf("resolver:heartbeat:subscription:%d\n", sub.id.SubscriptionID) | ||
} | ||
|
||
if _, err := sub.writer.Write(data); err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
// If Write fails (e.g. client disconnected), remove the subscription. | ||
_ = r.AsyncUnsubscribeSubscription(sub.id) | ||
return | ||
} | ||
r.asyncErrorWriter.WriteError(sub.ctx, err, nil, sub.writer) | ||
} | ||
err := sub.writer.Flush() | ||
if err != nil { | ||
// If flush fails (e.g. client disconnected), remove the subscription. | ||
if err := sub.writer.Heartbeat(); err != nil { | ||
// If heartbeat fails (e.g. client disconnected), remove the subscription. | ||
_ = r.AsyncUnsubscribeSubscription(sub.id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The question here is, should we really abort the whole subscription for a heartbeat write error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just retains the error from before, a write error is almost always EOF or context cancelled or etc, meaning the client is gone There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before, we unsubscribed only on client cancellation or flush error. |
||
return | ||
} | ||
|
||
if r.options.Debug { | ||
fmt.Printf("resolver:heartbeat:subscription:flushed:%d\n", sub.id.SubscriptionID) | ||
fmt.Printf("resolver:heartbeat:subscription:done:%d\n", sub.id.SubscriptionID) | ||
} | ||
|
||
if r.reporter != nil { | ||
r.reporter.SubscriptionUpdateSent() | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the approach to move it to the router 👍