-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
123 lines (110 loc) · 3.4 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"errors"
"fmt"
"github.com/journeymidnight/nentropy/helper"
"github.com/journeymidnight/nentropy/log"
"github.com/journeymidnight/nentropy/util/stop"
"github.com/journeymidnight/nentropy/util/tracing"
opentracing "github.com/opentracing/opentracing-go"
"golang.org/x/net/context"
"net"
"os"
"os/signal"
"syscall"
)
var (
logger *log.Logger
Listener net.Listener
raftPort int
cfg *Config
stopper *stop.Stopper
Server *OsdServer
)
func main() {
cfg = MakeConfig()
logger = helper.Logger
stopper = stop.NewStopper()
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
errChan := make(chan error, 1)
var err error
Listener, err = net.Listen("tcp", ":0")
if err != nil {
panic("take up a random port failed")
}
helper.Println(5, "Listen at :", Listener.Addr())
cfg.Tracer = tracing.NewTracer()
cfg.Tracer.Configure(cfg.Zipkin)
sp := cfg.Tracer.StartSpan("Server Start")
ctx := opentracing.ContextWithSpan(context.Background(), sp)
//defer sp.Finish()
go func() {
defer func() {
if Server != nil {
if r := recover(); r != nil {
panic(r)
}
}
}()
defer sp.Finish()
var err error
cfg.AmbientCtx = log.NewAmbientContext(ctx, cfg.Tracer)
if err = func() error {
Server, err = NewOsdServer(*cfg, stopper)
if err != nil {
return errors.New("failed to create server : " + err.Error())
}
if err = Server.Start(ctx); err != nil {
return errors.New("failed to start server : " + err.Error())
}
return nil
}(); err != nil {
errChan <- err
}
}()
defer helper.Printf(0, "exit...")
// Block until one of the signals above is received or the stopper
// is stopped externally (for example, via the quit endpoint).
select {
case err := <-errChan:
// SetSync both flushes and ensures that subsequent log writes are flushed too.
helper.Printf(5, "osd quit %v", err)
return
case <-stopper.ShouldStop():
// Server is being stopped externally and our job is finished
// here since we don't know if it's a graceful shutdown or not.
<-stopper.IsStopped()
// SetSync both flushes and ensures that subsequent log writes are flushed too.
return
case sig := <-signalCh:
// We start synchronizing log writes from here, because if a
// signal was received there is a non-zero chance the sender of
// this signal will follow up with SIGKILL if the shutdown is not
// timely, and we don't want logs to be lost.
helper.Printf(5, "received signal '%s'", sig)
if sig == os.Interrupt {
// Graceful shutdown after an interrupt should cause the process
// to terminate with a non-zero exit code; however SIGTERM is
// "legitimate" and should be acknowledged with a success exit
// code. So we keep the error state here for later.
msgDouble := "Note: a second interrupt will skip graceful shutdown and terminate forcefully"
fmt.Fprintln(os.Stdout, msgDouble)
}
//go func() {
// serverStatusMu.Lock()
// serverStatusMu.draining = true
// needToDrain := serverStatusMu.created
// serverStatusMu.Unlock()
// if needToDrain {
// if _, err := s.Drain(server.GracefulDrainModes); err != nil {
// // Don't use shutdownCtx because this is in a goroutine that may
// // still be running after shutdownCtx's span has been finished.
// log.Warning(context.Background(), err)
// }
// }
// stopper.Stop(context.Background())
//}()
}
return
}