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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
package stream
import (
"fmt"
"net/http"
"github.com/vulcand/predicate"
)
type hpredicate func(*context) bool
// IsValidExpression check if it's a valid expression.
func IsValidExpression(expr string) bool {
_, err := parseExpression(expr)
return err == nil
}
// Parses expression in the go language into Failover predicates.
func parseExpression(in string) (hpredicate, error) {
p, err := predicate.NewParser(predicate.Def{
Operators: predicate.Operators{
AND: and,
OR: or,
EQ: eq,
NEQ: neq,
LT: lt,
GT: gt,
LE: le,
GE: ge,
},
Functions: map[string]interface{}{
"RequestMethod": requestMethod,
"IsNetworkError": isNetworkError,
"Attempts": attempts,
"ResponseCode": responseCode,
},
})
if err != nil {
return nil, err
}
out, err := p.Parse(in)
if err != nil {
return nil, err
}
pr, ok := out.(hpredicate)
if !ok {
return nil, fmt.Errorf("expected predicate, got %T", out)
}
return pr, nil
}
// IsNetworkError returns a predicate that returns true if last attempt ended with network error.
func isNetworkError() hpredicate {
return func(c *context) bool {
return c.responseCode == http.StatusBadGateway || c.responseCode == http.StatusGatewayTimeout
}
}
// and returns predicate by joining the passed predicates with logical 'and'.
func and(fns ...hpredicate) hpredicate {
return func(c *context) bool {
for _, fn := range fns {
if !fn(c) {
return false
}
}
return true
}
}
// or returns predicate by joining the passed predicates with logical 'or'.
func or(fns ...hpredicate) hpredicate {
return func(c *context) bool {
for _, fn := range fns {
if fn(c) {
return true
}
}
return false
}
}
// not creates negation of the passed predicate.
func not(p hpredicate) hpredicate {
return func(c *context) bool {
return !p(c)
}
}
// eq returns predicate that tests for equality of the value of the mapper and the constant.
func eq(m interface{}, value interface{}) (hpredicate, error) {
switch mapper := m.(type) {
case toString:
return stringEQ(mapper, value)
case toInt:
return intEQ(mapper, value)
}
return nil, fmt.Errorf("unsupported argument: %T", m)
}
// neq returns predicate that tests for inequality of the value of the mapper and the constant.
func neq(m interface{}, value interface{}) (hpredicate, error) {
p, err := eq(m, value)
if err != nil {
return nil, err
}
return not(p), nil
}
// lt returns predicate that tests that value of the mapper function is less than the constant.
func lt(m interface{}, value interface{}) (hpredicate, error) {
switch mapper := m.(type) {
case toInt:
return intLT(mapper, value)
default:
return nil, fmt.Errorf("unsupported argument: %T", m)
}
}
// le returns predicate that tests that value of the mapper function is less or equal than the constant.
func le(m interface{}, value interface{}) (hpredicate, error) {
l, err := lt(m, value)
if err != nil {
return nil, err
}
e, err := eq(m, value)
if err != nil {
return nil, err
}
return func(c *context) bool {
return l(c) || e(c)
}, nil
}
// gt returns predicate that tests that value of the mapper function is greater than the constant.
func gt(m interface{}, value interface{}) (hpredicate, error) {
switch mapper := m.(type) {
case toInt:
return intGT(mapper, value)
default:
return nil, fmt.Errorf("unsupported argument: %T", m)
}
}
// ge returns predicate that tests that value of the mapper function is less or equal than the constant.
func ge(m interface{}, value interface{}) (hpredicate, error) {
g, err := gt(m, value)
if err != nil {
return nil, err
}
e, err := eq(m, value)
if err != nil {
return nil, err
}
return func(c *context) bool {
return g(c) || e(c)
}, nil
}
func stringEQ(m toString, val interface{}) (hpredicate, error) {
value, ok := val.(string)
if !ok {
return nil, fmt.Errorf("expected string, got %T", val)
}
return func(c *context) bool {
return m(c) == value
}, nil
}
func intEQ(m toInt, val interface{}) (hpredicate, error) {
value, ok := val.(int)
if !ok {
return nil, fmt.Errorf("expected int, got %T", val)
}
return func(c *context) bool {
return m(c) == value
}, nil
}
func intLT(m toInt, val interface{}) (hpredicate, error) {
value, ok := val.(int)
if !ok {
return nil, fmt.Errorf("expected int, got %T", val)
}
return func(c *context) bool {
return m(c) < value
}, nil
}
func intGT(m toInt, val interface{}) (hpredicate, error) {
value, ok := val.(int)
if !ok {
return nil, fmt.Errorf("expected int, got %T", val)
}
return func(c *context) bool {
return m(c) > value
}, nil
}
type context struct {
r *http.Request
attempt int
responseCode int
}
type toString func(c *context) string
type toInt func(c *context) int
// RequestMethod returns mapper of the request to its method e.g. POST.
func requestMethod() toString {
return func(c *context) string {
return c.r.Method
}
}
// Attempts returns mapper of the request to the number of proxy attempts.
func attempts() toInt {
return func(c *context) int {
return c.attempt
}
}
// ResponseCode returns mapper of the request to the last response code, returns 0 if there was no response code.
func responseCode() toInt {
return func(c *context) int {
return c.responseCode
}
}
|