-
Notifications
You must be signed in to change notification settings - Fork 11
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
Feature: Implement Graceful shutdown #21
Changes from 17 commits
a4e70ff
bb9b964
14cda9c
097300a
292d214
4f24899
06ba9e1
317e5c7
9cb3ba9
1ec0180
617c0bd
2b7a753
581314b
d4397db
fc9ddfb
3ebae2c
2bc082c
b9f1dc6
19336b4
7173f7e
9dc854a
ea9ac7d
df6733f
f57c01d
b6c965d
7a79fa6
0ebb7d3
44cc1e9
2f1d9ac
9751d02
042fa0e
a0725ea
d1eaa5f
2418a11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,13 @@ package server | |
import ( | ||
"context" | ||
"embed" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
"waffle/internal/visualize" | ||
|
||
|
@@ -42,10 +45,7 @@ import ( | |
// If the proxy server fails to start, the function logs a fatal error. | ||
// | ||
// The function returns nil upon normal completion. | ||
func Run(ctx context.Context, proxyServerPort, visualizeServerPort string, yamlConfigBytes []byte, certificates embed.FS) error { | ||
_, cancel := signal.NotifyContext(ctx, os.Interrupt) | ||
defer cancel() | ||
|
||
func Run(proxyServerPort, visualizeServerPort string, yamlConfigBytes []byte, certificates embed.FS) error { | ||
_, err := config.LoadEnvironmentConfig() | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
|
@@ -80,7 +80,6 @@ func Run(ctx context.Context, proxyServerPort, visualizeServerPort string, yamlC | |
) | ||
|
||
defender := guard.NewDefenseCoordinator([]guard.Defender{&guard.XSS{}}) | ||
|
||
limiter := ratelimit.NewInMemoryLimiter(time.Minute * 5) | ||
|
||
visualizeServerPort = fmt.Sprintf(":%s", visualizeServerPort) | ||
|
@@ -95,18 +94,47 @@ func Run(ctx context.Context, proxyServerPort, visualizeServerPort string, yamlC | |
) | ||
|
||
proxyServerPort = fmt.Sprintf(":%s", proxyServerPort) | ||
|
||
proxyServer := proxy.NewServer(proxyServerPort, certificateProvider, guardHandler) | ||
|
||
log.Printf("Starting Waffle Proxy on port %s 🚀\n", proxyServerPort) | ||
|
||
if err := proxyServer.Start(); err != nil { | ||
log.Fatal(err.Error()) | ||
signalChan := make(chan os.Signal, 1) | ||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go func() { | ||
if err := proxyServer.Start(); err != nil && !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatalf("Proxy server encountered an error: %v\n", err) | ||
} | ||
}() | ||
|
||
select { | ||
case sig := <-signalChan: | ||
log.Printf("Received shutdown signal: %s, shutting down gracefully...", sig) | ||
cancel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you can see here, when we receive signal into signalChan, we call cancel func, which closes our context, and 'graceful-shutdowning' process begins. |
||
case <-ctx.Done(): | ||
log.Println("Context canceled, shutting down...") | ||
} | ||
|
||
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer shutdownCancel() | ||
|
||
if err := proxyServer.Shutdown(shutdownCtx); err != nil { | ||
log.Printf("Error during server shutdown: %v", err) | ||
} else { | ||
log.Println("Proxy server shut down gracefully.") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Shutdown is a function to be called while 'gracefully shutdowning' the server. | ||
func Shutdown(ctx context.Context) error { | ||
return nil | ||
} | ||
|
||
// loadLocalCustomCACerts reads the local custom CA certificates from the embedded file system. | ||
// It reads the CA certificate file (ca.crt) located in the ".cert" directory and returns it as a slice of byte slices. | ||
// This CA certificate is used for establishing trust during TLS/SSL handshakes. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,29 +4,31 @@ go 1.22.0 | |
|
||
require ( | ||
github.com/Netflix/go-env v0.0.0-20220526054621-78278af1949d | ||
github.com/cdipaolo/goml v0.0.0-20220715001353-00e0c845ae1c | ||
github.com/corazawaf/libinjection-go v0.1.3 | ||
github.com/emirpasic/gods v1.18.1 | ||
github.com/goccy/go-yaml v1.11.3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to run go mod tidy here, seems we have some unused imports left. As an idea, we can add these to our CI/CD pipeline, and check if |
||
github.com/google/gopacket v1.1.19 | ||
github.com/google/uuid v1.6.0 | ||
github.com/malaschitz/randomForest v0.0.0-20240228214944-c64ffe1648e0 | ||
github.com/patrickmn/go-cache v2.1.0+incompatible | ||
github.com/stretchr/testify v1.9.0 | ||
golang.org/x/sys v0.17.0 | ||
nhooyr.io/websocket v1.8.10 | ||
) | ||
|
||
require ( | ||
github.com/cdipaolo/goml v0.0.0-20220715001353-00e0c845ae1c // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/fatih/color v1.16.0 // indirect | ||
github.com/google/gopacket v1.1.19 // indirect | ||
github.com/malaschitz/randomForest v0.0.0-20240228214944-c64ffe1648e0 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/sjwhitworth/golearn v0.0.0-20221228163002-74ae077eafb2 // indirect | ||
github.com/stretchr/objx v0.5.2 // indirect | ||
golang.org/x/crypto v0.14.0 // indirect | ||
golang.org/x/sys v0.17.0 // indirect | ||
golang.org/x/net v0.10.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect | ||
gonum.org/v1/gonum v0.9.3 // indirect | ||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it must be modified to the correct ports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean like this: