-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints_cpu.go
112 lines (100 loc) · 2.5 KB
/
endpoints_cpu.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
package main
import (
"net/http"
"runtime"
"strconv"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
const (
// params
queryParamNbTheads string = "nb_threads"
)
type CPUEndpoints struct {
lock *sync.Mutex
stopFuncs []func()
workers *sync.WaitGroup
}
func NewCPUEndpoints() *CPUEndpoints {
return &CPUEndpoints{
lock: &sync.Mutex{},
workers: &sync.WaitGroup{},
}
}
func (e *CPUEndpoints) Load(l *log.Entry, w http.ResponseWriter, r *http.Request) {
l.Debug("parsing query variables")
// parsing size
nbThreads := 1
nbThreadsString := r.URL.Query().Get(queryParamNbTheads)
if len(nbThreadsString) > 0 {
if !positiveIntegerRegex.MatchString(nbThreadsString) {
errorString := "number of threads doesn't match regex: " + positiveIntegerRegex.String()
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorString))
l.Warn(errorString)
return
}
nbThreads, _ = strconv.Atoi(nbThreadsString) // can't fail thanks to the regexp
}
if nbThreads == 0 {
// setting it to the number of cores in the system
nbThreads = runtime.NumCPU()
}
// parsing timeout
var timeout time.Duration
var err error
timeoutString := r.URL.Query().Get(queryParamTimeout)
if len(timeoutString) > 0 {
timeout, err = time.ParseDuration(timeoutString)
if err != nil {
errorString := "timeout is incorrect: " + err.Error()
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorString))
l.Warn(errorString)
return
} else if timeout < 0 {
errorString := "timeout is inferior to zero: " + timeout.String()
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorString))
l.Warn(errorString)
return
}
}
// starting workers
e.lock.Lock()
defer e.lock.Unlock()
e.workers.Add(nbThreads)
l.Infof("starting %d load workers, with timeout of %s", nbThreads, timeout.String())
for range nbThreads {
stopCh := make(chan int)
e.stopFuncs = append(e.stopFuncs, func() { stopCh <- 0 })
go func(timeout time.Duration, stopChan <-chan int) {
defer e.workers.Done()
// worker loop
for {
select {
case <-stopCh:
return
default:
if timeout > 0 {
time.Sleep(timeout)
}
}
}
}(timeout, stopCh)
}
w.WriteHeader(http.StatusOK)
}
func (e *CPUEndpoints) Reset(l *log.Entry, w http.ResponseWriter, r *http.Request) {
// stopping load workers
e.lock.Lock()
for _, stopFunc := range e.stopFuncs {
go stopFunc()
}
e.stopFuncs = []func(){}
e.lock.Unlock()
e.workers.Wait()
l.Info("load workers stopped")
w.WriteHeader(http.StatusOK)
}