Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: graceful shutdown and interrupt handling #73

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ The following emojis are used to highlight certain changes:

### Changed

- boxo 0.21
- go-libp2p 0.35

### Removed

### Fixed

- Release tag version is now included in `--version` output.
- `--version` now includes the release tag
- `start` command supports a graceful shutdown and improved handling of interrupt signals

### Security

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.4

require (
github.com/CAFxX/httpcompression v0.0.9
github.com/coreos/go-systemd/v22 v22.5.0
github.com/dustin/go-humanize v1.0.1
github.com/felixge/httpsnoop v1.0.4
github.com/ipfs/boxo v0.21.0
Expand Down Expand Up @@ -33,7 +34,6 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
Expand Down
44 changes: 40 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package main

import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"

"github.com/CAFxX/httpcompression"
sddaemon "github.com/coreos/go-systemd/v22/daemon"
"github.com/felixge/httpsnoop"
"github.com/ipfs/boxo/routing/http/client"
"github.com/ipfs/boxo/routing/http/server"
Expand Down Expand Up @@ -90,9 +96,7 @@ func start(ctx context.Context, cfg *config) error {
return err
}

log.Printf("Starting %s %s\n", name, version)
log.Printf("Listening on %s", cfg.listenAddress)
log.Printf("Delegated Routing API on http://127.0.0.1:%s/routing/v1", port)
fmt.Printf("Starting %s %s\n", name, version)

mdlw := middleware.New(middleware.Config{
Recorder: metrics.NewRecorder(metrics.Config{Prefix: "someguy"}),
Expand Down Expand Up @@ -132,7 +136,39 @@ func start(ctx context.Context, cfg *config) error {
http.Handle("/", handler)

server := &http.Server{Addr: cfg.listenAddress, Handler: nil}
return server.ListenAndServe()
quit := make(chan os.Signal, 3)
var wg sync.WaitGroup
wg.Add(1)

fmt.Printf("Listening on %s\n", cfg.listenAddress)
fmt.Printf("Delegated Routing API on http://127.0.0.1:%s/routing/v1\n", port)

go func() {
defer wg.Done()
err := server.ListenAndServe()
if err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("Failed to start /routing/v1 server: %v", err)
quit <- os.Interrupt
}
}()

sddaemon.SdNotify(false, sddaemon.SdNotifyReady)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
<-quit
sddaemon.SdNotify(false, sddaemon.SdNotifyStopping)
fmt.Printf("\nClosing /routing/v1 server...\n")

// Attempt a graceful shutdown
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Graceful shutdown failed:%+v\n", err)
}

go server.Close()
wg.Wait()
fmt.Println("Shutdown finished.")
return nil
}

func newHost(cfg *config) (host.Host, error) {
Expand Down
Loading