-
Notifications
You must be signed in to change notification settings - Fork 35
/
handler.go
100 lines (88 loc) · 2.42 KB
/
handler.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
package http
import (
"fmt"
"github.com/viant/endly/internal/util"
"github.com/viant/toolbox"
"log"
"net/http"
"strings"
"sync/atomic"
"time"
)
type httpHandler struct {
running int32
handler func(writer http.ResponseWriter, request *http.Request)
thinkTime time.Duration
}
const (
thinkTimeHeaderMs = "Think-Time-Ms"
)
func (h *httpHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if thinkTime := request.Header.Get(thinkTimeHeaderMs); thinkTime != "" {
h.thinkTime = time.Duration(toolbox.AsInt(thinkTime)) * time.Millisecond
fmt.Printf("Updated think time: %s\n", h.thinkTime)
}
h.handler(writer, request)
}
func getServerHandler(httpServer *http.Server, httpHandler *httpHandler, trips *HTTPServerTrips) func(writer http.ResponseWriter, request *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
trips.Mutex.Lock()
defer trips.Mutex.Unlock()
if atomic.LoadInt32(&httpHandler.running) == 0 {
return
}
var key, err = buildKeyValue(trips.IndexKeys, request)
if err != nil {
http.Error(writer, fmt.Sprintf("%v", err), http.StatusInternalServerError)
return
}
responses, ok := trips.Trips[key]
if !ok {
var errorMessage = fmt.Sprintf("key: %v not found, available: \n%v", key, strings.Join(toolbox.MapKeysToStringSlice(trips.Trips), ",\n"))
fmt.Println(errorMessage)
http.Error(writer, errorMessage, http.StatusNotFound)
return
}
var index uint32
for {
index := atomic.LoadUint32(&responses.Index)
if atomic.CompareAndSwapUint32(&responses.Index, index, index+1) {
if int(index) >= len(trips.Trips) {
if !trips.Rotate {
http.NotFound(writer, request)
return
}
}
atomic.StoreUint32(&responses.Index, 0)
index = 0
break
}
}
response := responses.Responses[index]
for k, headerValues := range response.Header {
for _, headerValue := range headerValues {
writer.Header().Set(k, headerValue)
}
}
if httpHandler.thinkTime > 0 {
time.Sleep(httpHandler.thinkTime)
}
writer.WriteHeader(response.Code)
if response.Body != "" {
var body, _ = util.FromPayload(response.Body)
_, err = writer.Write(body)
if err != nil {
log.Print(err)
}
}
if len(trips.Trips) == 0 {
func() { _ = httpServer.Close() }()
go func() {
time.Sleep(time.Second)
if atomic.LoadInt32(&httpHandler.running) == 0 {
_ = httpServer.Shutdown(nil)
}
}()
}
}
}