Switch to golang/dep.

This commit is contained in:
Ludovic Fernandez 2018-01-09 21:46:04 +01:00 committed by Traefiker
parent d88554fa92
commit 044d87d96d
239 changed files with 42372 additions and 7011 deletions

View file

@ -1,92 +0,0 @@
/*
package stream provides http.Handler middleware that passes-through the entire request
Stream works around several limitations caused by buffering implementations, but
also introduces certain risks.
Workarounds for buffering limitations:
1. Streaming really large chunks of data (large file transfers, or streaming videos,
etc.)
2. Streaming (chunking) sparse data. For example, an implementation might
send a health check or a heart beat over a long-lived connection. This
does not play well with buffering.
Risks:
1. Connections could survive for very long periods of time.
2. There is no easy way to enforce limits on size/time of a connection.
Examples of a streaming middleware:
// sample HTTP handler
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("hello"))
})
// Stream will literally pass through to the next handler without ANY buffering
// or validation of the data.
stream.New(handler)
*/
package stream
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/vulcand/oxy/utils"
)
const (
// No limit by default
DefaultMaxBodyBytes = -1
)
// Stream is responsible for buffering requests and responses
// It buffers large requests and responses to disk,
type Stream struct {
maxRequestBodyBytes int64
maxResponseBodyBytes int64
retryPredicate hpredicate
next http.Handler
errHandler utils.ErrorHandler
}
// New returns a new streamer middleware. New() function supports optional functional arguments
func New(next http.Handler, setters ...optSetter) (*Stream, error) {
strm := &Stream{
next: next,
maxRequestBodyBytes: DefaultMaxBodyBytes,
maxResponseBodyBytes: DefaultMaxBodyBytes,
}
for _, s := range setters {
if err := s(strm); err != nil {
return nil, err
}
}
return strm, nil
}
type optSetter func(s *Stream) error
// Wrap sets the next handler to be called by stream handler.
func (s *Stream) Wrap(next http.Handler) error {
s.next = next
return nil
}
func (s *Stream) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if log.GetLevel() >= log.DebugLevel {
logEntry := log.WithField("Request", utils.DumpHttpRequest(req))
logEntry.Debug("vulcand/oxy/stream: begin ServeHttp on request")
defer logEntry.Debug("vulcand/oxy/stream: competed ServeHttp on request")
}
s.next.ServeHTTP(w, req)
}

View file

@ -1,225 +0,0 @@
package stream
import (
"fmt"
"net/http"
"github.com/vulcand/predicate"
)
func IsValidExpression(expr string) bool {
_, err := parseExpression(expr)
return err == nil
}
type context struct {
r *http.Request
attempt int
responseCode int
}
type hpredicate func(*context) bool
// 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
}
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
}
}
// 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)
}
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)
}
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
}