-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (52 loc) · 1.08 KB
/
main.go
File metadata and controls
60 lines (52 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"HelixDB/db"
"HelixDB/resp"
"flag"
"fmt"
"net"
"os"
"os/signal"
"sync"
"syscall"
)
func main() {
port := flag.Int("port", 6378, "Port to listen on")
flag.IntVar(port, "p", *port, "Port to listen on (shorthand)")
flag.Parse()
db.ServerConfig.Port = *port
addr := fmt.Sprintf("0.0.0.0:%d", *port)
l, err := net.Listen("tcp", addr)
if err != nil {
fmt.Printf("Failed to bind to port %d\n", *port)
os.Exit(1)
}
stop := make(chan struct{})
db.StartActiveExpiry(stop)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-quit
fmt.Println("Shutting down...")
close(stop)
l.Close()
}()
var wg sync.WaitGroup
for {
conn, err := l.Accept()
if err != nil {
// l.Close() during shutdown causes Accept to return an error.
// Check if we're shutting down to avoid a spurious log line.
select {
case <-stop:
default:
fmt.Println("Error accepting connection: ", err.Error())
}
break
}
wg.Add(1)
go resp.HandleConn(conn, &wg)
}
wg.Wait()
fmt.Println("Shutdown complete.")
}