-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
74 lines (66 loc) · 1.45 KB
/
server.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
package log
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
"sync"
"time"
)
var logmaplock = &sync.Mutex{}
var logmap = []string{}
// 1 line => 500 charater => 100_000line =>50 mb
const LIMIT_LOG_MAP_LENGTH = 100_000
func flushLog() {
// flush periodically in 5s
for {
logmaplock.Lock()
if len(logmap) == 0 {
logmaplock.Unlock()
break
}
var lines []string
if len(logmap) < 100 {
lines = logmap
logmap = nil
} else {
lines = logmap[0:100]
logmap = logmap[100:]
}
logmaplock.Unlock()
if len(lines) > 0 {
sendLog(lines)
}
}
}
func sendLog(lines []string) {
body := ""
for _, line := range lines {
body += base64.StdEncoding.EncodeToString([]byte(line)) + "\n"
}
buff := bytes.NewBuffer([]byte(body))
// retry max 10
for i := 0; i < 10; i++ {
resp, err := http.Post(logServerHost+"/collect/?format=base64&secret="+logServerSecret, "text/plain", buff)
if err != nil {
fmt.Println("LOG ERR", err.Error(), "RETRY")
fmt.Println("STREAM LOG ERR 28340539", err.Error(), ", retrying in 10 seconds...")
time.Sleep(10 * time.Second)
continue
}
var body []byte
if resp.Body != nil {
body, _ = io.ReadAll(resp.Body)
resp.Body.Close()
}
if resp.StatusCode == 200 {
break
}
fmt.Println("STREAM LOG ERR 19234854", resp.StatusCode, ": "+string(body)+" , retrying in 5 seconds...")
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
break
}
time.Sleep(5 * time.Second)
}
}