-
Notifications
You must be signed in to change notification settings - Fork 53
/
dispatcher.go
154 lines (128 loc) · 3.26 KB
/
dispatcher.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gentleman
import (
c "gopkg.in/h2non/gentleman.v2/context"
)
// Dispatcher dispatches a given request triggering the middleware
// layer per specific phase and handling the request/response/error
// states accondingly.
type Dispatcher struct {
req *Request
}
// task function represents the required interface for dispatcher pipeline tasks.
type task func(*c.Context) (*c.Context, bool)
// NewDispatcher creates a new Dispatcher based on the given Context.
func NewDispatcher(req *Request) *Dispatcher {
return &Dispatcher{req}
}
// Dispatch triggers the middleware chains and performs the HTTP request.
func (d *Dispatcher) Dispatch() *c.Context {
// Pipeline of tasks to execute in FIFO order
pipeline := []task{
func(ctx *c.Context) (*c.Context, bool) {
return d.runBefore("request", ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runBefore("before dial", ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.doDial(ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runAfter("after dial", ctx)
},
func(ctx *c.Context) (*c.Context, bool) {
return d.runAfter("response", ctx)
},
}
// Reference to initial context
ctx := d.req.Context
// Execute tasks in order, stopping in case of error or explicit stop.
for _, task := range pipeline {
var stop bool
if ctx, stop = task(ctx); stop {
break
}
}
return ctx
}
func (d *Dispatcher) doDial(ctx *c.Context) (*c.Context, bool) {
// Perform the request via ctx.Client
res, err := ctx.Client.Do(ctx.Request)
ctx.Error = err
if err != nil {
ctx = d.req.Middleware.Run("error", ctx)
if ctx.Error != nil {
return ctx, true
}
}
// Assign response if present
if res != nil {
ctx.Response = res
}
return ctx, false
}
func (d *Dispatcher) runBefore(phase string, ctx *c.Context) (*c.Context, bool) {
// Run the request middleware
ctx, stop := d.run(phase, ctx)
if stop {
return ctx, true
}
// Verify if the response was injected
ctx, stop = d.intercepted(ctx)
if stop {
return ctx, true
}
// Verify if the should stop
return d.stop(ctx)
}
func (d *Dispatcher) runAfter(phase string, ctx *c.Context) (*c.Context, bool) {
// Trigger the after dial phase
ctx, stop := d.run(phase, ctx)
if stop {
return ctx, true
}
// Verify if the should stop
return d.stop(ctx)
}
func (d *Dispatcher) intercepted(ctx *c.Context) (*c.Context, bool) {
// Verify if the request was intercepted
if ctx.Response.StatusCode == 0 {
return ctx, false
}
// Trigger the intercept middleware
ctx, stop := d.run("intercept", ctx)
if stop {
return ctx, true
}
// Finally trigger the response middleware
ctx, _ = d.run("response", ctx)
return ctx, true
}
func (d *Dispatcher) stop(ctx *c.Context) (*c.Context, bool) {
if !ctx.Stopped {
return ctx, false
}
mw := d.req.Middleware
ctx = mw.Run("stop", ctx)
if ctx.Error != nil {
ctx = mw.Run("error", ctx)
if ctx.Error != nil {
return ctx, true
}
}
return ctx, ctx.Stopped
}
func (d *Dispatcher) run(phase string, ctx *c.Context) (*c.Context, bool) {
mw := d.req.Middleware
// Run the middleware by phase
ctx = mw.Run(phase, ctx)
if ctx.Error == nil {
return ctx, false
}
// Run error middleware
ctx = mw.Run("error", ctx)
if ctx.Error != nil {
return ctx, true
}
return ctx, false
}