/
report_batch.go
105 lines (89 loc) · 2.59 KB
/
report_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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mixerclient
import (
"sync"
"time"
"sofastack.io/sofa-mosn/pkg/utils"
"istio.io/api/mixer/v1"
)
const (
// the size of attribute channel
attributeChannelSize = 1024
)
// Options controlling report batch.
type reportOptions struct {
// Maximum number of reports to be batched.
maxBatchEntries int
// Maximum time a report item stayed in the buffer for batching.
maxBatchTime time.Duration
}
type reportBatch struct {
batchCompressor BatchCompressor
mixerClient MixerClient
options *reportOptions
batchSize int
attributesChan chan *v1.Attributes
}
func newReportOptions(maxEntries int, maxBatchTimeMs time.Duration) *reportOptions {
return &reportOptions{
maxBatchEntries: maxEntries,
maxBatchTime: maxBatchTimeMs,
}
}
func newReportBatch(compressor *AttributeCompressor, options *reportOptions, client MixerClient) *reportBatch {
batch := &reportBatch{
batchCompressor: compressor.CreateBatchCompressor(),
mixerClient: client,
options: options,
attributesChan: make(chan *v1.Attributes, attributeChannelSize),
}
wg := sync.WaitGroup{}
wg.Add(1)
utils.GoWithRecover(func() {
batch.main(&wg)
}, nil)
wg.Wait()
return batch
}
func (r *reportBatch) main(wg *sync.WaitGroup) {
timer := time.NewTicker(r.options.maxBatchTime).C
wg.Done()
for {
select {
case attributes := <-r.attributesChan:
r.batchCompressor.Add(attributes)
r.batchSize = r.batchCompressor.Size()
if r.batchSize >= r.options.maxBatchEntries {
r.flush()
}
case <-timer:
if r.batchSize > 0 {
r.flush()
}
}
}
}
func (r *reportBatch) report(attributes *v1.Attributes) {
r.attributesChan <- attributes
}
func (r *reportBatch) flush() {
request := r.batchCompressor.Finish()
r.mixerClient.SendReport(request)
r.batchCompressor.Clear()
r.batchSize = 0
}