Documentation ¶
Index ¶
- Variables
- func DefaultCacheKeyLookup(req *Request) (string, string)
- func Reload(runtime *Runtime)
- func Start(runtime *Runtime)
- type AuthHandler
- type Cache
- type CacheKeyLookup
- type CacheStorage
- type CachedResponse
- type Deserializer
- type FakeLogger
- func (f *FakeLogger) Error(message string)
- func (f *FakeLogger) Errorf(format string, v ...interface{})
- func (f *FakeLogger) Info(message string)
- func (f *FakeLogger) Infof(format string, v ...interface{})
- func (f *FakeLogger) IsVerbose() bool
- func (f *FakeLogger) Verbose()
- func (f *FakeLogger) Warn(message string)
- func (f *FakeLogger) Warnf(format string, v ...interface{})
- type Fragment
- type Garnish
- type Handler
- type HydrateLoader
- type HydrateResponse
- func (r *HydrateResponse) AddHeader(key, value string) Response
- func (r *HydrateResponse) Cached() bool
- func (r *HydrateResponse) Close()
- func (r *HydrateResponse) ContentLength() int
- func (r *HydrateResponse) Deserialize(deserializer Deserializer) error
- func (r *HydrateResponse) Expire(at time.Time)
- func (r *HydrateResponse) Expires() time.Time
- func (r *HydrateResponse) Header() http.Header
- func (r *HydrateResponse) Serialize(serializer Serializer) error
- func (r *HydrateResponse) Size() int
- func (r *HydrateResponse) Status() int
- func (r *HydrateResponse) ToCacheable(expires time.Time) CachedResponse
- func (r *HydrateResponse) Write(runtime *Runtime, w io.Writer)
- type LiteralFragment
- type Logger
- func (l *Logger) Error(message string)
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Info(message string)
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) IsVerbose() bool
- func (l *Logger) Verbose()
- func (l *Logger) Warn(message string)
- func (l *Logger) Warnf(format string, v ...interface{})
- type Logs
- type Middleware
- type MultiTransportUpstream
- type NormalResponse
- func (r *NormalResponse) AddHeader(key, value string) Response
- func (r *NormalResponse) Cached() bool
- func (r *NormalResponse) Close()
- func (r *NormalResponse) ContentLength() int
- func (r *NormalResponse) Deserialize(deserializer Deserializer) error
- func (r *NormalResponse) Expire(at time.Time)
- func (r *NormalResponse) Expires() time.Time
- func (r *NormalResponse) Header() http.Header
- func (r *NormalResponse) Serialize(serializer Serializer) error
- func (r *NormalResponse) Size() int
- func (r *NormalResponse) Status() int
- func (r *NormalResponse) ToCacheable(expires time.Time) CachedResponse
- func (r *NormalResponse) Write(runtime *Runtime, w io.Writer)
- type PurgeHandler
- type ReferenceFragment
- type Reporter
- type ReqBuilder
- type Request
- func (r *Request) Body() []byte
- func (r *Request) Cacheable() bool
- func (r *Request) Cached(reason string)
- func (r *Request) Clone() *Request
- func (r *Request) Close()
- func (r *Request) Error(message string)
- func (r *Request) Errorf(format string, args ...interface{})
- func (r *Request) FatalResponse(message string) Response
- func (r *Request) FatalResponseErr(message string, err error) Response
- func (r *Request) Info(message string)
- func (r *Request) Infof(format string, args ...interface{})
- func (r *Request) Params(key string) string
- func (r *Request) Q(key string) string
- type RequestTweaker
- type Response
- func Empty(status int) Response
- func EmptyH(status int, header http.Header) Response
- func Json(status int, body interface{}) Response
- func Respond(status int, body interface{}) Response
- func RespondH(status int, header http.Header, body interface{}) Response
- func Streaming(status int, header http.Header, contentLength int64, body io.ReadCloser) Response
- type Route
- type RouteCache
- type RouteStats
- type Runtime
- type Serializer
- type SingleTransportUpstream
- type Snapshot
- type StatsWorker
- type StreamingResponse
- func (r *StreamingResponse) AddHeader(key, value string) Response
- func (r *StreamingResponse) Cached() bool
- func (r *StreamingResponse) Close()
- func (r *StreamingResponse) ContentLength() int
- func (r *StreamingResponse) Header() http.Header
- func (r *StreamingResponse) Status() int
- func (r *StreamingResponse) ToCacheable(expires time.Time) CachedResponse
- func (r *StreamingResponse) Write(runtime *Runtime, w io.Writer)
- type Transport
- type Upstream
Constants ¶
This section is empty.
Variables ¶
var ( PurgeHitResponse = Empty(200) PurgeMissResponse = Empty(204) NotModifiedResponse = Empty(304) )
var ( EmptyOk = Empty(200) EmptyCreated = Empty(201) EmptyInvalidRequest = Empty(401) EmptyNotFound = Empty(404) EmptyServerError = Empty(500) )
var DefaultUserAgent = []string{""}
The User Agent to send to the upstream
var (
EmptyParams = params.New(0)
)
var STATS_PERCENTILES = map[string]float64{"75p": 0.75, "95p": 0.95}
The percentiles to measure. The key is used as the stat name.
var STATS_SAMPLE_SIZE int64 = 1000
The number of samples to keep
var STATS_SAMPLE_SIZE_F = float64(STATS_SAMPLE_SIZE)
The number of samples to keep as a float
var (
)Functions ¶
func DefaultCacheKeyLookup ¶
Types ¶
type AuthHandler ¶
Authorization / authentication handler
type Cache ¶
type Cache struct { sync.Mutex Storage CacheStorage Saint bool GraceTTL time.Duration PurgeHandler PurgeHandler // contains filtered or unexported fields }
type CacheKeyLookup ¶
A function that generates cache keys from a request
type CacheStorage ¶
type CacheStorage interface { Get(primary, secondary string) CachedResponse Set(primary string, secondary string, response CachedResponse) Delete(primary, secondary string) bool DeleteAll(primary string) bool Save(path string, count int, cutoff time.Duration) error Load(path string) error SetSize(size int) GetSize() int Stop() }
type CachedResponse ¶
type CachedResponse interface { Response Size() int Expire(at time.Time) Expires() time.Time Serialize(serializer Serializer) error Deserialize(deserializer Deserializer) error }
type Deserializer ¶
type FakeLogger ¶
func NewFakeLogger ¶
func NewFakeLogger() *FakeLogger
func (*FakeLogger) Error ¶
func (f *FakeLogger) Error(message string)
func (*FakeLogger) Errorf ¶
func (f *FakeLogger) Errorf(format string, v ...interface{})
func (*FakeLogger) Info ¶
func (f *FakeLogger) Info(message string)
func (*FakeLogger) Infof ¶
func (f *FakeLogger) Infof(format string, v ...interface{})
func (*FakeLogger) IsVerbose ¶
func (f *FakeLogger) IsVerbose() bool
func (*FakeLogger) Verbose ¶
func (f *FakeLogger) Verbose()
func (*FakeLogger) Warn ¶
func (f *FakeLogger) Warn(message string)
func (*FakeLogger) Warnf ¶
func (f *FakeLogger) Warnf(format string, v ...interface{})
type Handler ¶
func WrapMiddleware ¶
func WrapMiddleware(name string, m Middleware, next Handler) Handler
type HydrateLoader ¶
type HydrateLoader func(fragment ReferenceFragment) []byte
type HydrateResponse ¶
type HydrateResponse struct {
// contains filtered or unexported fields
}
func NewHydraterResponse ¶
func NewHydraterResponse(status int, header http.Header, fragments []Fragment) *HydrateResponse
func (*HydrateResponse) AddHeader ¶
func (r *HydrateResponse) AddHeader(key, value string) Response
func (*HydrateResponse) Cached ¶
func (r *HydrateResponse) Cached() bool
func (*HydrateResponse) Close ¶
func (r *HydrateResponse) Close()
func (*HydrateResponse) ContentLength ¶
func (r *HydrateResponse) ContentLength() int
func (*HydrateResponse) Deserialize ¶
func (r *HydrateResponse) Deserialize(deserializer Deserializer) error
func (*HydrateResponse) Expire ¶
func (r *HydrateResponse) Expire(at time.Time)
func (*HydrateResponse) Expires ¶
func (r *HydrateResponse) Expires() time.Time
func (*HydrateResponse) Header ¶
func (r *HydrateResponse) Header() http.Header
func (*HydrateResponse) Serialize ¶
func (r *HydrateResponse) Serialize(serializer Serializer) error
func (*HydrateResponse) Size ¶
func (r *HydrateResponse) Size() int
func (*HydrateResponse) Status ¶
func (r *HydrateResponse) Status() int
func (*HydrateResponse) ToCacheable ¶
func (r *HydrateResponse) ToCacheable(expires time.Time) CachedResponse
type LiteralFragment ¶
type LiteralFragment []byte
func (LiteralFragment) Render ¶
func (f LiteralFragment) Render(runtime *Runtime) []byte
func (LiteralFragment) Size ¶
func (f LiteralFragment) Size() int
type Logs ¶
type Logs interface { // Log an information message Info(message string) // Log an informational message using the specified format Infof(format string, v ...interface{}) // Log a warning message Warn(message string) // Log a warning message using the specified format Warnf(format string, v ...interface{}) // Log an error message Error(message string) // Log an error message using the specified format Errorf(format string, v ...interface{}) // Enable logging info messages Verbose() // Returns true if verbose logging is enabled IsVerbose() bool }
type Middleware ¶
type MultiTransportUpstream ¶
func (*MultiTransportUpstream) Headers ¶
func (u *MultiTransportUpstream) Headers() []string
func (*MultiTransportUpstream) Transport ¶
func (u *MultiTransportUpstream) Transport() *Transport
func (*MultiTransportUpstream) Tweaker ¶
func (u *MultiTransportUpstream) Tweaker() RequestTweaker
type NormalResponse ¶
type NormalResponse struct {
// contains filtered or unexported fields
}
A standard response. This response is used by the cache. It's also used when the upstream didn't provide a Content-Length, or whe the Content-Length was greater then the configured BytePool's capacity
func (*NormalResponse) AddHeader ¶
func (r *NormalResponse) AddHeader(key, value string) Response
func (*NormalResponse) Cached ¶
func (r *NormalResponse) Cached() bool
func (*NormalResponse) Close ¶
func (r *NormalResponse) Close()
func (*NormalResponse) ContentLength ¶
func (r *NormalResponse) ContentLength() int
func (*NormalResponse) Deserialize ¶
func (r *NormalResponse) Deserialize(deserializer Deserializer) error
func (*NormalResponse) Expire ¶
func (r *NormalResponse) Expire(at time.Time)
func (*NormalResponse) Expires ¶
func (r *NormalResponse) Expires() time.Time
func (*NormalResponse) Header ¶
func (r *NormalResponse) Header() http.Header
func (*NormalResponse) Serialize ¶
func (r *NormalResponse) Serialize(serializer Serializer) error
func (*NormalResponse) Size ¶
func (r *NormalResponse) Size() int
func (*NormalResponse) Status ¶
func (r *NormalResponse) Status() int
func (*NormalResponse) ToCacheable ¶
func (r *NormalResponse) ToCacheable(expires time.Time) CachedResponse
type PurgeHandler ¶
type PurgeHandler func(req *Request, lookup CacheKeyLookup, cache CacheStorage) Response
A function that purges the cache Returning a nil response means that the request will be forward onwards
type ReferenceFragment ¶
type ReferenceFragment struct { typed.Typed // contains filtered or unexported fields }
func NewReferenceFragment ¶
func NewReferenceFragment(data []byte) (ReferenceFragment, error)
func (ReferenceFragment) Render ¶
func (f ReferenceFragment) Render(runtime *Runtime) []byte
func (ReferenceFragment) Size ¶
func (f ReferenceFragment) Size() int
type ReqBuilder ¶
type ReqBuilder struct {
Request *Request
}
a request builder
func RB ¶
func RB() *ReqBuilder
func (*ReqBuilder) Param ¶
func (rb *ReqBuilder) Param(key, value string) *ReqBuilder
func (*ReqBuilder) Query ¶
func (rb *ReqBuilder) Query(key, value string) *ReqBuilder
type Request ¶
type Request struct { // wraps request.Url.Query without having to re-parse it on each request Query url.Values // the underlying bytepool for the body. Consumers should use the Body() method B *bytepool.Bytes // Every request has a unique id. This is forwarded to the upstreams in the X-Request-Id header Id string // The time the request started at (used by stats to track the time taken to process) Start time.Time // The actual *http.Request *http.Request // The route this request is associated with Route *Route // Garnish's runtime Runtime *Runtime // To be used by consumer as-needed, unused by Garnish itself. Context interface{} // contains filtered or unexported fields }
Extends an *http.Request
func (*Request) Body ¶
The request's body. This value is only available before the upstream is called (at which point it is drained)
func (*Request) Clone ¶
For now we don't clone the body. Clone is only used by the cache/grace right now, what are the chances that we want to cache a GET request with a body?
func (*Request) Close ¶
func (r *Request) Close()
Used internally to release any resources associated with the request
func (*Request) FatalResponse ¶
func (*Request) FatalResponseErr ¶
func (*Request) Infof ¶
Context-aware info message (only displayed if the global configuration has Debug logging enabled)
type RequestTweaker ¶
Tweaks request `out` before sending it to the upstream
type Response ¶
type Response interface { // The response's content length // Should return -1 when unknown ContentLength() int // Write out the response Write(runtime *Runtime, w io.Writer) // The status code Status() int // The headers Header() http.Header // Write a header AddHeader(key, value string) Response // Returns a cacheable version of this response // When detached is true, it's expected that the original // response will continue to be used. Detached = false is // an optimization for grace mode which discards the original response ToCacheable(ttl time.Time) CachedResponse // Releases any resources associated with the response Close() // Whether or not the response came from a cached source // (affects the cache stat) Cached() bool }
An http response
func Json ¶
Builds a response with the given status code and body The body can be a string, []byte, or io.ReadCloser. Will generate a generic Fatal (500) response for other types A Json response is the same as a normal response, except that the content-type is set to application/json
func Respond ¶
Builds a response with the given status code and body The body can be a string, []byte, or io.ReadCloser. Will generate a generic Fatal (500) response for other types
type Route ¶
type Route struct { Name string Method string Upstream Upstream Stats *RouteStats Cache *RouteCache StopHandler Handler FlowHandler Middleware }
type RouteCache ¶
type RouteCache struct { KeyLookup CacheKeyLookup TTL time.Duration }
func NewRouteCache ¶
func NewRouteCache(ttl time.Duration, keyLookup CacheKeyLookup) *RouteCache
type RouteStats ¶
Each route has its own stats. To avoid having to store potentially unlimited values to calculate percentiles, sampling is used. The total memory required for this is:
STATS_SAMPLE_SIZE * 2 * 64 * <NUMBER_OF_ROUTES>
func NewRouteStats ¶
func NewRouteStats(treshold time.Duration) *RouteStats
func (*RouteStats) Hit ¶
func (s *RouteStats) Hit(res Response, t time.Duration)
Called on each request
func (*RouteStats) Snapshot ¶
func (s *RouteStats) Snapshot() Snapshot
Get a snapsnot of this route's current stats Getting a snapshot resets all statistics
type Runtime ¶
type Runtime struct { Address string NotFoundResponse Response FatalResponse Response Executor Handler Upstreams map[string]Upstream Routes map[string]*Route Router *router.Router BytePool *bytepool.Pool StatsWorker *StatsWorker Cache *Cache Resolver *dnscache.Resolver HydrateLoader HydrateLoader }
All the data needed to serve requests Built automatically when the garnish.Start() is called
func (*Runtime) RegisterStats ¶
func (*Runtime) ReplaceWith ¶
type Serializer ¶
type SingleTransportUpstream ¶
type SingleTransportUpstream struct {
// contains filtered or unexported fields
}
func (*SingleTransportUpstream) Headers ¶
func (u *SingleTransportUpstream) Headers() []string
func (*SingleTransportUpstream) Transport ¶
func (u *SingleTransportUpstream) Transport() *Transport
func (*SingleTransportUpstream) Tweaker ¶
func (u *SingleTransportUpstream) Tweaker() RequestTweaker
type StatsWorker ¶
type StatsWorker struct {
// contains filtered or unexported fields
}
Background worker that persists the stats every minute
func NewStatsWorker ¶
func NewStatsWorker(runtime *Runtime, fileName string) *StatsWorker
func (*StatsWorker) Stop ¶
func (w *StatsWorker) Stop()
type StreamingResponse ¶
type StreamingResponse struct {
// contains filtered or unexported fields
}
A standard response. This response is used by the cache. It's also used when the upstream didn't provide a Content-Length, or whe the Content-Length was greater then the configured BytePool's capacity
func (*StreamingResponse) AddHeader ¶
func (r *StreamingResponse) AddHeader(key, value string) Response
func (*StreamingResponse) Cached ¶
func (r *StreamingResponse) Cached() bool
func (*StreamingResponse) Close ¶
func (r *StreamingResponse) Close()
func (*StreamingResponse) ContentLength ¶
func (r *StreamingResponse) ContentLength() int
func (*StreamingResponse) Header ¶
func (r *StreamingResponse) Header() http.Header
func (*StreamingResponse) Status ¶
func (r *StreamingResponse) Status() int
func (*StreamingResponse) ToCacheable ¶
func (r *StreamingResponse) ToCacheable(expires time.Time) CachedResponse
type Upstream ¶
type Upstream interface { Headers() []string Transport() *Transport Tweaker() RequestTweaker }
func CreateUpstream ¶
func CreateUpstream(headers []string, tweaker RequestTweaker, transports []*Transport) (Upstream, error)