httpcache: github.com/geekypanda/httpcache/nethttp Index | Files | Directories

package nethttp

import "github.com/geekypanda/httpcache/nethttp"

Index

Package Files

client.go handler.go response_recorder.go ruleset.go utils.go

Variables

var Client = &http.Client{Timeout: cfg.RequestCacheTimeout}

Client is used inside the global Request function this client is an exported to give you a freedom of change its Transport, Timeout and so on(in case of ssl)

var DefaultRuleSet = rule.Chained(

    rule.HeaderClaim(ruleset.AuthorizationRule),

    rule.HeaderClaim(ruleset.MustRevalidateRule),
    rule.HeaderClaim(ruleset.ZeroMaxAgeRule),

    rule.Header(ruleset.NoCacheRule, ruleset.NoCacheRule),
)

DefaultRuleSet is a list of the default pre-cache validators which exists in ALL handlers, local and remote.

func GetMaxAge Uses

func GetMaxAge(r *http.Request) entry.LifeChanger

GetMaxAge parses the "Cache-Control" header and returns a LifeChanger which can be passed to the response's Reset

func NoCache Uses

func NoCache(w http.ResponseWriter)

NoCache called when a particular handler is not valid for cache. If this function called inside a handler then the handler is not cached even if it's surrounded with the Cache/CacheFunc wrappers.

func ReleaseResponseRecorder Uses

func ReleaseResponseRecorder(res *ResponseRecorder)

ReleaseResponseRecorder releases a ResponseRecorder which has been previously received by AcquireResponseRecorder

type ClientHandler Uses

type ClientHandler struct {
    // contains filtered or unexported fields
}

ClientHandler is the client-side handler for each of the cached route paths's response register one client handler per route.

it's just calls a remote cache service server/handler,

which lives on other, external machine.

func NewClientHandler Uses

func NewClientHandler(bodyHandler http.Handler, life time.Duration, remote string) *ClientHandler

NewClientHandler returns a new remote client handler which asks the remote handler the cached entry's response with a GET request, or add a response with POST request these all are done automatically, users can use this handler as they use the local.go/NewHandler

the ClientHandler is useful when user wants to apply horizontal scaling to the app and has a central http server which handles

func (*ClientHandler) AddRule Uses

func (h *ClientHandler) AddRule(r rule.Rule) *ClientHandler

AddRule adds a rule in the chain, the default rules are executed first.

returns itself.

func (*ClientHandler) Rule Uses

func (h *ClientHandler) Rule(r rule.Rule) *ClientHandler

Rule sets the ruleset for this handler, see internal/net/http/ruleset.go for more information.

returns itself.

func (*ClientHandler) ServeHTTP Uses

func (h *ClientHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP , or remote cache client whatever you like, it's the client-side function of the ServeHTTP sends a request to the server-side remote cache Service and sends the cached response to the frontend client it is used only when you achieved something like horizontal scaling (separate machines) look ../remote/remote.ServeHTTP for more

if cache din't find then it sends a POST request and save the bodyHandler's body to the remote cache.

It takes 3 parameters the first is the remote address (it's the address you started your http server which handled by the Service.ServeHTTP) the second is the handler (or the mux) you want to cache and the third is the, optionally, cache expiration, which is used to set cache duration of this specific cache entry to the remote cache service if <=minimumAllowedCacheDuration then the server will try to parse from "cache-control" header

client-side function

type Handler Uses

type Handler struct {
    // contains filtered or unexported fields
}

Handler the local cache service handler contains the original bodyHandler, the memory cache entry and the validator for each of the incoming requests and post responses

func NewHandler Uses

func NewHandler(bodyHandler http.Handler,
    expireDuration time.Duration) *Handler

NewHandler returns a new cached handler

func (*Handler) AddRule Uses

func (h *Handler) AddRule(r rule.Rule) *Handler

AddRule adds a rule in the chain, the default rules are executed first.

returns itself.

func (*Handler) Rule Uses

func (h *Handler) Rule(r rule.Rule) *Handler

Rule sets the ruleset for this handler.

returns itself.

func (*Handler) ServeHTTP Uses

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type ResponseRecorder Uses

type ResponseRecorder struct {
    // contains filtered or unexported fields
}

ResponseRecorder is used by httpcache to be able to get the Body and the StatusCode of a request handler

func AcquireResponseRecorder Uses

func AcquireResponseRecorder(underline http.ResponseWriter) *ResponseRecorder

AcquireResponseRecorder returns a ResponseRecorder

func (*ResponseRecorder) Body Uses

func (res *ResponseRecorder) Body() []byte

Body joins the chunks to one []byte slice, this is the full body

func (*ResponseRecorder) ContentType Uses

func (res *ResponseRecorder) ContentType() string

ContentType returns the header's value of "Content-Type"

func (*ResponseRecorder) Header Uses

func (res *ResponseRecorder) Header() http.Header

Header returns the header map that will be sent by WriteHeader. Changing the header after a call to WriteHeader (or Write) has no effect unless the modified headers were declared as trailers by setting the "Trailer" header before the call to WriteHeader (see example). To suppress implicit response headers, set their value to nil.

func (*ResponseRecorder) StatusCode Uses

func (res *ResponseRecorder) StatusCode() int

StatusCode returns the status code, if not given then returns 200 but doesn't changes the existing behavior

func (*ResponseRecorder) Write Uses

func (res *ResponseRecorder) Write(contents []byte) (int, error)

Write writes the data to the connection as part of an HTTP reply.

If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.

Depending on the HTTP protocol version and the client, calling Write or WriteHeader may prevent future reads on the Request.Body. For HTTP/1.x requests, handlers should read any needed request body data before writing the response. Once the headers have been flushed (due to either an explicit Flusher.Flush call or writing enough data to trigger a flush), the request body may be unavailable. For HTTP/2 requests, the Go HTTP server permits handlers to continue to read the request body while concurrently writing the response. However, such behavior may not be supported by all HTTP/2 clients. Handlers should read before writing if possible to maximize compatibility.

func (*ResponseRecorder) WriteHeader Uses

func (res *ResponseRecorder) WriteHeader(statusCode int)

WriteHeader sends an HTTP response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.

Directories

PathSynopsis
rule

Package nethttp imports 10 packages (graph) and is imported by 7 packages. Updated 2018-02-16. Refresh now. Tools for package owners.