-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (68 loc) · 1.36 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
package main
import (
"bytes"
"fmt"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"time"
)
/*
Typical memory leaks
1. Growing global variable
2. Hanging go routine
3. Open streams
*/
var globalSlice = make([]int64, 0)
// appendSlice: Growing global variable
func appendSlice(c *gin.Context) {
globalSlice = append(globalSlice, time.Now().Unix())
c.JSON(200, map[string]int{
"sliceSize": len(globalSlice),
})
}
// hangingGoRoutine: Hanging go routine
func hangingGoRoutine(ctx *gin.Context) {
go time.Sleep(time.Hour * 24)
}
// makeHttpCall: Open streams
func makeHttpCall(ctx *gin.Context) {
client := &http.Client{}
file, err := os.ReadFile("LOREM-IPSUM.txt")
if err != nil {
return
}
bodyReader := bytes.NewReader(file)
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080/", bodyReader)
if err != nil {
panic(err)
}
_, err = client.Do(req)
if err != nil {
panic(err)
}
body, err := io.ReadAll(bodyReader)
//defer func(Body io.ReadCloser) {
// err := Body.Close()
// if err != nil {
// return
// }
//}(res.Body)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
func main() {
r := gin.Default()
pprof.Register(r)
r.GET("/append-slice", appendSlice)
r.GET("/hanging", hangingGoRoutine)
r.GET("/streams", makeHttpCall)
err := r.Run(":8080")
if err != nil {
panic(err)
}
}