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

FEAT: server graceful shutdown #50

Merged
merged 1 commit into from
Jun 14, 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
30 changes: 28 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
package main

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

"github.com/xray-web/web-check-api/config"
"github.com/xray-web/web-check-api/server"
)

func main() {
s := server.New(config.New())
log.Println(s.Run())
srv := server.New(config.New())

Check warning on line 17 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L17

Added line #L17 was not covered by tests

done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

Check warning on line 20 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L19-L20

Added lines #L19 - L20 were not covered by tests

go func() {
if err := srv.Run(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %v\n", err)

Check warning on line 24 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L22-L24

Added lines #L22 - L24 were not covered by tests
}
}()

<-done

Check warning on line 28 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L28

Added line #L28 was not covered by tests

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer func() {

Check warning on line 31 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L30-L31

Added lines #L30 - L31 were not covered by tests
// extra handling here, databases etc
cancel()
}()

Check warning on line 34 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L33-L34

Added lines #L33 - L34 were not covered by tests

if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server Shutdown Failed:%+v", err)

Check warning on line 37 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L36-L37

Added lines #L36 - L37 were not covered by tests
}
}
12 changes: 11 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"context"
"fmt"
"log"
"net/http"
Expand All @@ -14,10 +15,12 @@
conf config.Config
mux *http.ServeMux
checks *checks.Checks
srv *http.Server
}

func New(conf config.Config) *Server {
return &Server{
srv: &http.Server{},

Check warning on line 23 in server/server.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

server/server.go#L23

Go's `net/http` serve functions may be vulnerable to resource consumption attacks if timeouts are not properly configured prior to starting the HTTP server.
conf: conf,
mux: http.NewServeMux(),
checks: checks.NewChecks(),
Expand Down Expand Up @@ -49,12 +52,19 @@
s.mux.Handle("GET /api/social-tags", handlers.HandleGetSocialTags(s.checks.SocialTags))
s.mux.Handle("GET /api/tls", handlers.HandleTLS(s.checks.Tls))
s.mux.Handle("GET /api/trace-route", handlers.HandleTraceRoute())

s.srv.Handler = s.CORS(s.mux)
}

func (s *Server) Run() error {
s.routes()

addr := fmt.Sprintf("%s:%s", s.conf.Host, s.conf.Port)
log.Printf("Server started, listening on: %v\n", addr)
return http.ListenAndServe(addr, s.CORS(s.mux))
s.srv.Addr = addr
return s.srv.ListenAndServe()

Check warning on line 65 in server/server.go

View check run for this annotation

Codecov / codecov/patch

server/server.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

func (s *Server) Shutdown(ctx context.Context) error {
return s.srv.Shutdown(ctx)
}
41 changes: 41 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package server

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/xray-web/web-check-api/config"
"golang.org/x/net/context"
)

func TestServer(t *testing.T) {
t.Parallel()

t.Run("start server", func(t *testing.T) {
t.Parallel()

srv := New(config.New())
srv.routes()
ts := httptest.NewServer(srv.CORS(srv.mux))
defer ts.Close()

// wait up tot 10 seconds for health check to return 200
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(+10*time.Second))
defer cancel()
for {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ts.URL+"/health", nil)
assert.NoError(t, err)
resp, err := http.DefaultClient.Do(req)
if err == nil && resp.StatusCode == http.StatusOK {
break
}
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := srv.Shutdown(ctx)
assert.NoError(t, err)
})
}