-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandle.go
137 lines (115 loc) · 3.06 KB
/
handle.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
package differer
import (
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"github.com/jimen0/differer/scheduler"
"google.golang.org/protobuf/proto"
)
const (
maxInputCount = 4096
maxInputLength = 128
)
type input struct {
Addrs []string `json:"addresses"`
}
type output struct {
Results []addressResult `json:"results"`
}
type addressResult struct {
Runner string `json:"runner"`
Input string `json:"string"`
Output *scheduler.Result `json:"outputs"`
}
// HandleInput creates a handler capable of sharing tasks with the runners.
func HandleInput(runners []Runner) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/differer" {
http.NotFound(w, r)
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var i input
if err := json.NewDecoder(r.Body).Decode(&i); err != nil {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Bad schema")
return
}
if len(i.Addrs) == 0 {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "No addresses were received")
return
}
if len(i.Addrs) > maxInputCount {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Only %d addresses can be passed at once", maxInputCount)
return
}
for _, addr := range i.Addrs {
if addr == "" {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Empty addresses are not allowed")
return
}
if len(addr) > maxInputLength {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Max supported address length is %d", maxInputLength)
return
}
}
tasks := len(i.Addrs) * len(runners)
log.Printf("Got %d addresses for %d runners. Total tasks is %d", len(i.Addrs), len(runners), tasks)
var wg sync.WaitGroup
wg.Add(tasks)
results := make(chan addressResult, tasks)
for _, addr := range i.Addrs {
log.Printf("Creating job for %s", addr)
j := &scheduler.Job{Address: addr}
b, err := proto.Marshal(j)
if err != nil {
log.Printf("could not build task for address %q: %v", j.Address, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
go func(addr string, data []byte) {
for _, rn := range runners {
ar := addressResult{Runner: rn.GetName(), Input: addr}
res, err := rn.Run(r.Context(), data)
if err != nil {
log.Printf("Error while calling %s runner for %q: %v", rn.GetName(), addr, err)
ar.Output = res
results <- ar
wg.Done()
continue
}
ar.Output = res
results <- ar
wg.Done()
}
}(j.Address, b)
}
wg.Wait()
var out output
out.Results = make([]addressResult, 0, tasks)
for i := 0; i < tasks; i++ {
v := <-results
out.Results = append(out.Results, v)
}
close(results)
b, err := json.Marshal(out)
if err != nil {
log.Printf("Could not marshal results: %v", err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprint(w, "Something went wrong internally.")
return
}
w.Header().Add("content-type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", b)
}
}