-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinfluxdb_pool.go
More file actions
226 lines (193 loc) · 4.88 KB
/
Copy pathinfluxdb_pool.go
File metadata and controls
226 lines (193 loc) · 4.88 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package connpool
//Influxdb
import (
"errors"
"fmt"
"log"
"net/url"
"strings"
"sync"
"time"
"github.com/baishancloud/octopux-swtfr/g"
"github.com/influxdata/influxdb/client/v2"
)
type InfluxdbClient struct {
name string
Address string
Username string
Password string
Database string
UserAgent string
Precision string
Timeout time.Duration
UDPPayload int `toml:"udp_payload"`
cli client.Client
}
func (this InfluxdbClient) Name() string {
return this.name
}
func (this InfluxdbClient) Closed() bool {
return this.cli == nil
}
func (this InfluxdbClient) Close() error {
if this.cli != nil {
this.cli.Close()
this.cli = nil
return nil
}
return nil
}
func (this *InfluxdbClient) Connect() error {
// Backward-compatability with single Influx URL config files
// This could eventually be removed in favor of specifying the urls as a list
if this.Address == "" {
return fmt.Errorf("Influxdb url is nil.")
}
switch {
case strings.HasPrefix(this.Address, "udp"):
parsedURL, err := url.Parse(this.Address)
if err != nil {
return err
}
if this.UDPPayload == 0 {
this.UDPPayload = client.UDPPayloadSize
}
c, err := client.NewUDPClient(client.UDPConfig{
Addr: parsedURL.Host,
PayloadSize: this.UDPPayload,
})
if err != nil {
return err
}
this.cli = c
default:
// If URL doesn't start with "udp", assume HTTP client
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: this.Address,
Username: this.Username,
Password: this.Password,
UserAgent: this.UserAgent,
Timeout: this.Timeout,
})
if err != nil {
return err
}
this.cli = c
}
return nil
}
func newInfluxdbConnPool(name string, address string, connTimeout time.Duration, maxConns int, maxIdle int) *ConnPool {
pool := NewConnPool(name, address, maxConns, maxIdle)
pool.Username = g.Config().Influxdb.Username
pool.Password = g.Config().Influxdb.Password
pool.Database = g.Config().Influxdb.Database
pool.Precision = "s"
pool.New = func(connName string) (NConn, error) {
nconn := InfluxdbClient{
name: connName,
Address: pool.Address,
Username: pool.Username,
Password: pool.Password,
Database: pool.Database,
Precision: pool.Precision,
Timeout: connTimeout,
}
err := nconn.Connect()
if err != nil {
return nil, err
}
return nconn, nil
}
return pool
}
type InfluxdbConnPoolHelper struct {
sync.RWMutex
M map[string]*ConnPool
MaxConns int
MaxIdle int
ConnTimeout int
CallTimeout int
}
func (this *InfluxdbConnPoolHelper) Get(address string) (*ConnPool, bool) {
this.RLock()
defer this.RUnlock()
p, exists := this.M[address]
return p, exists
}
func (this *InfluxdbConnPoolHelper) Destroy() {
this.Lock()
defer this.Unlock()
addresses := make([]string, 0, len(this.M))
for address := range this.M {
addresses = append(addresses, address)
}
for _, address := range addresses {
this.M[address].Destroy()
delete(this.M, address)
}
}
func CreateInfluxdbCliPools(maxConns, maxIdle, connTimeout, callTimeout int, cluster []string) *InfluxdbConnPoolHelper {
tp := &InfluxdbConnPoolHelper{
M: make(map[string]*ConnPool), MaxConns: maxConns, MaxIdle: maxIdle,
ConnTimeout: connTimeout, CallTimeout: callTimeout,
}
ct := time.Duration(tp.ConnTimeout) * time.Millisecond
for _, address := range cluster {
if _, exist := tp.M[address]; exist {
continue
}
tp.M[address] = newInfluxdbConnPool(address, address, ct, maxConns, maxIdle)
}
return tp
}
func (this *InfluxdbConnPoolHelper) Proc() []string {
procs := []string{}
for _, cp := range this.M {
procs = append(procs, cp.Proc())
}
return procs
}
func (this *InfluxdbConnPoolHelper) Send(addr string, points []*client.Point) (err error) {
connPool, exists := this.Get(addr)
if !exists {
return fmt.Errorf("%s has no connection pool", addr)
}
conn, err := connPool.Fetch()
if err != nil {
return fmt.Errorf("%s get connection fail: conn %v, err %v. proc: %s", addr, conn, err, connPool.Proc())
}
cli := conn.(InfluxdbClient)
done := make(chan error)
go func() {
err = cli.Write(points)
done <- err
}()
select {
case <-time.After(time.Duration(this.CallTimeout) * time.Millisecond):
connPool.ForceClose(conn)
return fmt.Errorf("%s, call timeout", conn.Name())
case err = <-done:
if err != nil {
connPool.ForceClose(conn)
err = fmt.Errorf("%s, call failed, err %v. proc: %s", conn.Name(), err, connPool.Proc())
} else {
connPool.Release(conn)
}
return err
}
}
//influx write
func (this *InfluxdbClient) Write(points []*client.Point) error {
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: this.Database,
Precision: this.Precision,
})
for _, point := range points {
bp.AddPoint(point)
}
if e := this.cli.Write(bp); e != nil {
log.Println("ERROR: " + e.Error())
return errors.New("Could not write to any InfluxDB server in cluster")
}
return nil
}