-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
100 lines (87 loc) · 3.27 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
// 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 handles 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 shutdown the app when an integrity
// issue is identified.
func (app *App) SignalShutdown() {
app.shutdown <- syscall.SIGTERM
}
// Handle sets a handler function for a given HTTP method and path pair
// to the application server mux.
func (app *App) Handle(method string, path string, handler Handler, mw ...Middleware) {
handler = wrapMiddleware(mw, handler)
handler = wrapMiddleware(app.mw, handler)
h := func(w http.ResponseWriter, r *http.Request) {
v := Values{
TraceID: uuid.New().String(),
Now: time.Now().UTC(),
}
ctx := context.WithValue(r.Context(), key, &v)
if err := handler(ctx, w, r); err != nil {
if validateShutdown(err) {
app.SignalShutdown()
}
return
}
}
app.ContextMux.Handle(method, path, 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 connection is
// broken). For instance, when large amounts of data is being written or
// streamed to the client.
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 send.
// 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 packat instead of the TCP FIN, which is
// used to close a connection under normal circumstances.
return false
}
return true
}