Skip to content
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

fix: delete leftover heartbeat connections #1033

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 48 additions & 64 deletions v2/pkg/engine/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Resolver struct {
maxConcurrency chan struct{}

triggers map[uint64]*trigger
heartbeatSubLock *sync.Mutex
heartbeatSubscriptions map[*Context]*sub
events chan subscriptionEvent
triggerEventsSem *semaphore.Weighted
Expand Down Expand Up @@ -189,6 +190,7 @@ func New(ctx context.Context, options ResolverOptions) *Resolver {
propagateSubgraphStatusCodes: options.PropagateSubgraphStatusCodes,
events: make(chan subscriptionEvent),
triggers: make(map[uint64]*trigger),
heartbeatSubLock: &sync.Mutex{},
heartbeatSubscriptions: make(map[*Context]*sub),
reporter: options.Reporter,
asyncErrorWriter: options.AsyncErrorWriter,
Expand Down Expand Up @@ -407,6 +409,9 @@ func (r *Resolver) handleEvent(event subscriptionEvent) {
}

func (r *Resolver) handleHeartbeat(data []byte) {
r.heartbeatSubLock.Lock()
defer r.heartbeatSubLock.Unlock()

if r.options.Debug {
fmt.Printf("resolver:heartbeat:%d\n", len(r.heartbeatSubscriptions))
}
Expand All @@ -417,7 +422,7 @@ func (r *Resolver) handleHeartbeat(data []byte) {
s.mux.Lock()
skipHeartbeat := now.Sub(s.lastWrite) < r.multipartSubHeartbeatInterval
s.mux.Unlock()
if skipHeartbeat {
if skipHeartbeat || (c.Context().Err() != nil && errors.Is(c.Context().Err(), context.Canceled)) {
continue
}

Expand All @@ -428,6 +433,12 @@ func (r *Resolver) handleHeartbeat(data []byte) {

s.mux.Lock()
if _, err := s.writer.Write(data); err != nil {
if errors.Is(err, context.Canceled) {
// client disconnected
s.mux.Unlock()
_ = r.AsyncUnsubscribeSubscription(s.id)
return
}
r.asyncErrorWriter.WriteError(c, err, nil, s.writer)
}
err := s.writer.Flush()
Expand Down Expand Up @@ -468,30 +479,7 @@ func (r *Resolver) handleTriggerInitialized(triggerID uint64) {
}

func (r *Resolver) handleTriggerDone(triggerID uint64) {
trig, ok := r.triggers[triggerID]
if !ok {
return
}
isInitialized := trig.initialized
wg := trig.inFlight
subscriptionCount := len(trig.subscriptions)

delete(r.triggers, triggerID)

go func() {
if wg != nil {
wg.Wait()
}
for _, s := range trig.subscriptions {
s.writer.Complete()
}
if r.reporter != nil {
r.reporter.SubscriptionCountDec(subscriptionCount)
if isInitialized {
r.reporter.TriggerCountDec(1)
}
}
}()
r.shutdownTrigger(triggerID)
}

func (r *Resolver) handleAddSubscription(triggerID uint64, add *addSubscription) {
Expand All @@ -510,7 +498,9 @@ func (r *Resolver) handleAddSubscription(triggerID uint64, add *addSubscription)
executor: add.executor,
}
if add.ctx.ExecutionOptions.SendHeartbeat {
r.heartbeatSubLock.Lock()
r.heartbeatSubscriptions[add.ctx] = s
r.heartbeatSubLock.Unlock()
}
trig, ok := r.triggers[triggerID]
if ok {
Expand Down Expand Up @@ -636,20 +626,9 @@ func (r *Resolver) handleRemoveSubscription(id SubscriptionIdentifier) {
removed := 0
for u := range r.triggers {
trig := r.triggers[u]
for ctx, s := range trig.subscriptions {
if s.id == id {

if ctx.Context().Err() == nil {
s.writer.Complete()
}
delete(r.heartbeatSubscriptions, ctx)
delete(trig.subscriptions, ctx)
if r.options.Debug {
fmt.Printf("resolver:trigger:subscription:removed:%d:%d\n", trig.id, id.SubscriptionID)
}
removed++
}
}
removed += r.shutdownTriggerSubscriptions(u, func(sID SubscriptionIdentifier) bool {
return sID == id
})
if len(trig.subscriptions) == 0 {
r.shutdownTrigger(trig.id)
}
Expand All @@ -665,20 +644,9 @@ func (r *Resolver) handleRemoveClient(id int64) {
}
removed := 0
for u := range r.triggers {
for c, s := range r.triggers[u].subscriptions {
if s.id.ConnectionID == id && !s.id.internal {

if c.Context().Err() == nil {
s.writer.Complete()
}

delete(r.triggers[u].subscriptions, c)
if r.options.Debug {
fmt.Printf("resolver:trigger:subscription:done:%d:%d\n", u, s.id.SubscriptionID)
}
removed++
}
}
removed += r.shutdownTriggerSubscriptions(u, func(sID SubscriptionIdentifier) bool {
return sID.ConnectionID == id && !sID.internal
})
if len(r.triggers[u].subscriptions) == 0 {
r.shutdownTrigger(r.triggers[u].id)
}
Expand Down Expand Up @@ -739,30 +707,46 @@ func (r *Resolver) shutdownTrigger(id uint64) {
return
}
count := len(trig.subscriptions)
r.shutdownTriggerSubscriptions(id, nil)
trig.cancel()
delete(r.triggers, id)
if r.options.Debug {
fmt.Printf("resolver:trigger:done:%d\n", trig.id)
}
if r.reporter != nil {
r.reporter.SubscriptionCountDec(count)
if trig.initialized {
r.reporter.TriggerCountDec(1)
}
}
}

func (r *Resolver) shutdownTriggerSubscriptions(id uint64, shutdownMatcher func(a SubscriptionIdentifier) bool) int {
trig, ok := r.triggers[id]
if !ok {
return 0
}
removed := 0
for c, s := range trig.subscriptions {
if shutdownMatcher != nil && !shutdownMatcher(s.id) {
continue
}
if c.Context().Err() == nil {
s.writer.Complete()
}
if s.completed != nil {
close(s.completed)
}
r.heartbeatSubLock.Lock()
delete(r.heartbeatSubscriptions, c)
r.heartbeatSubLock.Unlock()
delete(trig.subscriptions, c)
if r.options.Debug {
fmt.Printf("resolver:trigger:subscription:done:%d:%d\n", trig.id, s.id.SubscriptionID)
}
removed++
}
trig.cancel()
delete(r.triggers, id)
if r.options.Debug {
fmt.Printf("resolver:trigger:done:%d\n", trig.id)
}
if r.reporter != nil {
r.reporter.SubscriptionCountDec(count)
if trig.initialized {
r.reporter.TriggerCountDec(1)
}
}
return removed
}

func (r *Resolver) handleShutdown() {
Expand Down
19 changes: 14 additions & 5 deletions v2/pkg/engine/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ func (t *TestErrorWriter) WriteError(ctx *Context, err error, res *GraphQLRespon
}
}

var multipartSubHeartbeatInterval = 15 * time.Millisecond

func newResolver(ctx context.Context) *Resolver {
return New(ctx, ResolverOptions{
MaxConcurrency: 1024,
Debug: false,
PropagateSubgraphErrors: true,
PropagateSubgraphStatusCodes: true,
AsyncErrorWriter: &TestErrorWriter{},
MaxConcurrency: 1024,
Debug: false,
PropagateSubgraphErrors: true,
PropagateSubgraphStatusCodes: true,
AsyncErrorWriter: &TestErrorWriter{},
MultipartSubHeartbeatInterval: multipartSubHeartbeatInterval,
})
}

Expand Down Expand Up @@ -5164,12 +5167,18 @@ func TestResolver_ResolveGraphQLSubscription(t *testing.T) {

ctx := &Context{
ctx: context.Background(),
ExecutionOptions: ExecutionOptions{
SendHeartbeat: true,
},
}

err := resolver.AsyncResolveGraphQLSubscription(ctx, plan, recorder, id)
assert.NoError(t, err)

recorder.AwaitComplete(t, defaultTimeout)
assert.Equal(t, 3, len(recorder.Messages()))
time.Sleep(2 * resolver.multipartSubHeartbeatInterval)
// Validate that despite the time, we don't see any heartbeats sent
assert.ElementsMatch(t, []string{
`{"data":{"counter":0}}`,
`{"data":{"counter":1}}`,
Expand Down
Loading