-
Notifications
You must be signed in to change notification settings - Fork 12
/
flywheel.go
235 lines (203 loc) · 5.54 KB
/
flywheel.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package flywheel
import (
"encoding/json"
"io"
"log"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
)
// SpinINTERVAL determines how often flywheel will update its
// internal state and/or check for idle timeouts
const SpinINTERVAL = time.Second
// Ping - HTTP requests "ping" the flywheel goroutine. This updates the idle timeout,
// and returns the current status to the http request.
type Ping struct {
replyTo chan Pong
setTimeout time.Duration
requestStart bool
requestStop bool
noop bool
}
// Pong - result of the ping request
type Pong struct {
Status Status `json:"-"`
StatusName string `json:"status"`
Err error `json:"error,omitempty"`
LastStarted time.Time `json:"last-started,omitempty"`
LastStopped time.Time `json:"last-stopped,omitempty"`
StopAt time.Time `json:"stop-due-at"`
}
// Flywheel struct holds all the state required by the flywheel goroutine.
type Flywheel struct {
config *Config
running bool
pings chan Ping
status Status
ready bool
stopAt time.Time
lastStarted time.Time
lastStopped time.Time
ec2 *ec2.EC2
autoscaling *autoscaling.AutoScaling
hcInterval time.Duration
idleTimeout time.Duration
}
// New - Create new Flywheel type
func New(config *Config) *Flywheel {
awsConfig := &aws.Config{Region: &config.Region}
sess := session.New(awsConfig)
return &Flywheel{
hcInterval: time.Duration(config.HcInterval),
idleTimeout: time.Duration(config.IdleTimeout),
config: config,
pings: make(chan Ping),
stopAt: time.Now(),
ec2: ec2.New(sess),
autoscaling: autoscaling.New(sess),
}
}
// ProxyEndpoint - retrieve the reverse proxy destination
func (fw *Flywheel) ProxyEndpoint(hostname string) string {
vhost, ok := fw.config.Vhosts[hostname]
if ok {
return vhost
}
return fw.config.Endpoint
}
// Spin - Runs the main loop for the Flywheel.
func (fw *Flywheel) Spin() {
hchan := make(chan Status, 1)
go fw.HealthWatcher(hchan)
ticker := time.NewTicker(SpinINTERVAL)
for {
select {
case ping := <-fw.pings:
fw.RecvPing(&ping)
case <-ticker.C:
fw.Poll()
case status := <-hchan:
if fw.status != status {
log.Printf("Healthcheck - status changed from %v to %v", fw.status, status)
// Status may change from STARTED to UNHEALTHY to STARTED due
// to things like AWS RequestLimitExceeded errors.
// If there is an active timeout, keep it instead of resetting.
if status == STARTED && fw.stopAt.Before(time.Now()) {
fw.stopAt = time.Now().Add(fw.idleTimeout)
log.Printf("Timer update. Stop scheduled for %v", fw.stopAt)
}
fw.status = status
}
}
}
}
// RecvPing - process user ping requests and update state if needed
func (fw *Flywheel) RecvPing(ping *Ping) {
var pong Pong
ch := ping.replyTo
defer close(ch)
switch fw.status {
case STOPPED:
if ping.requestStart {
pong.Err = fw.Start()
}
case STARTED:
if ping.noop {
// Status requests, etc. Don't update idle timer
} else if ping.requestStop {
pong.Err = fw.Stop()
} else if int64(ping.setTimeout) != 0 {
fw.stopAt = time.Now().Add(ping.setTimeout)
log.Printf("Timer update. Stop scheduled for %v", fw.stopAt)
} else {
fw.stopAt = time.Now().Add(fw.idleTimeout)
log.Printf("Timer update. Stop scheduled for %v", fw.stopAt)
}
}
pong.Status = fw.status
pong.StatusName = fw.status.String()
pong.LastStarted = fw.lastStarted
pong.LastStopped = fw.lastStopped
pong.StopAt = fw.stopAt
ch <- pong
}
// Poll - The periodic check for starting/stopping state transitions and idle
// timeouts
func (fw *Flywheel) Poll() {
switch fw.status {
case STARTED:
if time.Now().After(fw.stopAt) {
fw.Stop()
log.Print("Idle timeout - shutting down")
fw.status = STOPPING
}
case STOPPING:
if fw.ready {
log.Print("Shutdown complete")
fw.status = STOPPED
}
case STARTING:
if fw.ready {
fw.status = STARTED
fw.stopAt = time.Now().Add(fw.idleTimeout)
log.Printf("Startup complete. Stop scheduled for %v", fw.stopAt)
}
}
}
// WriteStatusFile - Before we exit the application we write the current state
func (fw *Flywheel) WriteStatusFile(statusFile string) {
var pong Pong
fd, err := os.OpenFile(statusFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Unable to write status file: %s", err)
return
}
defer fd.Close()
pong.Status = fw.status
pong.StatusName = fw.status.String()
pong.LastStarted = fw.lastStarted
pong.LastStopped = fw.lastStopped
buf, err := json.Marshal(pong)
if err != nil {
log.Printf("Unable to write status file: %s", err)
return
}
_, err = fd.Write(buf)
if err != nil {
log.Printf("Unable to write status file: %s", err)
return
}
}
// ReadStatusFile load status from the status file
func (fw *Flywheel) ReadStatusFile(statusFile string) {
fd, err := os.Open(statusFile)
if err != nil {
if err != os.ErrNotExist {
log.Printf("Unable to load status file: %v", err)
}
return
}
stat, err := fd.Stat()
if err != nil {
log.Printf("Unable to load status file: %v", err)
return
}
buf := make([]byte, int(stat.Size()))
_, err = io.ReadFull(fd, buf)
if err != nil {
log.Printf("Unable to load status file: %v", err)
return
}
var status Pong
err = json.Unmarshal(buf, &status)
if err != nil {
log.Printf("Unable to load status file: %v", err)
return
}
fw.status = status.Status
fw.lastStarted = status.LastStarted
fw.lastStopped = status.LastStopped
}