This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from XenitAB/errgroup
Use errgroup and context
- Loading branch information
Showing
15 changed files
with
518 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package helper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func NewErrGroupAndContext() (*errgroup.Group, context.Context, context.CancelFunc) { | ||
ctx := context.Background() | ||
ctx, cancel := context.WithCancel(ctx) | ||
g, ctx := errgroup.WithContext(ctx) | ||
return g, ctx, cancel | ||
} | ||
|
||
func WaitForErrGroup(g *errgroup.Group) error { | ||
err := g.Wait() | ||
if err != nil { | ||
return fmt.Errorf("error groups error: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func NewShutdownTimeoutContext() (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(context.Background(), 2*time.Second) | ||
} | ||
|
||
func WaitForStop(stopChan chan os.Signal, ctx context.Context) string { | ||
select { | ||
case sig := <-stopChan: | ||
return fmt.Sprintf("os.Signal (%s)", sig) | ||
case <-ctx.Done(): | ||
return "context" | ||
} | ||
} | ||
|
||
func NewStopChannel() chan os.Signal { | ||
stopChan := make(chan os.Signal, 2) | ||
signal.Notify(stopChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE) | ||
return stopChan | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package helper | ||
|
||
import ( | ||
"context" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type ServiceStarter interface { | ||
Start(ctx context.Context) error | ||
} | ||
|
||
type ServiceStopper interface { | ||
Stop(ctx context.Context) error | ||
} | ||
|
||
func StartService(ctx context.Context, g *errgroup.Group, s ServiceStarter) { | ||
g.Go(func() error { | ||
return s.Start(ctx) | ||
}) | ||
} | ||
|
||
func StopService(ctx context.Context, g *errgroup.Group, s ServiceStopper) { | ||
g.Go(func() error { | ||
return s.Stop(ctx) | ||
}) | ||
} |
Oops, something went wrong.