forked from ironsmile/nedomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
38 lines (30 loc) · 1.05 KB
/
new.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
package balancing
import (
"fmt"
"github.com/ironsmile/nedomi/types"
"github.com/ironsmile/nedomi/upstream/balancing/unweighted"
"github.com/ironsmile/nedomi/upstream/balancing/weighted"
)
// Package balancing deals with algorithms that ballance HTTP requests between
// different upstream servers.
const unweightedPrefix = "unweighted-"
var allAlgorithms = map[string]func() types.UpstreamBalancingAlgorithm{}
func init() {
for id, algo := range weighted.Algorithms {
allAlgorithms[id] = algo
}
for id, algo := range unweighted.Algorithms {
allAlgorithms[unweightedPrefix+id] = algo
}
}
// New creates and returns a new balancing algorithm based on its ID. It uses
// the `allAlgorithms` map which is the union of all weighted and unweighed
// algorithm implementations. Their maps are generated by running `go generate`
// in the weighted and unweighted subfolders.
func New(t string) (types.UpstreamBalancingAlgorithm, error) {
fnc, ok := allAlgorithms[t]
if !ok {
return nil, fmt.Errorf("No such balancing algorithm: %s", t)
}
return fnc(), nil
}