package main import bytes encoding json fmt github com go-redis redis

  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
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"net/http"
"net/url"
"strconv"
"time"
"os"
"log"
"strings"
)
type OrderedGetUrl struct {
BaseURL string
Params []map[string]string
}
func (o *OrderedGetUrl) Add(key string, value string) {
m := make(map[string]string)
m[key] = value
o.Params = append(o.Params, m)
}
func (o *OrderedGetUrl) BuildUrl() string {
buf := bytes.NewBufferString(o.BaseURL)
if len(o.Params) > 0 {
buf.WriteString("?")
}
for index, param := range o.Params {
if index > 0 {
buf.WriteString("&")
}
for k, v := range param {
buf.WriteString(fmt.Sprint(k, "="))
buf.WriteString(url.QueryEscape(v))
}
}
return buf.String()
}
type JsonData struct {
Ua string `json:"ua"`
Pclick string `json:"pclick"`
}
var queue = make(chan string)
var limiter chan bool
var errLogger = log.New(os.Stderr, "Error: ", 0)
func main() {
host := os.Getenv("REDIS_HOST")
if host == "" {
errLogger.Fatalln("Plase set REDIS_HOST environment")
//os.Exit(1)
}
workersEnv := os.Getenv("WORKERS")
if workersEnv == "" {
errLogger.Fatalln("Plase set WORKERS environment")
os.Exit(1)
}
workers, _ := strconv.Atoi(workersEnv)
limiter = make(chan bool, workers)
fmt.Println("Host is: ", host)
client := redis.NewClient(&redis.Options{
Addr: host,
Password: "", // no password set
DB: 0, // use default DB
})
go func() {
for {
func() {
defer recoverFromMainPanic()
limiter <- true
result, err := client.BLPop(0, "clicks").Result()
if err != nil {
time.Sleep(3 * time.Second)
<-limiter
errLogger.Println("Wait")
return
}
queue <- result[1]
}()
}
}()
for key := range queue {
go worker(key)
}
}
func recoverFromMainPanic() {
if r := recover(); r != nil {
<-limiter
errLogger.Println("recovered from Main Panic", r)
}
}
func recoverFromPanic() {
if r := recover(); r != nil {
errLogger.Println("recovered from ", r)
}
}
func worker(item string) {
defer func() {
<-limiter
}()
//fmt.Println("Started a GoRoutine")
defer recoverFromPanic()
splited := strings.SplitN(item, ":", 2)
var m JsonData
err := json.Unmarshal([]byte(splited[1]), &m)
if err != nil {
errLogger.Println("Unmarshaling error: ", err.Error())
errLogger.Println(splited)
return
}
parsedUrl, err := url.Parse(m.Pclick)
if err != nil {
log.Fatal(err)
}
query := parsedUrl.Query()
query.Set("q", splited[0])
parsedUrl.RawQuery = query.Encode()
client := http.Client{Timeout: time.Second * 10}
req, err := http.NewRequest("GET", parsedUrl.String(), nil)
if err != nil {
errLogger.Println(err.Error())
}
req.Header.Add("User-Agent", m.Ua)
resp, err := client.Do(req)
if err != nil {
errLogger.Println(err.Error())
return
}
const layout = "2006 Jan 01 15:04:05 (MST)"
fmt.Println(time.Now().Format(layout), parsedUrl.String(), resp.Status)
resp.Body.Close()
}