-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasthttp_server.go
124 lines (103 loc) · 3.55 KB
/
fasthttp_server.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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/pinpoint-apm/pinpoint-go-agent"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/fasthttp"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/http"
"github.com/valyala/fasthttp"
)
func main() {
opts := []pinpoint.ConfigOption{
pinpoint.WithAppName("GoFastHttpTest"),
pinpoint.WithAgentId("GoFastHttpTestAgent"),
pinpoint.WithHttpUrlStatEnable(true),
pinpoint.WithConfigFile(os.Getenv("HOME") + "/tmp/pinpoint-config.yaml"),
pphttp.WithHttpClientRecordRequestHeader([]string{"HEADERS-ALL"}),
pphttp.WithHttpClientRecordRespondHeader([]string{"HEADERS-ALL"}),
pphttp.WithHttpClientRecordRequestCookie([]string{"HEADERS-ALL"}),
pphttp.WithHttpServerRecordRequestHeader([]string{"Cookie", "Accept"}),
pphttp.WithHttpServerRecordRespondHeader([]string{"Set-Cookie", "X-My-Header"}),
pphttp.WithHttpServerRecordRequestCookie([]string{"HEADERS-ALL"}),
}
cfg, _ := pinpoint.NewConfig(opts...)
agent, err := pinpoint.NewAgent(cfg)
if err != nil {
log.Fatalf("pinpoint agent start fail: %v", err)
}
defer agent.Shutdown()
mux := func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
if strings.HasPrefix(path, "/foo") {
ppfasthttp.WrapHandler(fooHandler, "/foo")(ctx)
} else if strings.HasPrefix(path, "/bar") {
ppfasthttp.WrapHandler(barHandler, "/bar")(ctx)
} else {
ctx.Error("not found", fasthttp.StatusNotFound)
}
}
if err := fasthttp.ListenAndServe(":9000", mux); err != nil {
log.Fatalf("Error in ListenAndServe: %v", err)
}
}
func fooHandler(ctx *fasthttp.RequestCtx) {
sleep()
tracer := pinpoint.FromContext(ctx.UserValue(ppfasthttp.CtxKey).(context.Context))
defer tracer.NewSpanEvent("f1").EndSpanEvent()
defer tracer.NewSpanEvent("f2").EndSpanEvent()
fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
client(ctx)
ctx.SetContentType("text/plain; charset=utf8")
ctx.Response.Header.Set("X-My-Header", "my-header-value")
// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
ctx.Response.Header.SetCookie(&c)
}
func barHandler(ctx *fasthttp.RequestCtx) {
sleep()
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
ctx.SetStatusCode(fasthttp.StatusBadRequest)
}
func client(ctx *fasthttp.RequestCtx) {
// Get URI from a pool
url := fasthttp.AcquireURI()
url.Parse(nil, []byte("http://localhost:8080/"))
url.SetUsername("Aladdin")
url.SetPassword("Open Sesame")
hc := &fasthttp.HostClient{
Addr: "localhost:8080", // The host address and port must be set explicitly
}
req := fasthttp.AcquireRequest()
req.SetURI(url) // copy url into request
fasthttp.ReleaseURI(url) // now you may release the URI
req.Header.SetMethod(fasthttp.MethodGet)
req.Header.SetCookie("cookie-name", "cookie-value")
req.Header.Set("X-My-Header", "my-header-value")
resp := fasthttp.AcquireResponse()
ctxWithTracer := ctx.UserValue(ppfasthttp.CtxKey).(context.Context)
err := ppfasthttp.DoClient(func() error {
return hc.Do(req, resp)
}, ctxWithTracer, req, resp)
if err == nil {
fmt.Printf("Response: %s\n", resp.Body())
} else {
fmt.Fprintf(os.Stderr, "Connection error: %v\n", err)
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}
func sleep() {
seed := rand.NewSource(time.Now().UnixNano())
random := rand.New(seed).Intn(10000)
time.Sleep(time.Duration(random+1) * time.Millisecond)
}