forked from hanguofeng/taiji
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.go
159 lines (128 loc) · 3.55 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/Shopify/sarama"
"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/samuel/go-zookeeper/zk"
)
type Server struct {
// callbackManager
callbackManagers []*CallbackManager
callbackManagerRunner *ServiceRunner
// adminServer
adminServer *http.Server
adminServerRouter *mux.Router
// global http connection pool
httpTransport http.RoundTripper
// config
config *ServiceConfig
}
var serverInstance *Server
func GetServer() *Server {
// singleton
if serverInstance == nil {
serverInstance = &Server{
adminServerRouter: mux.NewRouter(),
}
}
return serverInstance
}
func (s *Server) Init(configFileName string) error {
config, err := LoadConfigFile(configFileName)
if err != nil {
glog.Fatalf("Load Config err [err:%s]", err.Error())
return err
}
s.config = config
// init admin server
if config.StatServerPort > 0 {
s.adminServer = &http.Server{
Addr: fmt.Sprintf(":%d", config.StatServerPort),
Handler: s.adminServerRouter,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
MaxHeaderBytes: 1 << 20,
}
}
// init sarama logger
sarama.Logger = NewLibraryLogger(2, "[Sarama] ")
// init zookeeper logger
zk.DefaultLogger = NewLibraryLogger(2, "[Zookeeper] ")
// init http transport
s.httpTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConnsPerHost: config.ConnectionPoolSize,
}
// init callback managers
for i, _ := range config.Callbacks {
callbackConfig := &config.Callbacks[i]
glog.V(1).Infof("Initialize CallbackManager [callbackConfig:%v]", callbackConfig)
callbackManager := NewCallbackManager()
if err := callbackManager.Init(callbackConfig); err != nil {
glog.Fatalf("Init CallbackManager failed [url:%s][err:%s]", callbackConfig.Url, err)
return err
}
s.callbackManagers = append(s.callbackManagers, callbackManager)
}
s.callbackManagerRunner = NewServiceRunner()
return nil
}
func (s *Server) Validate(configFileName string) error {
_, err := LoadConfigFile(configFileName)
return err
}
func (s *Server) Run() error {
// run consumer managers
s.callbackManagerRunner.Prepare()
_, err := s.callbackManagerRunner.RunAsync(s.callbackManagers)
if err != nil {
glog.Fatalf("Start CallbackManager failed, Pusher failed to start")
return err
}
glog.V(1).Infof("Pusher server get to work")
// run http service
if s.adminServer != nil {
go func() {
if err := s.adminServer.ListenAndServe(); err != nil {
glog.Fatalf("Start admin http server failed [err:%s]", err.Error())
}
}()
}
// register signal callback
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGKILL)
select {
case <-s.callbackManagerRunner.WaitForExitChannel():
glog.Fatal("Pusher have one CallbackManager unexpected stopped, stopping server")
case <-c:
glog.Info("Pusher catch exit signal")
s.callbackManagerRunner.Close()
}
glog.Infof("Pusher exit done")
// adminServer would not close properly
return nil
}
func (s *Server) GetAdminServerRouter() *mux.Router {
return s.adminServerRouter
}
func (s *Server) GetCallbackManagers() []*CallbackManager {
return s.callbackManagers
}
func (s *Server) GetHttpTransport() http.RoundTripper {
return s.httpTransport
}
func (s *Server) GetConfig() *ServiceConfig {
return s.config
}