mirror of
https://tildegit.org/solderpunk/molly-brown
synced 2024-05-11 09:16:04 +02:00
Catch SIGTERM and shutdown gracefully.
This commit is contained in:
parent
86720131d3
commit
17d17a1629
|
@ -15,11 +15,13 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan LogEntry, errorLog *log.Logger) {
|
func handleGeminiRequest(conn net.Conn, config Config, accessLogEntries chan LogEntry, errorLog *log.Logger, wg *sync.WaitGroup) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
defer wg.Done()
|
||||||
var tlsConn (*tls.Conn) = conn.(*tls.Conn)
|
var tlsConn (*tls.Conn) = conn.(*tls.Conn)
|
||||||
var log LogEntry
|
var log LogEntry
|
||||||
log.Time = time.Now()
|
log.Time = time.Now()
|
||||||
|
|
37
main.go
37
main.go
|
@ -6,7 +6,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var VERSION = "0.0.0"
|
var VERSION = "0.0.0"
|
||||||
|
@ -109,14 +112,36 @@ func main() {
|
||||||
// Restrict access to the files specified in config
|
// Restrict access to the files specified in config
|
||||||
enableSecurityRestrictions(config, errorLog)
|
enableSecurityRestrictions(config, errorLog)
|
||||||
|
|
||||||
// Infinite serve loop
|
// Start listening for signals
|
||||||
for {
|
shutdown := make(chan struct{})
|
||||||
|
sigterm := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigterm, syscall.SIGTERM)
|
||||||
|
go func() {
|
||||||
|
<-sigterm
|
||||||
|
errorLog.Println("Caught SIGTERM. Waiting for handlers to finish...")
|
||||||
|
close(shutdown)
|
||||||
|
listener.Close()
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Infinite serve loop (SIGTERM breaks out)
|
||||||
|
running := true
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for running {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
if err != nil {
|
if err == nil {
|
||||||
errorLog.Println("Error accepting connection: " + err.Error())
|
wg.Add(1)
|
||||||
log.Fatal(err)
|
go handleGeminiRequest(conn, config, accessLogEntries, errorLog, &wg)
|
||||||
|
} else {
|
||||||
|
select {
|
||||||
|
case <-shutdown:
|
||||||
|
running = false
|
||||||
|
default:
|
||||||
|
errorLog.Println("Error accepting connection: " + err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
go handleGeminiRequest(conn, config, accessLogEntries, errorLog)
|
|
||||||
}
|
}
|
||||||
|
// Wait for still-running handler Go routines to finish
|
||||||
|
wg.Wait()
|
||||||
|
errorLog.Println("Exiting.")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue