-
Notifications
You must be signed in to change notification settings - Fork 19
/
log_sender.go
118 lines (103 loc) · 3.56 KB
/
log_sender.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
package client
import (
"fmt"
"github.com/techrail/bark/constants"
"github.com/techrail/bark/models"
"time"
)
const logBatchSizeSmall = 10
const logBatchSizeMedium = 100
const logBatchSizeLarge = 500
// keepSendingLogs is responsible for sending logs to server.
func keepSendingLogs(serverUrl string) {
clientChannelLength := 0
for {
clientChannelLength = len(PendingLogsChan)
var logBatch = []models.BarkLog{}
if clientChannelLength >= logBatchSizeLarge {
// Bulk insert large
for i := 0; i < logBatchSizeLarge; i++ {
elem, ok := <-PendingLogsChan
if !ok {
fmt.Println("E#1LUFJ8 - Error occurred while getting large batch from channel")
break // Something went wrong
}
logBatch = append(logBatch, elem)
}
go func() {
defer Wg.Add(-logBatchSizeLarge)
_, err := PostLogArray(serverUrl+constants.BatchInsertUrl, logBatch)
if err.Severity == 1 {
fmt.Println("E#1LUH90 - Large Batch insertion failed. Error: " + err.Msg + "\n")
for _, logEntry := range logBatch {
fmt.Printf("E#1LUHCR - Log message: | %v\n", logEntry)
}
return
}
fmt.Println("L#1LVP5D - Large Batch sent at ", time.Now().Format("2006-01-02 15:04:05"))
}()
} else if clientChannelLength >= logBatchSizeMedium && clientChannelLength < logBatchSizeLarge {
// Bulk insert large
for i := 0; i < logBatchSizeMedium; i++ {
elem, ok := <-PendingLogsChan
if !ok {
fmt.Println("E#1LUFJC - Error occurred while getting medium batch from channel")
break // Something went wrong
}
logBatch = append(logBatch, elem)
}
go func() {
defer Wg.Add(-logBatchSizeMedium)
_, err := PostLogArray(serverUrl+constants.BatchInsertUrl, logBatch)
if err.Severity == 1 {
fmt.Println("E#1LUHEZ - Medium Batch insertion failed. Error: " + err.Msg + "\n")
for _, logEntry := range logBatch {
fmt.Printf("E#1LUHF3 - Log message: | %v\n", logEntry)
}
return
}
fmt.Println("L#1LUE2H - Medium Batch sent at ", time.Now().Format("2006-01-02 15:04:05"))
}()
} else if clientChannelLength >= logBatchSizeSmall && clientChannelLength < logBatchSizeMedium {
// Bulk insert large
for i := 0; i < logBatchSizeSmall; i++ {
elem, ok := <-PendingLogsChan
if !ok {
fmt.Println("E#1LUFTF - Error occurred while getting small batch from channel")
break // Something went wrong
}
logBatch = append(logBatch, elem)
}
go func() {
defer Wg.Add(-logBatchSizeSmall)
_, err := PostLogArray(serverUrl+constants.BatchInsertUrl, logBatch)
if err.Severity == 1 {
fmt.Println("E#1LUHFT - Small Batch insertion failed. Error: " + err.Msg + "\n")
for _, logEntry := range logBatch {
fmt.Printf("E#1LUHFV - Log message: | %v\n", logEntry)
}
return
}
fmt.Println("L#1LVP4K - Small Batch sent at ", time.Now().Format("2006-01-02 15:04:05"))
}()
} else if clientChannelLength > 0 && clientChannelLength < logBatchSizeSmall {
// Commit one at a time
singleLog, ok := <-PendingLogsChan
if !ok {
fmt.Println("E#1LUHNW - Error occurred while getting single log from channel")
}
go func() {
defer Wg.Done()
_, err := PostLog(serverUrl+constants.SingleInsertUrl, singleLog)
if err.Severity == 1 {
fmt.Println("E#1LUHW8 - Single log entry insertion failed. Error: " + err.Msg + "\n")
fmt.Printf("E#1LUHWI - Log message: | %v\n", singleLog)
return
}
fmt.Println("L#1LUHWR - Single insertion sent at ", time.Now().Format("2006-01-02 15:04:05"))
}()
} else {
time.Sleep(100 * time.Millisecond)
}
}
}