Skip to content

Commit

Permalink
Fix windows build
Browse files Browse the repository at this point in the history
Signed-off-by: Joeky <joeky5888@gmail.com>
  • Loading branch information
joeky888 committed Feb 13, 2024
1 parent 69c06f5 commit c563524
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 14 deletions.
15 changes: 1 addition & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"
"unsafe"

"github.com/appleboy/graceful"
"github.com/valyala/fasthttp"
"golang.org/x/exp/slog"
)
Expand Down Expand Up @@ -175,17 +174,5 @@ func main() {
}
}()

graceful.NewManager().AddRunningJob(func(ctx context.Context) error {
<-ctx.Done()
server.DisableKeepalive = true
if err := server.Shutdown(); err != nil {
Warn("Shutdown err: %s", err)
defer os.Exit(1)
} else {
Info("gracefully stopped")
}
return nil
})

<-graceful.NewManager().Done()
<-wait(server)
}
28 changes: 28 additions & 0 deletions wait_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build !windows
// +build !windows

package main

import (
"context"
"os"

"github.com/appleboy/graceful"
"github.com/valyala/fasthttp"
)

func wait(server *fasthttp.Server) <-chan struct{} {
graceful.NewManager().AddRunningJob(func(ctx context.Context) error {
<-ctx.Done()
server.DisableKeepalive = true
if err := server.Shutdown(); err != nil {
Warn("Shutdown err: %s", err)
defer os.Exit(1)
} else {
Info("gracefully stopped")
}
return nil
})

return graceful.NewManager().Done()
}
31 changes: 31 additions & 0 deletions wait_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build windows
// +build windows

package main

import (
"os"
"os/signal"
"syscall"

"github.com/valyala/fasthttp"
)

func wait(server *fasthttp.Server) <-chan struct{} {
ctx := make(chan os.Signal)
sig := make(chan struct{})
signal.Notify(ctx, os.Interrupt, syscall.SIGTERM)
go func() {
<-ctx
server.DisableKeepalive = true
if err := server.Shutdown(); err != nil {
Warn("Shutdown err: %s", err)
defer os.Exit(1)
} else {
Info("gracefully stopped")
}
sig <- struct{}{}
}()

return sig
}

0 comments on commit c563524

Please sign in to comment.