GitLab is used only for code review, issue tracking and project management. Canonical locations for source code are still https://gitweb.torproject.org/ https://git.torproject.org/ and git-rw.torproject.org.

Let Web server shut down gracefully.

That allows us to cache bridge lines of connections that were still open
while we received the signal to shut down.
parent 0b06c612
package main
import (
"context"
"flag"
"io"
"log"
......@@ -114,23 +115,36 @@ func main() {
if err := cache.ReadFromDisk(cacheFile); err != nil {
log.Printf("Could not read cache because: %s", err)
}
// When catching SIGINT or SIGTERM, write our cache to disk before exiting.
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT)
signal.Notify(signalChan, syscall.SIGTERM)
var srv http.Server
srv.Addr = addr
srv.Handler = NewRouter()
log.Printf("Starting service on port %s.", addr)
go func() {
<-signalChan
if err := cache.WriteToDisk(cacheFile); err != nil {
log.Printf("Could not write cache because: %s", err)
if certFilename != "" && keyFilename != "" {
srv.ListenAndServeTLS(certFilename, keyFilename)
} else {
srv.ListenAndServe()
}
os.Exit(1)
}()
router := NewRouter()
log.Printf("Starting service on port %s.", addr)
if certFilename != "" && keyFilename != "" {
log.Fatal(http.ListenAndServeTLS(addr, certFilename, keyFilename, router))
} else {
log.Fatal(http.ListenAndServe(addr, router))
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT)
signal.Notify(signalChan, syscall.SIGTERM)
log.Printf("Waiting for signal to shut down.")
<-signalChan
log.Printf("Received signal to shut down.")
// Give our Web server a maximum of a minute to shut down, and finish
// handling open connections.
t := time.Now().Add(time.Minute)
ctx, cancel := context.WithDeadline(context.Background(), t)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("Failed to shut down Web server: %s", err)
}
if err := cache.WriteToDisk(cacheFile); err != nil {
log.Printf("Failed to write cache to disk: %s", err)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment