Skip to content

Commit b7bf7f2

Browse files
committed
[ws-proxy] Implement graceful shutdown
1 parent c85581c commit b7bf7f2

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

components/ws-proxy/cmd/run.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"time"
1414

1515
"github.com/bombsimon/logrusr/v2"
16-
"github.com/gitpod-io/golang-crypto/ssh"
1716
"github.com/spf13/cobra"
1817
"google.golang.org/grpc"
1918
"google.golang.org/grpc/credentials"
@@ -36,6 +35,7 @@ import (
3635
"github.com/gitpod-io/gitpod/ws-proxy/pkg/config"
3736
"github.com/gitpod-io/gitpod/ws-proxy/pkg/proxy"
3837
"github.com/gitpod-io/gitpod/ws-proxy/pkg/sshproxy"
38+
"github.com/gitpod-io/golang-crypto/ssh"
3939
)
4040

4141
var (
@@ -159,11 +159,15 @@ var runCmd = &cobra.Command{
159159
}
160160
}
161161

162-
go proxy.NewWorkspaceProxy(cfg.Ingress, cfg.Proxy, proxy.HostBasedRouter(cfg.Ingress.Header, cfg.Proxy.GitpodInstallation.WorkspaceHostSuffix, cfg.Proxy.GitpodInstallation.WorkspaceHostSuffixRegex), infoprov, signers).MustServe()
163-
log.Infof("started proxying on %s", cfg.Ingress.HTTPAddress)
162+
ctrlCtx := ctrl.SetupSignalHandler()
163+
164+
go func() {
165+
proxy.NewWorkspaceProxy(cfg.Ingress, cfg.Proxy, proxy.HostBasedRouter(cfg.Ingress.Header, cfg.Proxy.GitpodInstallation.WorkspaceHostSuffix, cfg.Proxy.GitpodInstallation.WorkspaceHostSuffixRegex), infoprov, signers).MustServe(ctrlCtx)
166+
log.Infof("started proxying on %s", cfg.Ingress.HTTPAddress)
167+
}()
164168

165169
log.Info("🚪 ws-proxy is up and running")
166-
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
170+
if err := mgr.Start(ctrlCtx); err != nil {
167171
log.WithError(err).Fatal(err, "problem starting ws-proxy")
168172
}
169173

components/ws-proxy/pkg/proxy/proxy.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ package proxy
66

77
import (
88
"bytes"
9+
"context"
910
"crypto/tls"
11+
"errors"
1012
stdlog "log"
1113
"net/http"
1214
"os"
1315
"path/filepath"
16+
"time"
1417

1518
"github.com/gorilla/mux"
1619
"github.com/klauspost/cpuid/v2"
@@ -49,13 +52,20 @@ func redirectToHTTPS(w http.ResponseWriter, r *http.Request) {
4952
}
5053

5154
// MustServe starts the proxy and ends the process if doing so fails.
52-
func (p *WorkspaceProxy) MustServe() {
55+
func (p *WorkspaceProxy) MustServe(ctx context.Context) {
5356
handler, err := p.Handler()
5457
if err != nil {
5558
log.WithError(err).Fatal("cannot initialize proxy - this is likely a configuration issue")
5659
return
5760
}
58-
srv := &http.Server{
61+
62+
httpServer := &http.Server{
63+
Addr: p.Ingress.HTTPAddress,
64+
Handler: http.HandlerFunc(redirectToHTTPS),
65+
ErrorLog: stdlog.New(logrusErrorWriter{}, "", 0),
66+
}
67+
68+
httpsServer := &http.Server{
5969
Addr: p.Ingress.HTTPSAddress,
6070
Handler: handler,
6171
TLSConfig: &tls.Config{
@@ -77,17 +87,35 @@ func (p *WorkspaceProxy) MustServe() {
7787
crt = filepath.Join(tproot, crt)
7888
key = filepath.Join(tproot, key)
7989
}
90+
8091
go func() {
81-
err := http.ListenAndServe(p.Ingress.HTTPAddress, http.HandlerFunc(redirectToHTTPS))
82-
if err != nil {
92+
err := httpServer.ListenAndServe()
93+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
8394
log.WithError(err).Fatal("cannot start http proxy")
8495
}
8596
}()
8697

87-
err = srv.ListenAndServeTLS(crt, key)
98+
go func() {
99+
err = httpsServer.ListenAndServeTLS(crt, key)
100+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
101+
log.WithError(err).Fatal("cannot start proxy")
102+
return
103+
}
104+
}()
105+
106+
<-ctx.Done()
107+
108+
shutDownCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
109+
defer cancel()
110+
111+
err = httpServer.Shutdown(shutDownCtx)
88112
if err != nil {
89-
log.WithError(err).Fatal("cannot start proxy")
90-
return
113+
log.WithError(err).Fatal("cannot stop HTTP server")
114+
}
115+
116+
err = httpsServer.Shutdown(shutDownCtx)
117+
if err != nil {
118+
log.WithError(err).Fatal("cannot stop HTTPS server")
91119
}
92120
}
93121

0 commit comments

Comments
 (0)