Skip to content

Commit 59210a2

Browse files
Graceful error when no upstreams (#47)
1 parent 64026b3 commit 59210a2

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

api/upstream.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func (r *roundrobin) get() string {
2121
r.Lock()
2222
defer r.Unlock()
2323

24+
if len(r.pool) == 0 {
25+
return ""
26+
}
27+
2428
if r.current >= len(r.pool) {
2529
r.current %= len(r.pool)
2630
}
@@ -32,6 +36,12 @@ func (r *roundrobin) get() string {
3236

3337
// Modified BalancerForward to add upstream server to fiber context
3438
func BalancerForward(servers []string, clients ...*fasthttp.Client) fiber.Handler {
39+
if len(servers) == 0 {
40+
return func(c *fiber.Ctx) error {
41+
return c.SendStatus(fiber.StatusServiceUnavailable)
42+
}
43+
}
44+
3545
r := &roundrobin{
3646
current: 0,
3747
pool: servers,

0 commit comments

Comments
 (0)