/
MHQueueInvoker.go
227 lines (177 loc) · 5.26 KB
/
MHQueueInvoker.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
package engine
import (
"adexchange/lib"
m "adexchange/models"
"bytes"
"fmt"
"github.com/astaxie/beego"
"github.com/garyburd/redigo/redis"
"gopkg.in/vmihailenco/msgpack.v2"
"time"
)
type MHQueueData struct {
QueueName string
AdResponse *m.AdResponse
}
func invokeMHQueue(demand *Demand) {
beego.Debug("Start Invoke MHQueue,did:" + demand.Did)
var adResponse *m.AdResponse
queueName := generateQueueName(demand)
queueChan := make(chan *m.AdResponse)
timeoutChan := make(chan bool, 1)
go processDemand(demand, queueName, 0)
go processDemand(demand, queueName, 1)
//go processDemand(demand, queueName, i)
//go processAdResponseQueue(queueName, queueChan)
go waitQueue(demand.Timeout, timeoutChan)
go processAdResponseFromQueue(queueName, queueChan)
select {
case adResponse = <-queueChan:
beego.Debug("Queue Return adresponse")
break
case <-timeoutChan:
beego.Debug("Queue Timeout")
adResponse = generateErrorResponse(demand.AdRequest, demand.AdspaceKey, lib.ERROR_TIMEOUT_ERROR)
break
}
//beego.Debug("End queue")
//t1 := time.Now().UnixNano()
//adResponse = getAdResponseFromQueue(queueName)
//t2 := time.Now().UnixNano()
//duration := int((t2 - t1) / 1000000)
//if duration > 100 {
// beego.Info(fmt.Sprintf("=====Redis duration=====:%d", duration))
//}
if adResponse == nil {
adResponse = generateErrorResponse(demand.AdRequest, demand.AdspaceKey, lib.ERROR_NO_AD_FROM_QUEUE)
}
demand.Result <- adResponse
}
func generateQueueName(demand *Demand) (queueName string) {
var buffer bytes.Buffer
strToday := time.Now().Format("2006-01-02")
buffer.WriteString("_")
buffer.WriteString("MHQUEUE")
buffer.WriteString("_")
buffer.WriteString(strToday)
buffer.WriteString("_")
buffer.WriteString(demand.AdRequest.AdspaceKey)
buffer.WriteString("_")
buffer.WriteString(demand.AdspaceKey)
buffer.WriteString("_")
buffer.WriteString(lib.ConvertIntToString(demand.AdRequest.Width))
buffer.WriteString("_")
buffer.WriteString(lib.ConvertIntToString(demand.AdRequest.Height))
if len(demand.TargetingCode) > 0 {
buffer.WriteString("_")
buffer.WriteString(demand.TargetingCode)
}
queueName = buffer.String()
return
}
func processDemand(demand *Demand, queueName string, index int) {
newDemand := new(Demand)
newDemand.URL = demand.URL
newDemand.Timeout = demand.Timeout * 10
newDemand.AdRequest = demand.AdRequest
newDemand.RealAdspaceKey = demand.RealAdspaceKey
newDemand.AdspaceKey = demand.AdspaceKey
newDemand.AdSecretKey = demand.AdSecretKey
newDemand.TargetingCode = demand.TargetingCode
newDemand.Result = make(chan *m.AdResponse)
newDemand.Priority = demand.Priority
//Generate new did for queue invoker
newDemand.Did = lib.GenerateBid(newDemand.AdRequest.AdspaceKey + lib.ConvertIntToString(index))
//beego.Debug(newDemand.Did)
go invokeMH(newDemand)
adResponse := <-newDemand.Result
//sent in MHInvoker
//go SendDemandLog(adResponse)
if adResponse.StatusCode == lib.STATUS_SUCCESS {
SendMHQueue(adResponse, queueName)
}
}
//func SendDemandResponse(adResponse *m.AdResponse, queueName string) {
// c := lib.Pool.Get()
// b, err := msgpack.Marshal(adResponse)
// beego.Debug("==========" + beego.AppConfig.String("runmode") + queueName)
// if err == nil {
// c = lib.Pool.Get()
// c.Do("lpush", beego.AppConfig.String("runmode")+queueName, b)
// } else {
// beego.Error(err.Error())
// }
//}
func waitQueue(timeout int, timeoutChan chan bool) {
time.Sleep(time.Millisecond * time.Duration(timeout))
timeoutChan <- true
}
//func processAdResponseQueue(queueName string, queueChan chan *m.AdResponse) {
// c := lib.Pool.Get()
// beego.Debug("==========" + beego.AppConfig.String("runmode") + queueName)
// reply, err := c.Do("rpop", beego.AppConfig.String("runmode")+queueName)
// if err != nil {
// beego.Error(err.Error())
// }
// var adResponse *m.AdResponse
// switch reply := reply.(type) {
// case []byte:
// b, _ := redis.Bytes(reply, nil)
// adResponse = getAdResponse(b)
// break
// case nil:
// beego.Info("AdResponse Queue Connection timeout")
// break
// default:
// beego.Info("AdResponse Queue Unknow reply:")
// beego.Info(reply)
// break
// }
// defer c.Close()
// queueChan <- adResponse
// //if queueChan != nil {
// // queueChan <- adResponse
// //}
//}
func getAdResponse(b []byte) (adResponse *m.AdResponse) {
adResponse = new(m.AdResponse)
err := msgpack.Unmarshal(b, adResponse)
if err != nil {
beego.Critical(err.Error())
}
return adResponse
}
func processAdResponseFromQueue(queueName string, queueChan chan *m.AdResponse) {
t1 := time.Now().UnixNano()
var adResponse *m.AdResponse
c := lib.GetQueuePool().Get()
reply, err := c.Do("rpop", beego.AppConfig.String("runmode")+queueName)
if err != nil {
beego.Error(err.Error())
}
//var adResponse *m.AdResponse
switch reply := reply.(type) {
case []byte:
b, _ := redis.Bytes(reply, nil)
adResponse = getAdResponse(b)
break
case nil:
beego.Debug("AdResponse Queue return nil")
break
default:
beego.Debug("AdResponse Queue Unknow reply:")
beego.Debug(reply)
break
}
defer c.Close()
t2 := time.Now().UnixNano()
duration := int((t2 - t1) / 1000000)
if duration > 100 {
beego.Info(fmt.Sprintf("=====Redis duration=====:%d", duration))
}
queueChan <- adResponse
//return
//if queueChan != nil {
// queueChan <- adResponse
//}
}