-
Notifications
You must be signed in to change notification settings - Fork 1
/
launcher.go
175 lines (141 loc) · 3.27 KB
/
launcher.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
package main
import (
"errors"
"net/http"
"sync"
"time"
"github.com/labstack/echo/v4"
)
type Cache struct {
mu sync.Mutex
target *Target
exp time.Time
}
func (c *Cache) Get() (Target, bool) {
c.mu.Lock()
defer c.mu.Unlock()
if c.target == nil || time.Now().After(c.exp) {
return Target{}, false
}
return *c.target, true
}
func (c *Cache) Set(target *Target, exp time.Time) {
c.mu.Lock()
defer c.mu.Unlock()
c.target = target
c.exp = exp
}
func (c *Cache) ClearIfSame(t *Target) {
c.mu.Lock()
defer c.mu.Unlock()
if *t.URL == *c.target.URL {
c.target = nil
}
}
type Launcher struct {
cache *Cache
lmu sync.Mutex
client InstanceClient
alarmClient AlarmClient
cacheTtl time.Duration
launchWait time.Duration
}
func NewLauncerFromConfig(c *Config) *Launcher {
cli := NewInstanceClientFromConfig(c)
alarmClient := NewAlarmClientFromConfig(c)
return &Launcher{
cache: new(Cache),
cacheTtl: c.CacheTtl,
client: cli,
alarmClient: alarmClient,
launchWait: c.WaitTime,
}
}
func (l *Launcher) HandleProxyError() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
t, tok := c.Get("target").(*Target)
err := next(c)
if err == nil {
return nil
}
he, ok := err.(*echo.HTTPError)
if !ok {
return err
}
if he.Code != http.StatusBadGateway && he.Code != http.StatusServiceUnavailable {
return err
}
c.Logger().Debugf("Got HTTP error: %v", he)
if !tok {
c.Logger().Debug("Cannot find target in context")
return err
}
ok, err = l.client.CheckInstance(t.Instance)
if err != nil {
return err
}
if !ok {
l.cache.ClearIfSame(t)
return c.Redirect(http.StatusTemporaryRedirect, c.Request().URL.Path)
}
return c.Render(http.StatusServiceUnavailable, "RefreshTemplate", RefreshPageParams{
Title: "503 Service Unavailable",
Message: "Sorry, the server may not be ready right now.",
Emoji: "😔",
Seconds: 21,
})
}
}
}
func (l *Launcher) Launch() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var t Target
t, ok := l.cache.Get()
if ok {
c.Set("target", &t)
return next(c)
}
t, created, err := l.getInstance(c)
if err != nil {
return err
}
err = l.alarmClient.AutoTerminate(c, &t)
if err != nil {
return err
}
c.Set("target", &t)
if created && l.launchWait > 0 {
return c.Render(http.StatusOK, "RefreshTemplate", RefreshPageParams{
Title: "Launcher is on it",
Message: "The server is being initialized.",
Emoji: "🤔",
Seconds: int(l.launchWait / time.Second),
})
}
return next(c)
}
}
}
func (l *Launcher) getInstance(c echo.Context) (Target, bool, error) {
l.lmu.Lock()
defer l.lmu.Unlock()
if t, ok := l.cache.Get(); ok {
return t, false, nil
}
t, err := l.client.FindInstance(c)
if err != nil && !errors.Is(err, errNotFound) {
return Target{}, false, err
}
if err == nil {
l.cache.Set(t, time.Now().Add(l.cacheTtl))
return *t, false, nil
}
t, err = l.client.LaunchInstance(c)
if err != nil {
return Target{}, false, err
}
l.cache.Set(t, time.Now().Add(l.cacheTtl))
return *t, true, nil
}