Skip to content

Commit

Permalink
use correct context for db client disconnect
Browse files Browse the repository at this point in the history
Signed-off-by: xeptore <[email protected]>
  • Loading branch information
xeptore committed Mar 24, 2023
1 parent 7d5b001 commit 82e5453
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/xeptore/wireuse
go 1.20

require (
github.com/fsnotify/fsnotify v1.6.0
github.com/golang/mock v1.6.0
github.com/joho/godotenv v1.5.1
github.com/rs/zerolog v1.29.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
Expand Down Expand Up @@ -108,7 +106,6 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
34 changes: 10 additions & 24 deletions ingest/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"syscall"
"time"

"github.com/fsnotify/fsnotify"
"github.com/joho/godotenv"
"github.com/rs/zerolog"
"go.mongodb.org/mongo-driver/bson"
Expand All @@ -28,9 +27,8 @@ import (
)

var (
restartMarkFileName string
wgDeviceName string
wgPreDownDumpFileName string
restartMarkFileName string
wgDeviceName string
)

func main() {
Expand All @@ -53,7 +51,6 @@ func main() {

flag.StringVar(&restartMarkFileName, "r", "", "restart-mark file name")
flag.StringVar(&wgDeviceName, "i", "", "wireguard interface")
flag.StringVar(&wgPreDownDumpFileName, "d", "", "wireguard pre-down dumped output file")

flag.Parse()
if nonFlagArgs := flag.Args(); len(nonFlagArgs) > 0 {
Expand All @@ -79,7 +76,9 @@ func main() {
log.Fatal().Err(err).Msg("failed to verify database connectivity")
}
defer func() {
if err := client.Disconnect(ctx); err != nil {
disconnectCtx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
if err := client.Disconnect(disconnectCtx); err != nil {
log.Err(err).Msg("failed to disconnect from database")
return
}
Expand All @@ -95,24 +94,11 @@ func main() {

signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
ctx, cancel := context.WithCancelCause(ctx)
runCtx, cancelEngineRun := context.WithCancelCause(ctx)
stopSignalErr := errors.New("stop signal received")
go func() {
<-signals
cancel(stopSignalErr)
}()

go func() {
if wgPreDownDumpFileName == "" {
return
}

watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize wireguard pre-down dump file watcher")
}
defer watcher.Close()
watcher.Add("")
cancelEngineRun(stopSignalErr)
}()

timeTicker := time.NewTicker(5 * time.Second)
Expand All @@ -127,10 +113,10 @@ func main() {
wp := wgPeers{wg}
store := storeMongo{collection}
engine := ingest.NewEngine(&rmf, &wp, &store, log)
if err := engine.Run(ctx, engineTicker, restartMarkFileName); nil != err {
if err := ctx.Err(); nil != err {
if err := engine.Run(runCtx, engineTicker, restartMarkFileName); nil != err {
if err := runCtx.Err(); nil != err {
if errors.Is(err, context.Canceled) {
if errors.Is(context.Cause(ctx), stopSignalErr) {
if errors.Is(context.Cause(runCtx), stopSignalErr) {
log.Info().Msg("root context was canceled due to receiving a interrupt signal")
return
}
Expand Down

0 comments on commit 82e5453

Please sign in to comment.