-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
121 lines (95 loc) · 2.63 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
//go:build linux
// +build linux
package main
import (
"context"
"flag"
"fmt"
net "github.com/godzie44/go-uring/net"
reactor "github.com/godzie44/go-uring/reactor"
"github.com/godzie44/go-uring/uring"
"io"
"log"
gonet "net"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
)
var noURing = flag.Bool("no-uring", false, "dont use io_uring based http server")
var ringCount = flag.Int("r", 1, "io_urings count")
func main() {
flag.Parse()
go func() {
_ = http.ListenAndServe("localhost:6060", nil)
}()
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(w http.ResponseWriter, request *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("pong\n")) //nolint
})
mux.HandleFunc("/post", func(w http.ResponseWriter, request *http.Request) {
if request.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
return
}
defer request.Body.Close()
body, _ := io.ReadAll(request.Body)
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("you send: %s", string(body)))) //nolint
})
mux.HandleFunc("/get-file", func(w http.ResponseWriter, request *http.Request) {
file, err := os.ReadFile("./example/http-server/gopher.png")
checkErr(err)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Write(file) //nolint
})
mux.HandleFunc("/file-info", func(w http.ResponseWriter, request *http.Request) {
request.ParseMultipartForm(10 << 20) //nolint
file, handler, err := request.FormFile("file")
checkErr(err)
defer file.Close()
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Uploaded File: %+v\n", handler.Filename)
fmt.Fprintf(w, "File Size: %+v\n", handler.Size)
fmt.Fprintf(w, "MIME Header: %+v\n", handler.Header)
})
if *noURing {
log.Println("start default net/http server")
defSrv(mux)
} else {
log.Println("start io_uring server")
ringSrv(mux)
}
}
func defSrv(mux *http.ServeMux) {
lc := gonet.ListenConfig{}
l, _ := lc.Listen(context.Background(), "tcp", "0.0.0.0:8080")
server := &http.Server{
Handler: mux,
}
err := server.Serve(l)
checkErr(err)
}
func ringSrv(mux *http.ServeMux) {
ringCnt := *ringCount
var params []uring.SetupOption
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
rings, stop, err := uring.CreateMany(ringCnt, uring.MaxEntries>>2, 1, params...)
checkErr(err)
defer stop() //nolint
reactor, err := reactor.NewNet(rings)
checkErr(err)
l, err := net.NewListener(gonet.ListenConfig{}, "0.0.0.0:8080", reactor)
checkErr(err)
server := &http.Server{Addr: "0.0.0.0:8080", Handler: mux}
defer server.Close()
err = server.Serve(l)
checkErr(err)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}