Documentation ¶
Index ¶
- Constants
- func CopyHeaders(dst http.Header, src http.Header)
- func CopyURL(i *url.URL) *url.URL
- func DefaultLoggerDebugFunc() bool
- func DumpHttpRequest(req *http.Request) string
- func HasHeaders(names []string, headers http.Header) bool
- func NopWriteCloser(w io.Writer) io.WriteCloser
- func RemoveHeaders(headers http.Header, names ...string)
- type BasicAuth
- type BufferWriter
- type DefaultLogger
- type ErrorHandler
- type ErrorHandlerFunc
- type ExtractSource
- type ExtractorFunc
- type Logger
- type LoggerDebugFunc
- type ProxyWriter
- func (p *ProxyWriter) CloseNotify() <-chan bool
- func (p *ProxyWriter) Flush()
- func (p *ProxyWriter) GetLength() int64
- func (p *ProxyWriter) Header() http.Header
- func (p *ProxyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (p *ProxyWriter) StatusCode() int
- func (p *ProxyWriter) Write(buf []byte) (int, error)
- func (p *ProxyWriter) WriteHeader(code int)
- type SerializableHttpRequest
- type SourceExtractor
- type StdHandler
Constants ¶
const StatusClientClosedRequest = 499
StatusClientClosedRequest non-standard HTTP status code for client disconnection
const StatusClientClosedRequestText = "Client Closed Request"
StatusClientClosedRequestText non-standard HTTP status for client disconnection
Variables ¶
This section is empty.
Functions ¶
func CopyHeaders ¶
CopyHeaders copies http headers from source to destination, it does not overide, but adds multiple headers
func DefaultLoggerDebugFunc ¶
func DefaultLoggerDebugFunc() bool
DefaultLoggerDebugFunc is the default LoggerDebugFunc which returns false.
func DumpHttpRequest ¶
DumpHttpRequest dump a HTTP request to JSON
func HasHeaders ¶
HasHeaders determines whether any of the header names is present in the http headers
func NopWriteCloser ¶
func NopWriteCloser(w io.Writer) io.WriteCloser
NopWriteCloser returns a WriteCloser with a no-op Close method wrapping the provided Writer w.
func RemoveHeaders ¶
RemoveHeaders removes the header with the given names from the headers map
Types ¶
type BasicAuth ¶
BasicAuth basic auth information
func ParseAuthHeader ¶
ParseAuthHeader creates a new BasicAuth from header values
type BufferWriter ¶
type BufferWriter struct { H http.Header Code int W io.WriteCloser // contains filtered or unexported fields }
BufferWriter buffer writer
func NewBufferWriter ¶
func NewBufferWriter(w io.WriteCloser) *BufferWriter
NewBufferWriter creates a new BufferWriter
func NewBufferWriterWithLogger ¶
func NewBufferWriterWithLogger(w io.WriteCloser, log Logger, debug LoggerDebugFunc) *BufferWriter
NewBufferWriterWithLogger creates a new BufferWriter with a custom logger
func (*BufferWriter) CloseNotify ¶
func (b *BufferWriter) CloseNotify() <-chan bool
CloseNotify returns a channel that receives at most a single value (true) when the client connection has gone away.
func (*BufferWriter) Header ¶
func (b *BufferWriter) Header() http.Header
Header gets response header
func (*BufferWriter) Hijack ¶
func (b *BufferWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack lets the caller take over the connection.
func (*BufferWriter) WriteHeader ¶
func (b *BufferWriter) WriteHeader(code int)
WriteHeader writes status code
type DefaultLogger ¶
type DefaultLogger struct{}
func (*DefaultLogger) Debugf ¶
func (*DefaultLogger) Debugf(string, ...interface{})
func (*DefaultLogger) Errorf ¶
func (*DefaultLogger) Errorf(string, ...interface{})
func (*DefaultLogger) Fatalf ¶
func (*DefaultLogger) Fatalf(string, ...interface{})
func (*DefaultLogger) Infof ¶
func (*DefaultLogger) Infof(string, ...interface{})
func (*DefaultLogger) Warnf ¶
func (*DefaultLogger) Warnf(string, ...interface{})
type ErrorHandler ¶
type ErrorHandler interface {
ServeHTTP(w http.ResponseWriter, req *http.Request, err error)
}
ErrorHandler error handler
var DefaultHandler ErrorHandler = &StdHandler{}
DefaultHandler default error handler
type ErrorHandlerFunc ¶
type ErrorHandlerFunc func(http.ResponseWriter, *http.Request, error)
ErrorHandlerFunc error handler function type
func (ErrorHandlerFunc) ServeHTTP ¶
func (f ErrorHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, err error)
ServeHTTP calls f(w, r).
type ExtractSource ¶
ExtractSource extract source function type
type ExtractorFunc ¶
ExtractorFunc extractor function type
type Logger ¶
type Logger interface { Debugf(string, ...interface{}) Infof(string, ...interface{}) Warnf(string, ...interface{}) Errorf(string, ...interface{}) Fatalf(string, ...interface{}) }
Logger is a minimal logger interface which satisfies oxy logging needs.
type LoggerDebugFunc ¶
type LoggerDebugFunc func() bool
LoggerDebugFunc is a function that returns a boolean that tells whether or not oxy should generate debug call that can be costly.
If the logger you use is logrus, the function you should pass as option should look like this:
import ( "github.com/sirupsen/logrus" ) stdLogger := logrus.StandardLogger() stdLogger.SetLevel(logrus.DebugLevel) logrusLogger := stdLogger.WithField("lib", "vulcand/oxy") logrusDebugFunc := func() bool { return logrusLogger.Logger.Level >= logrus.DebugLevel } cbLogger := cbreaker.Logger(logrusLogger) cbDebug := cbreaker.Debug(logrusDebugFunc) cb, err := cbreaker.New(next, "NetworkErrorRatio() > 0.3", cbLogger, cbDebug)
If the logger you use is zap:
import ( "abstraction.fr/oxy/v2/cbreaker" "go.uber.org/zap/zap" "go.uber.org/zap/zapcore" ) zapAtomLevel := zap.NewAtomicLevel() zapAtomLevel.SetLevel(zapcore.DebugLevel) zapEncoderCfg := zap.NewProductionEncoderConfig() zapEncoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder zapCore := zapcore.NewCore(zapcore.NewConsoleEncoder(zapEncoderCfg), zapcore.Lock(os.Stdout), zapAtomLevel) zapLogger := zap.New(zapCore).With(zap.String("lib", "vulcand/oxy")) zapSugaredLogger := zapLogger.Sugar() zapDebug := func() bool { return zapAtomLevel.Enabled(zapcore.DebugLevel) } cbLogger := cbreaker.Logger(zapSugaredLogger) cbDebug := cbreaker.Debug(zapDebug) cb, err := cbreaker.New(next, "NetworkErrorRatio() > 0.3", cbLogger, cbDebug)
type ProxyWriter ¶
type ProxyWriter struct {
// contains filtered or unexported fields
}
ProxyWriter calls recorder, used to debug logs
func NewProxyWriter ¶
func NewProxyWriter(w http.ResponseWriter) *ProxyWriter
NewProxyWriter creates a new ProxyWriter
func NewProxyWriterWithLogger ¶
func NewProxyWriterWithLogger(w http.ResponseWriter, logger Logger, debug LoggerDebugFunc) *ProxyWriter
NewProxyWriterWithLogger creates a new ProxyWriter
func (*ProxyWriter) CloseNotify ¶
func (p *ProxyWriter) CloseNotify() <-chan bool
CloseNotify returns a channel that receives at most a single value (true) when the client connection has gone away.
func (*ProxyWriter) GetLength ¶
func (p *ProxyWriter) GetLength() int64
GetLength gets content length
func (*ProxyWriter) Hijack ¶
func (p *ProxyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack lets the caller take over the connection.
func (*ProxyWriter) WriteHeader ¶
func (p *ProxyWriter) WriteHeader(code int)
WriteHeader writes status code
type SerializableHttpRequest ¶
type SerializableHttpRequest struct { Method string URL *url.URL Proto string // "HTTP/1.0" ProtoMajor int // 1 ProtoMinor int // 0 Header http.Header ContentLength int64 TransferEncoding []string Host string Form url.Values PostForm url.Values MultipartForm *multipart.Form Trailer http.Header RemoteAddr string RequestURI string TLS *tls.ConnectionState }
SerializableHttpRequest serializable HTTP request
func (*SerializableHttpRequest) ToJson ¶
func (s *SerializableHttpRequest) ToJson() string
ToJson serializes to JSON
type SourceExtractor ¶
type SourceExtractor interface {
Extract(req *http.Request) (token string, amount int64, err error)
}
SourceExtractor extracts the source from the request, e.g. that may be client ip, or particular header that identifies the source. amount stands for amount of connections the source consumes, usually 1 for connection limiters error should be returned when source can not be identified
func NewExtractor ¶
func NewExtractor(variable string) (SourceExtractor, error)
NewExtractor creates a new SourceExtractor
type StdHandler ¶
type StdHandler struct{}
StdHandler Standard error handler
func (*StdHandler) ServeHTTP ¶
func (e *StdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request, err error)