-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
report.go
138 lines (119 loc) · 2.73 KB
/
report.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
package trace
import (
"fmt"
"net"
"os"
"sync"
"time"
)
const (
// MaxPackageSize .
_maxPackageSize = 1024 * 32
// safe udp package size // MaxPackageSize = 508 _dataChSize = 4096)
// max memory usage 1024 * 32 * 4096 -> 128MB
_dataChSize = 4096
_defaultWriteChannalTimeout = 50 * time.Millisecond
_defaultWriteTimeout = 200 * time.Millisecond
)
// reporter trace reporter.
type reporter interface {
WriteSpan(sp *Span) error
Close() error
}
// newReport with network address
func newReport(network, address string, timeout time.Duration, protocolVersion int32) reporter {
if timeout == 0 {
timeout = _defaultWriteTimeout
}
report := &connReport{
network: network,
address: address,
dataCh: make(chan []byte, _dataChSize),
done: make(chan struct{}),
timeout: timeout,
version: protocolVersion,
}
go report.daemon()
return report
}
type connReport struct {
version int32
rmx sync.RWMutex
closed bool
network, address string
dataCh chan []byte
conn net.Conn
done chan struct{}
timeout time.Duration
}
func (c *connReport) daemon() {
for b := range c.dataCh {
c.send(b)
}
c.done <- struct{}{}
}
func (c *connReport) WriteSpan(sp *Span) error {
data, err := marshalSpan(sp, c.version)
if err != nil {
return err
}
return c.writePackage(data)
}
func (c *connReport) writePackage(data []byte) error {
c.rmx.RLock()
defer c.rmx.RUnlock()
if c.closed {
return fmt.Errorf("report already closed")
}
if len(data) > _maxPackageSize {
return fmt.Errorf("package too large length %d > %d", len(data), _maxPackageSize)
}
select {
case c.dataCh <- data:
return nil
case <-time.After(_defaultWriteChannalTimeout):
return fmt.Errorf("write to data channel timeout")
}
}
func (c *connReport) Close() error {
c.rmx.Lock()
c.closed = true
c.rmx.Unlock()
t := time.NewTimer(time.Second)
close(c.dataCh)
select {
case <-t.C:
c.closeConn()
return fmt.Errorf("close report timeout force close")
case <-c.done:
return c.closeConn()
}
}
func (c *connReport) send(data []byte) {
if c.conn == nil {
if err := c.reconnect(); err != nil {
c.Errorf("connect error: %s retry after second", err)
time.Sleep(time.Second)
return
}
}
c.conn.SetWriteDeadline(time.Now().Add(100 * time.Microsecond))
if _, err := c.conn.Write(data); err != nil {
c.Errorf("write to conn error: %s, close connect", err)
c.conn.Close()
c.conn = nil
}
}
func (c *connReport) reconnect() (err error) {
c.conn, err = net.DialTimeout(c.network, c.address, c.timeout)
return
}
func (c *connReport) closeConn() error {
if c.conn != nil {
return c.conn.Close()
}
return nil
}
func (c *connReport) Errorf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
}