-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert.go
79 lines (66 loc) · 1.51 KB
/
insert.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
package mtsdb
import (
"context"
"sync"
"time"
"github.com/jackc/pgx/v5"
)
type insertMetric struct {
TableName string
Container *sync.Map
Labels []string
}
func (m *mtsdb) insert() {
defer func() {
m.wg.Done()
m.concurrentInserts.Add(-1)
}()
m.wg.Add(1)
m.concurrentInserts.Add(1)
m.metrics.Range(func(key, metric any) bool {
batch := new(pgx.Batch)
im := metric.(MetricInterface).Write()
sql := m.generateSql(im.TableName, im.Labels)
im.Container.Range(func(key, value any) bool {
values := make([]any, len(im.Labels)+1)
for i, fieldValue := range value.(*MetricLabelValues).fields {
values[i] = fieldValue
}
values[len(im.Labels)] = value.(*MetricLabelValues).count.Load()
batch.Queue(sql, values...)
if batch.Len() >= m.config.BatchInsertSize {
m.wg.Add(1) // m.wg.Done() is on sendBatch
m.job <- *batch
batch = &pgx.Batch{}
}
return true
})
if batch.Len() > 0 {
m.wg.Add(1) // m.wg.Done() is on sendBatch
m.job <- *batch
}
return true
})
}
func (m *mtsdb) raiseError(err error) {
select { // non-blocking channel send
case m.err <- err:
default:
}
}
// bulk insert
func (m *mtsdb) sendBatch(batch *pgx.Batch) {
defer m.wg.Done()
tm := time.Now().UnixMilli()
br := m.pool.SendBatch(context.Background(), batch)
defer func(br pgx.BatchResults) {
_ = br.Close()
}(br)
_, err := br.Exec()
if err != nil {
m.raiseError(err)
return
}
m.MetricInserts.Add(uint64(batch.Len()))
m.MetricDurationMs.Add(uint64(time.Now().UnixMilli() - tm))
}