-
Notifications
You must be signed in to change notification settings - Fork 0
/
producer_batch.go
134 lines (118 loc) · 2.67 KB
/
producer_batch.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
// Copyright (c) 2022. Homeland Interactive Technology Ltd. All rights reserved.
package ruixuego
import (
"net/http"
"sync"
"time"
)
func newBatchWriter(c *Client, conf *BigDataConfig) *batchWriter {
return &batchWriter{
conf: conf,
client: c,
bufferMutex: new(sync.RWMutex),
buffer: make([]*BigDataLog, 0, conf.BatchSize),
cacheMutex: new(sync.RWMutex),
cache: make([]*BigDataLog, 0, conf.BatchSize*2),
closed: make(chan struct{}, 1),
}
}
type batchWriter struct {
conf *BigDataConfig
client *Client
bufferMutex *sync.RWMutex
buffer []*BigDataLog
cacheMutex *sync.RWMutex
cache []*BigDataLog
gzipPool *gzipPool
closed chan struct{}
}
func (bw *batchWriter) Init() error {
if !bw.conf.AutoFlush {
return nil
}
go func() {
ticker := time.NewTicker(bw.conf.AutoFlushInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := bw.Flush()
if err != nil {
logger.Errorf(err.Error())
}
case <-bw.closed:
return
}
}
}()
return nil
}
func (bw *batchWriter) Write(logData *BigDataLog) error {
bw.bufferMutex.Lock()
bw.buffer = append(bw.buffer, logData)
bw.bufferMutex.Unlock()
if bw.bufferLength() >= bw.conf.BatchSize || bw.cacheLength() > 0 {
return bw.Flush()
}
return nil
}
func (bw *batchWriter) Flush() error {
bw.bufferMutex.Lock()
bw.cacheMutex.Lock()
defer bw.cacheMutex.Unlock()
if len(bw.cache) == 0 && len(bw.buffer) == 0 {
bw.bufferMutex.Unlock()
return nil
}
defer func() {
if len(bw.cache) > bw.conf.CacheCapacity {
bw.cache = append(bw.cache[:0], bw.cache[len(bw.cache)-bw.conf.CacheCapacity:]...)
}
}()
if len(bw.cache) == 0 || len(bw.buffer) >= bw.conf.BatchSize {
bw.cache = append(bw.cache, bw.buffer...)
bw.buffer = bw.buffer[:0]
}
bw.bufferMutex.Unlock()
n := len(bw.cache)
if n > bw.conf.BatchSize {
n = bw.conf.BatchSize
}
b, err := MarshalJSON(bw.cache[:n])
if err != nil {
return err
}
code := 0
for i := 0; i < 3; i++ {
code, err = bw.client.track(b, n, !bw.conf.DisableCompress)
if err != nil {
logger.Errorf("failed to send track log: [%d] %s, data: %s", code, err.Error(), b)
if code != http.StatusOK {
continue
}
return err
} else {
bw.cache = append(bw.cache[:0], bw.cache[n:]...)
break
}
}
return err
}
func (bw *batchWriter) Close() error {
close(bw.closed)
err := bw.Flush()
if err != nil {
return err
}
return nil
}
func (bw *batchWriter) bufferLength() int {
bw.bufferMutex.RLock()
defer bw.bufferMutex.RUnlock()
return len(bw.buffer)
}
func (bw *batchWriter) cacheLength() int {
bw.cacheMutex.RLock()
defer bw.cacheMutex.RUnlock()
return len(bw.cache)
}