Skip to content

Commit

Permalink
feat(SPV-848): gracafully shutdown in webhooks example
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Jul 4, 2024
1 parent c675400 commit 042514b
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions examples/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

walletclient "github.com/bitcoin-sv/spv-wallet-go-client"
Expand Down Expand Up @@ -48,17 +51,27 @@ func main() {
panic(err)
}

server := http.Server{
Addr: ":5005",
Handler: nil,
ReadHeaderTimeout: time.Second * 10,
}
go func() {
_ = http.ListenAndServe(":5005", nil)
_ = server.ListenAndServe()
}()

<-time.After(30 * time.Second)
// wait for signal to shutdown
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan

fmt.Printf("Unsubscribing...\n")
err = wh.Unsubscribe(context.Background())
if err != nil {
if err = wh.Unsubscribe(context.Background()); err != nil {
panic(err)
}

fmt.Printf("Shutting down...\n")
if err = server.Shutdown(context.Background()); err != nil {
panic(err)
}
}

0 comments on commit 042514b

Please sign in to comment.