-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathevio.go
48 lines (42 loc) · 1.1 KB
/
evio.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
package main
import (
"fmt"
"log"
"os"
"runtime"
"github.com/tidwall/evio"
)
func main() {
logging := false
if len(os.Args) > 1 {
if os.Args[1] == "--debug" {
logging = true
}
}
const responseString = "HTTP/1.1 204\r\n\r\n"
responseBuffer := []byte(responseString)
buffer := make([]byte, 0, len(responseBuffer)*10000)
for i := 0; i < 10000; i++ {
buffer = append(buffer, responseBuffer...)
}
var events evio.Events
events.Serving = func(s evio.Server) (action evio.Action) {
log.Printf("serving %s\n", s.Addrs[0])
return
}
events.Opened = func(id int, info evio.Info) (out []byte, opts evio.Options, ctx interface{}, action evio.Action) {
// Reuse the input buffer on Data callbacks to avoid unneeded alloc.
opts.ReuseInputBuffer = true
return
}
events.Data = func(id int, ctx interface{}, in []byte) (out []byte, action evio.Action) {
if logging {
fmt.Printf("%q\n", in)
}
numberOfRequests := len(in) / 40
out = buffer[:numberOfRequests*len(responseBuffer)]
return
}
events.NumLoops = runtime.NumCPU() / 2
evio.Serve(events, "tcp://0.0.0.0:8000?reuseport=true")
}