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: simplify WaitForCleanup #1747

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 3 additions & 15 deletions internal/api/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"context"
"sync"

"github.com/supabase/auth/internal/utilities"
)

var (
Expand All @@ -12,19 +14,5 @@ var (
// WaitForCleanup waits until all API servers are shut down cleanly or until
// the provided context signals done, whichever comes first.
func WaitForCleanup(ctx context.Context) {
cleanupDone := make(chan struct{})

go func() {
defer close(cleanupDone)

cleanupWaitGroup.Wait()
}()

select {
case <-ctx.Done():
return

case <-cleanupDone:
return
}
utilities.WaitForCleanup(ctx, &cleanupWaitGroup)
}
18 changes: 3 additions & 15 deletions internal/observability/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package observability
import (
"context"
"sync"

"github.com/supabase/auth/internal/utilities"
)

var (
Expand All @@ -12,19 +14,5 @@ var (
// WaitForCleanup waits until all observability long-running goroutines shut
// down cleanly or until the provided context signals done.
func WaitForCleanup(ctx context.Context) {
cleanupDone := make(chan struct{})

go func() {
defer close(cleanupDone)

cleanupWaitGroup.Wait()
}()

select {
case <-ctx.Done():
return

case <-cleanupDone:
return
}
utilities.WaitForCleanup(ctx, &cleanupWaitGroup)
}
25 changes: 24 additions & 1 deletion internal/utilities/context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utilities

import "context"
import (
"context"
"sync"
)

type contextKey string

Expand All @@ -26,3 +29,23 @@ func GetRequestID(ctx context.Context) string {

return obj.(string)
}

// WaitForCleanup waits until all long-running goroutines shut
// down cleanly or until the provided context signals done.
func WaitForCleanup(ctx context.Context, wg *sync.WaitGroup) {
cleanupDone := make(chan struct{})

go func() {
defer close(cleanupDone)

wg.Wait()
}()

select {
case <-ctx.Done():
return

case <-cleanupDone:
return
}
}
Loading