-
Notifications
You must be signed in to change notification settings - Fork 1
/
echo.go
119 lines (100 loc) · 2.56 KB
/
echo.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
// Copyright 2019 The vogo Authors. All rights reserved.
package main
import (
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"path/filepath"
"time"
"github.com/vogo/gracego"
)
var (
server *http.Server
listenAddr = ":8081"
)
func main() {
http.HandleFunc("/hello", HelloHandler)
http.HandleFunc("/sleep5s", SleepHandler)
http.HandleFunc("/calculate5s", CalculateHandler)
http.HandleFunc("/download.zip", DownloadHandler)
http.HandleFunc("/upgrade", UpgradeHandler)
server = &http.Server{}
err := gracego.EnableWritePid("/tmp")
if err != nil {
fmt.Printf("write pid error: %v", err)
}
err = gracego.Serve(server, "echo", listenAddr)
if err != nil {
fmt.Printf("server error: %v", err)
}
}
// HelloHandler handle hello request
func HelloHandler(w http.ResponseWriter, r *http.Request) {
info("request hello")
response(w, 200, "world")
}
// SleepHandler handle sleep request
func SleepHandler(w http.ResponseWriter, r *http.Request) {
info("request sleep")
time.Sleep(5 * time.Second)
response(w, 200, "world")
}
// CalculateHandler handle calculation request
func CalculateHandler(w http.ResponseWriter, r *http.Request) {
info("request calculate")
fiveSecondCalc()
response(w, 200, "world")
}
// the calculation will cost about 5.89s for 2.3 GHz Intel Core i5
func fiveSecondCalc() {
for i := 0; i < math.MaxInt16; i++ {
for j := 0; j < 1<<13; j++ {
math.Sin(float64(i))
math.Cos(float64(i))
}
}
}
// DownloadHandler download the graceup server zip
func DownloadHandler(w http.ResponseWriter, r *http.Request) {
info("request download")
path, err := os.Executable()
if err != nil {
responseError(w, err)
return
}
dir := filepath.Dir(path)
zipFilePath := filepath.Join(dir, "echo.zip")
file, err := os.OpenFile(zipFilePath, os.O_RDONLY, os.ModePerm)
if err != nil {
responseError(w, err)
return
}
w.Header().Add("content-type", "application/octet-stream")
_, err = io.Copy(w, file)
if err != nil {
responseError(w, err)
return
}
}
// UpgradeHandler restart server
func UpgradeHandler(w http.ResponseWriter, r *http.Request) {
info("request upgrade")
err := gracego.Upgrade("v2", "echo", "http://127.0.0.1"+listenAddr+"/download.zip")
if err != nil {
responseError(w, err)
}
response(w, 200, "success")
}
func responseError(w http.ResponseWriter, err error) {
response(w, 500, err.Error())
}
func response(w http.ResponseWriter, code int, msg string) {
w.WriteHeader(code)
_, _ = w.Write([]byte(msg))
}
func info(format string, args ...interface{}) {
log.Println(gracego.GetServerID(), "-", fmt.Sprintf(format, args...))
}