forked from crask/kafka-pusher
/
server.go
127 lines (107 loc) · 2.71 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
package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"time"
//"path/filepath"
"runtime"
"syscall"
"github.com/golang/glog"
"gopkg.in/Shopify/sarama.v1"
)
type HTTPServer struct {
Addr string
Handler http.Handler
ReadTimeout time.Duration
WriteTimeout time.Duration
MaxHeaderBytes int
KeepAliveEnable bool
RouterFunc func(map[string]func(http.ResponseWriter, *http.Request))
Wg *sync.WaitGroup
Mux map[string]func(http.ResponseWriter, *http.Request)
}
type Server struct {
managers []*Manager
httpsvr *http.Server
}
func NewServer() *Server {
return &Server{}
}
func (this *Server) Init(configFile string) error {
runtime.GOMAXPROCS(runtime.NumCPU())
config, err := loadConfig(configFile)
if err != nil {
glog.Fatalf("[Pusher]Load Config err: %s", err.Error())
return err
}
// check env var
if commitInterval < 0 {
glog.Errorf("[Pusher]Invalid commit interval:%d", commitInterval)
return errors.New("Invalid param")
}
// init sarama logger
sarama.Logger = log.New(os.Stdout, "[Sarama] ", log.LstdFlags)
// init consumer managers
for _, callbackConfig := range config.Callbacks {
glog.Infoln(callbackConfig)
manager := NewManager()
if e := manager.Init(&callbackConfig); e != nil {
glog.Fatalf("[Pusher]Init manager for url[%s] failed, %s", callbackConfig.Url, e.Error())
return e
}
this.managers = append(this.managers, manager)
}
// init http service
if statPort > 0 {
hdl := NewHandler()
hdl.AssignRouter()
this.httpsvr = &http.Server{
Addr: fmt.Sprintf(":%d", statPort),
Handler: hdl,
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
MaxHeaderBytes: 1 << 20,
}
}
return nil
}
func (this *Server) Run() error {
// run consumer managers
for _, mgr := range this.managers {
mgr.Work()
}
glog.V(2).Info("[Pusher]Managers get to work!")
// run http service
if statPort > 0 {
if err := this.httpsvr.ListenAndServe(); err != nil {
glog.Fatalln("[Pusher]Start admin http server failed.", err)
return err
}
glog.V(2).Info("[Pusher]Start admin http server success.")
}
// register signal callback
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGTERM, syscall.SIGKILL)
select {
case <-c:
glog.V(2).Info("[Pusher]Catch exit signal")
for _, mgr := range this.managers {
mgr.Close()
}
glog.V(2).Info("[Pusher]Exit done")
}
return nil
}
func (this *Server) Find(topic, group string) (*Manager, error) {
for _, mgr := range this.managers {
if mgr.Topic == topic && mgr.Group == group {
return mgr, nil
}
}
return nil, errors.New("Manager not found")
}