-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.go
124 lines (105 loc) · 2.84 KB
/
utils.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
// SPDX-FileCopyrightText: 2022 Marshall Wace <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"context"
"fmt"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func cleanupPeers(peers []string) []string {
cleanedPeers := []string{}
for _, peer := range peers {
cleanPeer := strings.TrimSpace(peer)
if !strings.Contains(cleanPeer, "http://") {
cleanPeer = fmt.Sprintf("http://%s", cleanPeer)
}
if strings.Count(cleanPeer, ":") < 2 {
cleanPeer = fmt.Sprintf("%s:%d", cleanPeer, port)
}
cleanedPeers = append(cleanedPeers, cleanPeer)
}
return cleanedPeers
}
func serverGracefulShutdown(server *http.Server, quit <-chan os.Signal, done chan<- bool) {
<-quit
log.Info("HTTP server is shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Could not gracefully shutdown the HTTP server: %v\n", err)
}
close(done)
}
func constructCacheKey(bucket string, key string) string {
return fmt.Sprintf("%s#%s", bucket, key)
}
func jsonLogMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Next()
duration := getDurationInMillseconds(start)
entry := log.WithFields(log.Fields{
"client_ip": getClientIP(c),
"duration": duration,
"method": c.Request.Method,
"path": c.Request.RequestURI,
"status": c.Writer.Status(),
"referrer": c.Request.Referer(),
})
if c.Writer.Status() >= 500 {
entry.Error(c.Errors.String())
} else {
entry.Info("")
}
}
}
func httpMetricsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.RequestURI, "/_groupcache") {
return
}
start := time.Now()
c.Next()
duration := getDurationInMillseconds(start)
httpRequestsMetric.WithLabelValues(
c.Request.Method,
c.Request.RequestURI,
strconv.Itoa(c.Writer.Status()),
).Inc()
httpRequestsLatencyMetric.WithLabelValues(
c.Request.Method,
c.Request.RequestURI,
strconv.Itoa(c.Writer.Status()),
).Set(duration)
}
}
func getDurationInMillseconds(start time.Time) float64 {
end := time.Now()
duration := end.Sub(start)
milliseconds := float64(duration) / float64(time.Millisecond)
rounded := float64(int(milliseconds*100+.5)) / 100
return rounded
}
func getClientIP(c *gin.Context) string {
requester := c.Request.Header.Get("X-Forwarded-For")
if len(requester) == 0 {
requester = c.Request.Header.Get("X-Real-IP")
}
if len(requester) == 0 {
requester = c.Request.RemoteAddr
}
if strings.Contains(requester, ",") {
requester = strings.Split(requester, ",")[0]
}
return requester
}
func unsupportedRequest(c *gin.Context) {
c.String(400, "Unsupported request under read-only mode.")
}