-
Notifications
You must be signed in to change notification settings - Fork 53
/
endpoint.go
61 lines (49 loc) · 1.56 KB
/
endpoint.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
package weaver
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gojektech/weaver/pkg/matcher"
"github.com/pkg/errors"
)
// EndpointConfig - Defines a config for external service
type EndpointConfig struct {
Matcher string `json:"matcher"`
ShardExpr string `json:"shard_expr"`
ShardFunc string `json:"shard_func"`
ShardConfig json.RawMessage `json:"shard_config"`
}
func (endpointConfig *EndpointConfig) genShardKeyFunc() (shardKeyFunc, error) {
matcherFunc, found := matcher.New(endpointConfig.Matcher)
if !found {
return nil, errors.WithStack(fmt.Errorf("failed to find a matcherMux for: %s", endpointConfig.Matcher))
}
return func(req *http.Request) (string, error) {
return matcherFunc(req, endpointConfig.ShardExpr)
}, nil
}
type Endpoint struct {
sharder Sharder
shardKeyFunc shardKeyFunc
}
func NewEndpoint(endpointConfig *EndpointConfig, sharder Sharder) (*Endpoint, error) {
if sharder == nil {
return nil, errors.New("nil sharder passed in")
}
shardKeyFunc, err := endpointConfig.genShardKeyFunc()
if err != nil {
return nil, errors.Wrapf(err, "failed to generate shardKeyFunc for %s", endpointConfig.ShardExpr)
}
return &Endpoint{
sharder: sharder,
shardKeyFunc: shardKeyFunc,
}, nil
}
func (endpoint *Endpoint) Shard(request *http.Request) (*Backend, error) {
shardKey, err := endpoint.shardKeyFunc(request)
if err != nil {
return nil, errors.Wrapf(err, "failed to find shardKey")
}
return endpoint.sharder.Shard(shardKey)
}
type shardKeyFunc func(*http.Request) (string, error)