-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
136 lines (115 loc) · 2.85 KB
/
handler.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
133
134
135
136
package water
import (
"context"
"errors"
"github.com/go-water/water/circuitbreaker"
"github.com/go-water/water/endpoint"
"github.com/go-water/water/logger"
"github.com/go-water/water/ratelimit"
"github.com/sony/gobreaker"
"golang.org/x/time/rate"
"log/slog"
"net/http"
"reflect"
)
type HandlerFunc func(*Context)
type RouterHandler struct {
wt *Water
h HandlerFunc
}
func (r *RouterHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := r.wt.pool.Get().(*Context)
ctx.Writer = w
ctx.Request = req
ctx.wt = r.wt
ctx.reset()
r.h(ctx)
r.wt.pool.Put(ctx)
}
type handler struct {
e endpoint.Endpoint
finalizer []ServerFinalizerFunc
l *slog.Logger
dl *rate.Limiter
el *rate.Limiter
breaker *gobreaker.CircuitBreaker
}
func NewHandler(srv Service, options ...ServerOption) Handler {
h := new(handler)
for _, option := range options {
option(h)
}
h.e = h.endpoint(srv)
if h.dl != nil {
h.e = ratelimit.NewDelayingLimiter(h.dl)(h.e)
}
if h.el != nil {
h.e = ratelimit.NewErrorLimiter(h.el)(h.e)
}
if h.breaker != nil {
h.e = circuitbreaker.GoBreaker(h.breaker)(h.e)
}
l := logger.NewLogger(logger.Level, logger.AddSource).With(slog.String("name", srv.Name(srv)))
srv.SetLogger(l)
h.l = l
return h
}
func (h *handler) endpoint(service Service) endpoint.Endpoint {
return func(ctx context.Context, req any) (any, error) {
function, srv, ctxV, reqV, err := h.readRequest(ctx, service, req)
if err != nil {
return nil, err
}
returnValues := function.Call([]reflect.Value{srv, ctxV, reqV})
if len(returnValues) != 2 {
return nil, errors.New("method Handle does not return two arguments")
}
returnValue := returnValues[1].Interface()
if returnValue == nil {
return returnValues[0].Interface(), nil
} else {
er, ok := returnValue.(error)
if ok {
return nil, er
}
return nil, errors.New("method Handle return argument not include error type")
}
}
}
func (h *handler) readRequest(ctx context.Context, service Service, req any) (function, srv, ctxV, reqV reflect.Value, err error) {
typ := reflect.TypeOf(service)
srv = reflect.ValueOf(service)
method, ok := typ.MethodByName("Handle")
if ok {
mType := method.Type
num := mType.NumIn()
if num == 3 {
ctxV = reflect.ValueOf(ctx)
reqV = reflect.ValueOf(req)
function = method.Func
} else {
err = errors.New("method Handle does not include three parameters")
}
} else {
err = errors.New("method Handle not implemented")
}
return
}
func (h *handler) ServerWater(ctx context.Context, req any) (resp any, err error) {
if len(h.finalizer) > 0 {
defer func() {
for _, fn := range h.finalizer {
fn(ctx, err)
}
}()
}
resp, err = h.e(ctx, req)
if err != nil {
h.l.Error(err.Error())
return nil, err
}
return resp, nil
}
func (h *handler) GetLogger() *slog.Logger {
return h.l
}