-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.go
122 lines (101 loc) · 3.93 KB
/
web.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
// Package web contains a small web framework extension.
package web
import (
"context"
"errors"
"net/http"
"os"
"syscall"
"time"
"github.com/dimfeld/httptreemux/v5"
"github.com/google/uuid"
)
// A Handler is a type that handles a http request within our own little mini
// framework.
type Handler func(ctx context.Context, w http.ResponseWriter, r *http.Request) error
// App is the entrypoint into our application and what configures our context
// object for each of our http handlers. Feel free to add any configuration
// data/logic on this App struct.
type App struct {
*httptreemux.ContextMux
shutdown chan os.Signal
mw []Middleware
}
// NewApp creates an App value that handle a set of routes for the application.
func NewApp(shutdown chan os.Signal, mw ...Middleware) *App {
return &App{
ContextMux: httptreemux.NewContextMux(),
shutdown: shutdown,
mw: mw,
}
}
// SignalShutdown is used to gracefully shut down the app when an integrity
// issue is identified.
func (a *App) SignalShutdown() {
a.shutdown <- syscall.SIGTERM
}
// Handle sets a handler function for a given HTTP method and path pair
// to the application server mux.
func (a *App) Handle(method string, group string, path string, handler Handler, mw ...Middleware) {
// First wrap handler specific middleware around this handler.
handler = wrapMiddleware(mw, handler)
// Add the application's general middleware to the handler chain.
handler = wrapMiddleware(a.mw, handler)
// The function to execute for each request.
h := func(w http.ResponseWriter, r *http.Request) {
// Set the context with the required values to
// process the request.
v := Values{
TraceID: uuid.NewString(),
Now: time.Now().UTC(),
}
ctx := context.WithValue(r.Context(), key, &v)
// Call the wrapped handler functions.
if err := handler(ctx, w, r); err != nil {
// Since there was an error, validate the condition of this
// error and determine if we need to actually shutdown or not.
if validateShutdown(err) {
a.SignalShutdown()
return
}
}
}
finalPath := path
if group != "" {
finalPath = "/" + group + path
}
a.ContextMux.Handle(method, finalPath, h)
}
// validateShutdown validates the error for special conditions that do not
// warrant an actual shutdown by the system.
func validateShutdown(err error) bool {
// Ignore syscall.EPIPE and syscall.ECONNRESET errors which occurs
// when a write operation happens on the http.ResponseWriter that
// has simultaneously been disconnected by the client (TCP
// connections is broken). For instance, when large amounts of
// data is being written or streamed to the client.
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
// https://gosamples.dev/broken-pipe/
// https://gosamples.dev/connection-reset-by-peer/
switch {
case errors.Is(err, syscall.EPIPE):
// Usually, you get the broken pipe error when you write to the connection after the
// RST (TCP RST Flag) is sent.
// The broken pipe is a TCP/IP error occurring when you write to a stream where the
// other end (the peer) has closed the underlying connection. The first write to the
// closed connection causes the peer to reply with an RST packet indicating that the
// connection should be terminated immediately. The second write to the socket that
// has already received the RST causes the broken pipe error.
return false
case errors.Is(err, syscall.ECONNRESET):
// Usually, you get connection reset by peer error when you read from the
// connection after the RST (TCP RST Flag) is sent.
// The connection reset by peer is a TCP/IP error that occurs when the other end (peer)
// has unexpectedly closed the connection. It happens when you send a packet from your
// end, but the other end crashes and forcibly closes the connection with the RST
// packet instead of the TCP FIN, which is used to close a connection under normal
// circumstances.
return false
}
return true
}