-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
186 lines (145 loc) · 3.53 KB
/
client.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
package hipchat
import (
"fmt"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/gabrielperezs/CARBOnic/cmds"
"github.com/gabrielperezs/CARBOnic/lib"
"github.com/tbruyelle/hipchat-go/hipchat"
)
const (
hipchatRetry = 60 // 60 seconds
hipChatInterval int32 = 15 // Will be modify with random 0 to 5
hipchatRoomPoolInterval = 5 * time.Second
maxClientCalls uint64 = 100
maxTimeout = 10 * time.Second
idleTimeout = 30 * time.Second
)
func newConnection(token string) *HipChatClient {
hcc := &HipChatClient{
token: token,
client: hipchat.NewClient(token),
htclient: &http.Client{
Timeout: maxTimeout * 2,
Transport: &http.Transport{
ExpectContinueTimeout: maxTimeout,
ResponseHeaderTimeout: maxTimeout,
TLSHandshakeTimeout: maxTimeout,
IdleConnTimeout: idleTimeout,
MaxIdleConns: 10,
},
},
}
hcc.client.SetHTTPClient(hcc.htclient)
go hcc.receiver()
return hcc
}
type HipChatClient struct {
sync.Mutex
token string
client *hipchat.Client
rooms []*HipChat
htclient *http.Client
count uint64
}
func (hb *HipChatClient) sender(roomID string, message *lib.Message) {
var err error
var resp *http.Response
if message.Score > 5 {
notifRq := &hipchat.NotificationRequest{
Color: "red",
Message: fmt.Sprintf("@all %s", message.Msg),
}
resp, err = hb.client.Room.Notification(roomID, notifRq)
} else {
msgReq := &hipchat.RoomMessageRequest{
Message: fmt.Sprintf("%s", message.Msg),
}
resp, err = hb.client.Room.Message(roomID, msgReq)
}
if err != nil {
hb.resetCli(resp)
log.Printf("HipChat Pull ERROR [%s]: %s", roomID, err)
return
}
hb.counter(resp)
}
func (hb *HipChatClient) counter(resp *http.Response) {
n := atomic.AddUint64(&hb.count, 1)
if n > maxClientCalls {
hb.resetCli(resp)
}
}
func (hb *HipChatClient) resetCli(resp *http.Response) {
atomic.StoreUint64(&hb.count, 0)
if resp == nil {
return
}
if resp.Body == nil {
return
}
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
resp.Close = true
resp.Request.Close = true
}
func (hb *HipChatClient) receiver() {
for {
hb.Lock()
rooms := hb.rooms
hb.Unlock()
for _, t := range rooms {
// The room is exiting
if t.exiting {
continue
}
roomID := t.cfg.RoomID
hist, resp, err := hb.client.Room.Latest(roomID, &hipchat.LatestHistoryOptions{
MaxResults: t.maxResults,
NotBefore: t.lastMsgID,
})
if err != nil {
hb.resetCli(resp)
log.Printf("HipChat Pull ERROR [RoomID %s]: %s", roomID, err)
log.Printf("HipChat Pull ERROR [RoomID %s] response: %#v", roomID, resp)
log.Printf("HipChat Pull [RoomID %s] re-try in %d seconds", roomID, hipchatRetry)
time.Sleep(time.Second * hipchatRetry)
continue
}
hb.counter(resp)
for _, m := range hist.Items {
if m.ID == t.lastMsgID {
continue
}
from := ""
switch m.From.(type) {
case string:
from = m.From.(string)
case map[string]interface{}:
f := m.From.(map[string]interface{})
from = f["name"].(string)
}
msg := m.Message
t.lastMsgID = m.ID
if t.maxResults == 1 {
continue
}
if lib.IsDupMessage(fmt.Sprintf("hipchat_cmd_%s", roomID), msg) {
continue
}
cmds.Commands(t, from, msg)
}
if t.maxResults == 1 {
t.maxResults = 10
}
time.Sleep(hipchatRoomPoolInterval)
}
time.Sleep(time.Duration(hipChatInterval+rand.Int31n(5)) * time.Second)
}
}