/
main.go
186 lines (156 loc) · 3.73 KB
/
main.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 main
import (
"github.com/codegangsta/martini"
"github.com/mreiferson/go-httpclient"
"github.com/ugorji/go/codec"
"io/ioutil"
"log"
"math"
"net/http"
"reflect"
"strconv"
"sync"
"time"
// "runtime"
)
// TODO:
// 1. review recovery mw
// 2. log to file.. logger mw
// 3. all app constants can be flags..
const (
VERSION = "v0.9"
MAXTIME = 30 // in seconds
THRUPUT = 30 // max number of goroutines per fetch request
ADDR = "0.0.0.0:9333" // server address
)
var (
reqId uint64 = 0 // Request counter
)
type Response struct {
Url string
Status int
Data []byte
}
func main() {
// runtime.GOMAXPROCS(runtime.NumCPU())
// fmt.Printf("GOMAXPROCS is %d\n", runtime.GOMAXPROCS(0))
// Server and middleware
m := martini.New()
m.Use(martini.Logger())
m.Use(martini.Recovery())
r := martini.NewRouter()
m.Action(r.Handle)
r.Get("/", func() string {
return "."
})
r.Any("/fetch", func(req *http.Request, res http.ResponseWriter) {
err := req.ParseForm()
if err != nil {
renderMsg(res, 422, "Unable to parse parameters")
return
}
// Grab params and set defaults
urls := req.Form["url"]
if len(urls) == 0 {
urls = req.Form["url[]"]
}
maxtime, _ := strconv.Atoi(req.Form.Get("maxtime"))
if maxtime == 0 {
maxtime = MAXTIME
}
if len(urls) == 0 {
renderMsg(res, 422, "Url parameter required")
return
}
responses := httpFetch(urls, maxtime)
if err != nil {
renderMsg(res, 500, err.Error())
} else {
renderMsg(res, 200, responses)
}
})
// Boot the server
log.Println("** Purls", VERSION, "http server listening on", ADDR)
log.Println("** with thruput:", THRUPUT, "maxtime:", MAXTIME)
if err := http.ListenAndServe(ADDR, m); err != nil {
log.Fatal(err)
}
}
func httpFetch(urls []string, maxtime int) []*Response {
n := len(urls)
if n == 0 {
return nil
}
log.Println("Purls req", reqId)
reqId++
responses := make([]*Response, n)
in := make(chan int)
go func() {
for i, url := range urls {
responses[i] = &Response{Url: url}
in <- i
}
close(in)
}()
timeout := time.Duration(time.Duration(maxtime) * time.Second)
transport := &httpclient.Transport{RequestTimeout: timeout} //, DisableKeepAlives: true}
client := &http.Client{Transport: transport}
defer transport.Close()
// hrmm.. problem with the thruput is that we need to add more
// logic around overall timeouts.. could be maxtime+maxtime when n > thurput
var wg sync.WaitGroup
thruput := int(math.Min(float64(THRUPUT), float64(n)))
for i := 0; i < thruput; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := range in {
url := urls[j]
log.Println("Fetching", j, url)
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
if err != nil {
log.Println("Http connect error for", url, "because:", err.Error())
break
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Http GET error reading body for", url, "because:", err.Error())
break
}
responses[j].Status = resp.StatusCode
responses[j].Data = body
}
}()
}
wg.Wait()
return responses
}
func renderMsg(res http.ResponseWriter, status int, data interface{}) {
var out []byte
var mh codec.MsgpackHandle
enc := codec.NewEncoderBytes(&out, &mh)
// Typecast ahead of encoding
var err error
switch data.(type) {
case string:
err = enc.Encode(data.(string))
default:
err = enc.Encode(data)
}
if err != nil {
log.Println("Encoding error:", err.Error())
renderMsg(res, 500, err.Error())
return
}
res.Header().Set("Content-Type", "application/x-msgpack")
res.WriteHeader(status)
res.Write(out)
}
//
// * Debug helpers
//
func dTypeOf(x interface{}) {
log.Println(reflect.TypeOf(x))
}