Skip to content

Commit

Permalink
fix: simplify WaitForCleanup (#1747)
Browse files Browse the repository at this point in the history
Deduplicate the `WaitForCleanup` function into one under `utilities`.
  • Loading branch information
LashaJini authored Sep 2, 2024
1 parent f3a28d1 commit 0084625
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
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
}
}

0 comments on commit 0084625

Please sign in to comment.