-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
85 lines (72 loc) · 1.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
package zipkin
import (
"git.apache.org/thrift.git/lib/go/thrift"
zkcol "github.com/mattkanwisher/distributedtrace/gen/zipkincollector"
)
// Server defines the basic ZipKin server interface.
type Server interface {
Start() error
Stop() error
}
type serverImpl struct {
*thrift.TSimpleServer
stop chan bool
collector *Collector
combiner *Combiner
output Output
}
// NewServer() creates a new server with the specified configuration, or the default
// configuration if nil is given.
func NewServer(config *Config, output Output) (Server, error) {
config = fillDefaultConfig(config)
transport, e := thrift.NewTServerSocket(config.ListenAddress)
if e != nil {
return nil, e
}
var (
collector = NewCollector(config)
combiner = NewCombiner(config)
processor = zkcol.NewZipkinCollectorProcessor(collector)
factory = thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocol = thrift.NewTBinaryProtocolFactoryDefault()
server = thrift.NewTSimpleServer4(processor, transport, factory, protocol)
impl = &serverImpl{server, nil, collector, combiner, output}
)
return impl, nil
}
func (s *serverImpl) Start() error {
s.stop = make(chan bool)
go s.spanPump()
go s.combinePump()
return s.TSimpleServer.Serve()
}
func (s *serverImpl) Stop() error {
e := s.TSimpleServer.Stop()
if e != nil {
return e
}
// stop 2 pumps
s.stop <- true
s.stop <- true
return nil
}
func (s *serverImpl) spanPump() {
for {
select {
case <-s.stop:
return
case span := <-s.collector.Receive():
s.combiner.Send(span)
}
}
}
func (s *serverImpl) combinePump() {
for {
select {
case <-s.stop:
return
case result := <-s.combiner.Receive():
s.output.Write(result)
}
}
}