-
Notifications
You must be signed in to change notification settings - Fork 341
/
key.go
37 lines (31 loc) · 1.05 KB
/
key.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
package consistenthash
import (
"github.com/zalando/skipper/eskip"
"github.com/zalando/skipper/filters"
"github.com/zalando/skipper/loadbalancer"
)
type consistentHashKey struct {
template *eskip.Template
}
// NewConsistentHashKey creates a filter Spec, whose instances
// set the request key used by the `consistentHash` algorithm to select backend endpoint
func NewConsistentHashKey() filters.Spec { return &consistentHashKey{} }
func (*consistentHashKey) Name() string {
return filters.ConsistentHashKeyName
}
func (*consistentHashKey) CreateFilter(args []interface{}) (filters.Filter, error) {
if len(args) != 1 {
return nil, filters.ErrInvalidFilterParameters
}
value, ok := args[0].(string)
if !ok {
return nil, filters.ErrInvalidFilterParameters
}
return &consistentHashKey{eskip.NewTemplate(value)}, nil
}
func (c *consistentHashKey) Request(ctx filters.FilterContext) {
if key, ok := c.template.ApplyContext(ctx); ok {
ctx.StateBag()[loadbalancer.ConsistentHashKey] = key
}
}
func (*consistentHashKey) Response(filters.FilterContext) {}