Skip to content

Commit

Permalink
feat: add safe shutdown of http servers
Browse files Browse the repository at this point in the history
call shutdown when interrupt signal is made

Signed-off-by: Caleb Woodbine <[email protected]>
  • Loading branch information
BobyMCbobs committed Jun 12, 2023
1 parent 128eac9 commit 1a7c981
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
30 changes: 24 additions & 6 deletions cmd/coolest-serverless-app/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"

"github.com/BobyMCbobs/sample-ko-monorepo/pkg/common"
Expand Down Expand Up @@ -117,11 +120,12 @@ func NewCoolestServerlessApp() *CoolestServerlessApp {
handler := common.Logging(mux)
return &CoolestServerlessApp{
server: &http.Server{
Addr: ":8080",
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Addr: ":8080",
Handler: handler,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
},
weatherMetrics: weatherMetrics,
latitude: GetLatitude(),
Expand All @@ -131,7 +135,21 @@ func NewCoolestServerlessApp() *CoolestServerlessApp {

func (c *CoolestServerlessApp) Run() {
log.Printf("Listening on HTTP port '%v'\n", c.server.Addr)
log.Fatal(c.server.ListenAndServe())
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
if err := c.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()

<-done
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := c.server.Shutdown(ctx); err != nil {
log.Fatalf("Server didn't exit gracefully %v", err)
}
}

func (c *CoolestServerlessApp) updateWeatherMetrics() error {
Expand Down
31 changes: 25 additions & 6 deletions cmd/mission-critical-service/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"context"
"crypto/rand"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"github.com/BobyMCbobs/sample-ko-monorepo/pkg/common"
Expand Down Expand Up @@ -55,18 +59,33 @@ func NewMissionCritialService() *MissionCritialService {
handler := common.Logging(mux)
return &MissionCritialService{
server: &http.Server{
Addr: appPort,
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Addr: appPort,
Handler: handler,
ReadTimeout: 10 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
},
}
}

func (w *MissionCritialService) Run() {
log.Printf("Listening on HTTP port '%v'\n", w.server.Addr)
log.Fatal(w.server.ListenAndServe())

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := w.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()

<-done
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := w.server.Shutdown(ctx); err != nil {
log.Fatalf("Server didn't exit gracefully %v", err)
}
}

func main() {
Expand Down
39 changes: 24 additions & 15 deletions cmd/webthingy/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
"runtime/debug"
"os"
"os/signal"
"syscall"
"time"

"github.com/BobyMCbobs/sample-ko-monorepo/pkg/common"
Expand Down Expand Up @@ -35,27 +38,33 @@ func NewWebThingy() *WebThingy {
handler := common.Logging(mux)
return &WebThingy{
server: &http.Server{
Addr: ":8080",
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
Addr: ":8080",
Handler: handler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
MaxHeaderBytes: 1 << 20,
},
}
}

func (w *WebThingy) Run() {
info, _ := debug.ReadBuildInfo()
var debugInfo string
for _, i := range info.Settings {
switch i.Key {
case "vcs.revision", "vcs.time", "vcs.modified":
debugInfo += i.Key + " " + i.Value + " "
}
}
log.Printf("%v", debugInfo)
log.Printf("Listening on HTTP port '%v'\n", w.server.Addr)
log.Fatal(w.server.ListenAndServe())
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
if err := w.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()

<-done
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := w.server.Shutdown(ctx); err != nil {
log.Fatalf("Server didn't exit gracefully %v", err)
}
}

func main() {
Expand Down

0 comments on commit 1a7c981

Please sign in to comment.