-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
67 lines (56 loc) · 1.28 KB
/
server.go
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
61
62
63
64
65
66
67
package main
// SPDX-FileCopyrightText: © Moritz Poldrack & AUTHORS
// SPDX-License-Identifier: AGPL-3.0-or-later
import (
"fmt"
"net"
"os"
"os/signal"
"sync"
"time"
"git.sr.ht/~mpldr/uniview/internal/mansion"
"git.sr.ht/~mpldr/uniview/internal/server"
"git.sr.ht/~mpldr/uniview/protocol"
"git.sr.ht/~poldi1405/glog"
"google.golang.org/grpc"
)
func serverShutdown(signals <-chan os.Signal, srv *grpc.Server, rooms *mansion.Mansion) {
sig := <-signals
glog.Infof("received %s, shutting down", sig)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
awaitStop := make(chan struct{})
go func() {
srv.GracefulStop()
close(awaitStop)
}()
select {
case <-awaitStop:
case <-time.After(5 * time.Second):
srv.Stop()
}
}()
go func() {
defer wg.Done()
rooms.Close()
}()
wg.Wait()
}
func startServer() error {
sigs := make(chan os.Signal, 8)
srv := grpc.NewServer()
roomMan := mansion.New()
protocol.RegisterUniViewServer(srv, &server.Server{
Rooms: roomMan,
})
go serverShutdown(sigs, srv, roomMan)
signal.Notify(sigs, os.Interrupt)
glog.Debugf("starting listener on %s:%d", "0.0.0.0", 1558)
lis, err := net.Listen("tcp", "0.0.0.0:1558")
if err != nil {
return fmt.Errorf("failed to start listener: %w", err)
}
return srv.Serve(lis)
}