Skip to content

Commit d993b01

Browse files
committed
fix: data race
1 parent 18f1218 commit d993b01

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

internal/scheduler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package internal
22

33
import (
4+
"context"
45
"log"
56
"os"
67
"path"
78
"time"
89
)
910

10-
func StartScheduler(shutdown chan os.Signal) {
11+
func StartScheduler(ctx context.Context) {
1112
hourlyChecker := time.NewTicker(1 * time.Hour)
1213
defer hourlyChecker.Stop()
1314

1415
for {
1516
select {
16-
case <-shutdown:
17+
case <-ctx.Done():
1718
log.Println("Stopping scheduler")
1819
return
1920
case <-hourlyChecker.C:

main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"os"
78
"os/signal"
89
"syscall"
10+
"time"
911

1012
"github.com/jibon57/secure-file-download-server/internal"
1113
"gopkg.in/yaml.v3"
@@ -34,17 +36,17 @@ func main() {
3436
sigChan := make(chan os.Signal, 1)
3537
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
3638

39+
ctx, cancel := context.WithCancel(context.Background())
40+
defer cancel()
41+
3742
// start scheduler
38-
go internal.StartScheduler(sigChan)
43+
go internal.StartScheduler(ctx)
3944

4045
go func() {
4146
sig := <-sigChan
47+
ctx.Done()
4248
log.Println("exit requested, shutting down", "signal", sig)
43-
err = router.Shutdown()
44-
if err != nil {
45-
log.Fatalln(err)
46-
}
47-
log.Println("server shutdown")
49+
router.ShutdownWithTimeout(time.Second)
4850
}()
4951

5052
err = router.Listen(fmt.Sprintf(":%d", internal.AppCnf.Port))

0 commit comments

Comments
 (0)