Skip to content

Commit

Permalink
Add DoGCWhen
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Oct 26, 2022
1 parent b958bf5 commit adb3f88
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions appx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package appx

import (
"context"
"os"
"os/signal"
"runtime"
"syscall"
"time"
)
Expand All @@ -19,3 +21,22 @@ func Context() context.Context {
func Uptime() time.Duration {
return time.Since(startTime)
}

// DoGCWhen one of the given signal happens (GC means runtime.GC()).
// Function is async, context is used to close underlying goroutine.
// In most cases appx.DoGCWhen(os.SIGUSR1) should be enough.
func DoGCWhen(ctx context.Context, signals ...os.Signal) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, signals...)

go func() {
for {
select {
case <-ctx.Done():
return
case <-ch:
runtime.GC()
}
}
}()
}

0 comments on commit adb3f88

Please sign in to comment.