-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththreads.go
164 lines (152 loc) · 4.02 KB
/
threads.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// logger listens on lc for an incoming array of strings
// and writes those strings to either Stdout, or if the path
// variable is not equal to the empty string, to a logfile
// defined by path. It will also check for a boolean value on the
// mute channel and if true, will disable any logging.
func logger(path string, lc <-chan []string, mute <-chan bool) {
var (
logBuffer bytes.Buffer
logger *log.Logger
enabled bool = true
)
defer wg.Done()
if path != "" {
logWriter, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
defer logWriter.Close()
logger = log.New(logWriter, "", 0)
} else {
logger = log.New(os.Stdout, "", 0)
}
for logEntry := range lc {
select {
case enabled = <-mute:
if enabled {
logBuffer.Reset()
logBuffer.WriteString(time.Now().Format(time.RFC3339))
logBuffer.WriteString(" ")
for _, logString := range logEntry {
logBuffer.WriteString(logString)
}
logger.Print(logBuffer.String())
}
default:
if enabled {
logBuffer.Reset()
logBuffer.WriteString(time.Now().Format(time.RFC3339))
logBuffer.WriteString(" ")
for _, logString := range logEntry {
logBuffer.WriteString(logString)
}
logger.Print(logBuffer.String())
}
}
}
}
// reloadConfigOnHangUp waits for a hang-up signal to arrive on hc.
// When such a signal arrives, the configuration is reloaded from disk
// and a log message is send to lc. If the new configuration is successfully
// processed, the results will be send to gc.
func reloadConfigOnHangUp(caches *string, hc <-chan os.Signal, lc chan<- []string, gc chan<- map[string]Group) {
var groups map[string]Group
defer wg.Done()
Start:
for range hc {
lc <- []string{"Loading configuration & setting up connections.\n"}
groups = make(map[string]Group)
groupList, err := LoadCachesFromIni(*caches)
if err != nil {
lc <- []string{err.Error()}
continue
}
for _, g := range groupList {
for i := 0; i < len(g.Caches); i++ {
_, err = url.Parse(g.Caches[i].Address)
if err != nil {
lc <- []string{err.Error()}
break Start
}
g.Caches[i].Client = createHTTPClient()
}
groups[g.Name] = g
}
gc <- groups
}
}
// gracefulTerminate waits for an Interrupt, TERM or ABRT signal to arrive on the kill channel
// and then closes all the other channel before exiting.
func gracefulTerminate(log chan<- []string, hup chan<- os.Signal, kill chan os.Signal, grp chan<- map[string]Group, mute chan<- bool) {
defer wg.Done()
<-kill
close(mute)
close(grp)
close(kill)
close(hup)
log <- []string{"Broadcaster exited successfully.\n"}
close(log)
}
// jobWorker listens on the jobs channel and handles
// any incoming job.
func jobWorker(jobs <-chan Job, retries int) {
var (
statusCode int
err error
)
defer wg.Done()
for job := range jobs {
for i := 0; i <= retries; i++ {
client := job.Cache.Client
reqString := job.Cache.Address + job.Cache.Item
r, err := http.NewRequest(job.Cache.Method, reqString, nil)
if err != nil {
statusCode = http.StatusInternalServerError
job.Cache.Client = createHTTPClient()
continue
}
rw.Lock()
// Preserve the headers
for k, v := range job.Cache.Headers {
r.Header.Set(k, strings.Join(v, " "))
}
// The "Host" header is the hardest
r.Header.Set("X-Host", job.Cache.Headers.Get("Host"))
r.Host = job.Cache.Headers.Get("Host")
rw.Unlock()
resp, err := client.Do(r)
if err != nil {
statusCode = http.StatusInternalServerError
job.Cache.Client = createHTTPClient()
continue
}
_, err = io.Copy(io.Discard, resp.Body)
if err != nil {
statusCode = http.StatusInternalServerError
job.Cache.Client = createHTTPClient()
continue
}
resp.Body.Close()
statusCode = resp.StatusCode
break
}
if err != nil {
job.Result <- []byte(err.Error())
continue
}
job.Status <- statusCode
}
}