-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathhttp_utils.go
460 lines (381 loc) · 10.1 KB
/
http_utils.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package main
import (
"bytes"
"encoding/hex"
"errors"
"flag"
"fmt"
"io"
"math/rand"
"net"
gourl "net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
"text/template"
"time"
)
func usageAndExit(msg string) {
if msg != "" {
fmt.Println(msg)
}
flag.Usage()
fmt.Println("")
os.Exit(1)
}
type flagSlice []string
func (h *flagSlice) String() string {
return fmt.Sprintf("%s", *h)
}
func (h *flagSlice) Set(value string) error {
*h = append(*h, value)
return nil
}
var logLevels = map[int]string{
vTRACE: "TRACE",
vDEBUG: "DEBUG",
vINFO: "INFO",
}
// Optimize verbosePrint function to avoid unnecessary formatting
func verbosePrint(level int, vfmt string, args ...interface{}) {
if *verbose > level {
return
}
prefix := "[ERROR]"
if l, ok := logLevels[level]; ok {
prefix = "[" + l + "]"
}
// Avoid unnecessary Sprintf calls when there are no arguments
if len(args) == 0 {
fmt.Println(prefix + " " + vfmt)
} else {
fmt.Printf(prefix+" "+vfmt+"\n", args...)
}
}
const (
IntMax = int(^uint(0) >> 1)
IntMin = ^IntMax
// String generation constants
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterNumBytes = "0123456789"
// HTTP related constants
httpContentTypeJSON = "application/json"
httpWorkerApiPath = "/api"
)
var (
ErrInitWsClient = errors.New("init ws client error")
ErrInitHttpClient = errors.New("init http client error")
ErrInitTcpClient = errors.New("init tcp client error")
ErrUrl = errors.New("check url error")
)
var (
fnSrc = rand.NewSource(time.Now().UnixNano()) // for functions
fnMap = template.FuncMap{
"intSum": intSum,
"random": random,
"randomDate": randomDate,
"randomString": randomString,
"randomNum": randomNum,
"date": date,
"UUID": uuid,
"escape": escape,
"getEnv": getEnv,
"hexToString": hexToString,
"stringToHex": stringToHex,
"toString": toString,
}
fnUUID = randomString(10)
)
// template functions
func intSum(v ...int64) int64 {
var r int64
for _, r1 := range v {
r += int64(r1)
}
return r
}
// Add a mutex to protect concurrent access to fnSrc
var fnSrcMutex sync.Mutex
// Optimize random function to ensure thread safety
func random(min, max int64) int64 {
fnSrcMutex.Lock()
defer fnSrcMutex.Unlock()
return rand.Int63n(max-min) + min
}
func formatTime(now time.Time, fmt string) string {
switch fmt {
case "YMD":
return now.Format("20060201")
case "HMS":
return now.Format("150405")
default:
return now.Format("20060201-150405")
}
}
// YMD = yyyyMMdd, HMS = HHmmss, YMDHMS = yyyyMMdd-HHmmss
func date(fmt string) string {
return formatTime(time.Now(), fmt)
}
// Optimize randomDate function to ensure thread safety
func randomDate(fmt string) string {
fnSrcMutex.Lock()
randomTime := time.Unix(rand.Int63n(time.Now().Unix()-94608000)+94608000, 0)
fnSrcMutex.Unlock()
return formatTime(randomTime, fmt)
}
func escape(u string) string {
return gourl.QueryEscape(u)
}
// Optimize randomN function for more efficient random number generation
func randomN(n int, letter string) string {
if n <= 0 {
return ""
}
b := make([]byte, n)
letterLen := int64(len(letter))
fnSrcMutex.Lock()
for i := 0; i < n; i++ {
b[i] = letter[rand.Int63n(letterLen)%letterLen]
}
fnSrcMutex.Unlock()
return string(b)
}
// randomString generates a random string of length n
// using alphanumeric characters
func randomString(n int) string {
return randomN(n, letterBytes)
}
// randomNum generates a random numeric string of length n
func randomNum(n int) string {
return randomN(n, letterNumBytes)
}
// uuid returns a unique identifier string
func uuid() string {
return fnUUID
}
func getEnv(key string) string {
return os.Getenv(key)
}
// Optimize hexToString function with error handling
func hexToString(hexStr string) string {
data, err := hex.DecodeString(hexStr)
if err != nil {
verbosePrint(vERROR, "hex decode error: %v", err)
return ""
}
return string(data)
}
func stringToHex(s string) string {
data := []byte(s)
return hex.EncodeToString(data)
}
func toString(args ...interface{}) string {
return fmt.Sprintf(`"%v"`, args...)
}
// Optimize parseTime function to support more time units and better error handling
func parseTime(timeStr string) int64 {
if len(timeStr) == 0 {
usageAndExit("empty time string")
}
unit := timeStr[len(timeStr)-1]
valueStr := timeStr
multi := int64(1)
switch unit {
case 's':
valueStr = timeStr[:len(timeStr)-1]
case 'm':
valueStr = timeStr[:len(timeStr)-1]
multi = 60
case 'h':
valueStr = timeStr[:len(timeStr)-1]
multi = 3600
case 'd':
valueStr = timeStr[:len(timeStr)-1]
multi = 86400
}
// If the last character is not a valid time unit, assume seconds
if unit >= '0' && unit <= '9' {
valueStr = timeStr
}
t, err := strconv.ParseInt(valueStr, 10, 64)
if err != nil || t <= 0 {
usageAndExit(fmt.Sprintf("invalid duration: %s", timeStr))
}
return multi * t
}
var bytePool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
// Optimize fastRead function with buffer size limit
func fastRead(r io.Reader, cycleRead bool) (int64, error) {
buf := bytePool.Get().(*bytes.Buffer)
buf.Reset()
defer bytePool.Put(buf)
// Set maximum read size to prevent memory overflow
const maxReadSize = 10 * 1024 * 1024 // 10MB
var n int64
var err error
// Use LimitReader to restrict single read size
limitedReader := io.LimitReader(r, maxReadSize)
n, err = io.Copy(buf, limitedReader)
if err != nil && err != io.EOF {
return n, err
}
// If reading reaches the limit and needs to continue reading
if n == maxReadSize && cycleRead {
// Continue reading remaining data without saving, only calculate size
for {
nn, err := io.Copy(io.Discard, io.LimitReader(r, maxReadSize))
n += nn
if err != nil || nn < maxReadSize {
break
}
}
}
return n, nil
}
func parseInputWithRegexp(input, regx string) ([]string, error) {
re := regexp.MustCompile(regx)
matches := re.FindStringSubmatch(input)
if len(matches) < 1 {
return nil, fmt.Errorf("could not parse the provided input; input = %v", input)
}
return matches, nil
}
// Optimize parseFile function with more efficient line splitting
func parseFile(fileName string, delimiter []rune) ([]string, error) {
content, err := os.ReadFile(fileName)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
if delimiter == nil {
return []string{string(content)}, nil
}
// Pre-allocate sufficient capacity to reduce reallocation
contentStr := string(content)
estimatedLines := min(int64(len(contentStr)/30), 1000) // Estimate line count
result := make([]string, 0, estimatedLines)
// Create delimiter set for quick lookup
delimSet := make(map[rune]struct{}, len(delimiter))
for _, d := range delimiter {
delimSet[d] = struct{}{}
}
lines := strings.FieldsFunc(contentStr, func(r rune) bool {
_, ok := delimSet[r]
return ok
})
// Filter empty lines
for _, line := range lines {
if line != "" {
result = append(result, line)
}
}
return result, nil
}
type ConnOption struct {
Timeout time.Duration `json:"timeout"`
DisableKeepAlives bool `json:"disable_keep_alives"`
}
type tcpConn struct {
tcpClient net.Conn
uri string
option ConnOption
lastUsed time.Time // Add lastUsed field to track when the connection was last used
}
// Add a connection pool to reuse TCP connections
var tcpConnPool = sync.Pool{
New: func() interface{} {
return &tcpConn{}
},
}
// DialTCP creates a new TCP connection with timeout control and connection pooling
func DialTCP(uri string, option ConnOption) (*tcpConn, error) {
// Get TCP connection object from pool
tcpConn := tcpConnPool.Get().(*tcpConn)
// Add connection timeout control
dialer := net.Dialer{
Timeout: option.Timeout,
}
conn, err := dialer.Dial("tcp", uri)
if err != nil {
// Put the object back to the pool when connection fails
tcpConnPool.Put(tcpConn)
return nil, fmt.Errorf("failed to dial TCP: %w", err)
}
if err := conn.SetDeadline(time.Now().Add(option.Timeout)); err != nil {
conn.Close()
tcpConnPool.Put(tcpConn)
return nil, fmt.Errorf("failed to set deadline: %w", err)
}
// Reuse connection object
tcpConn.tcpClient = conn
tcpConn.uri = uri
tcpConn.option = option
tcpConn.lastUsed = time.Now() // Initialize lastUsed with current time
return tcpConn, nil
}
// Optimize tcpConn.Do method with write timeout control
func (tcp *tcpConn) Do(body []byte) (int64, error) {
if tcp.tcpClient == nil {
return 0, ErrInitTcpClient
}
// Set write timeout
if err := tcp.tcpClient.SetWriteDeadline(time.Now().Add(tcp.option.Timeout)); err != nil {
return 0, fmt.Errorf("set write deadline failed: %w", err)
}
if _, err := tcp.tcpClient.Write(body); err != nil {
return 0, fmt.Errorf("write failed: %w", err)
}
// Set read timeout
if err := tcp.tcpClient.SetReadDeadline(time.Now().Add(tcp.option.Timeout)); err != nil {
return 0, fmt.Errorf("set read deadline failed: %w", err)
}
// Update lastUsed time after successful operation
tcp.lastUsed = time.Now()
return fastRead(tcp.tcpClient, false)
}
// Optimize tcpConn.Close method with connection pool support
func (tcp *tcpConn) Close() error {
if tcp.tcpClient == nil {
return ErrInitTcpClient
}
err := tcp.tcpClient.Close()
tcp.tcpClient = nil
// Put the connection object back to the pool for reuse
tcpConnPool.Put(tcp)
if err != nil {
return fmt.Errorf("close failed: %w", err)
}
return nil
}
// Add connection status check method
func (tcp *tcpConn) isExpired() bool {
if tcp.tcpClient == nil {
return true
}
if time.Since(tcp.lastUsed) > tcp.option.Timeout {
tcp.Close()
return true
}
return false
}
// Helper functions
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}