-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.go
132 lines (115 loc) · 2.66 KB
/
main2.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
125
126
127
128
129
130
131
132
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/gorilla/mux"
)
const BUSY_MAX = 5
var sad = false
var busy = false
var busy_counter = 0
func handler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
name := query.Get("name")
if name == "" {
name = "guest"
}
// Set sad flag depending on current value and payload contents
if sad {
if strings.ToLower(name) == "happy" {
sad = false
}
} else {
if strings.ToLower(name) == "sad" {
sad = true
}
}
if strings.Contains(strings.ToLower(name), "busy") {
// Set if received "busy"
busy = true
}
const MAX_LEN = 32
guest_name, _ := os.LookupEnv("HOSTNAME")
str_len := len(guest_name)
from := 0
if str_len > MAX_LEN {
from = str_len - MAX_LEN
}
log.Printf("Received request for %s\n", name)
w.Write([]byte(fmt.Sprintf("Hello, %s from container %s (sad: %t, busy: %t)\n", name, guest_name[from:str_len], sad, busy)))
}
func healthHandler(w http.ResponseWriter, r *http.Request) {
var status int
var ret string
if sad {
status = http.StatusInternalServerError
ret = fmt.Sprintf("error")
} else {
status = http.StatusOK
ret = fmt.Sprintf("ok")
}
w.WriteHeader(status)
w.Write([]byte(ret))
log.Printf("HealthHandler sent %d\n", status)
}
func readinessHandler(w http.ResponseWriter, r *http.Request) {
var status int
var ret string
if busy {
status = http.StatusInternalServerError
busy_counter
// Indicate not busy after three invocations
if busy_counter == BUSY_MAX {
busy = false
busy_counter = 0
}
ret = fmt.Sprintf("busy")
} else {
status = http.StatusOK
ret = fmt.Sprintf("ok")
}
w.WriteHeader(status)
w.Write([]byte(ret))
log.Printf("ReadinessHandler sent %d\n", status)
}
func main() {
// Create Server and Route Handlers
r := mux.NewRouter()
r.HandleFunc("/", handler)
r.HandleFunc("/health", healthHandler)
r.HandleFunc("/readiness", readinessHandler)
srv := &http.Server{
Handler: r,
Addr: ":8080",
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
// Start Server
go func() {
log.Println("Starting Server")
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
// Graceful Shutdown
waitForShutdown(srv)
}
func waitForShutdown(srv *http.Server) {
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Block until we receive our signal.
<-interruptChan
// create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
srv.Shutdown(ctx)
log.Println("Shutting down")
os.Exit(0)
}