Documentation
¶
Overview ¶
Package casper implements H2O's 1 CASPer (cache-aware server push) in golang.
It wraps go's standard server push method and maintains a fingerprint of browser caches and decides to push or cancel. The fingerprint is generated by using golomb-coded sets (compressed encoding of Bloom filter).
Below is a simple example of usage.
// Initialize casper with false-positive probability // 1/64 and number of assets 10. pusher := casper.New(1<<6, 10) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Execute cache aware server push. // // In this example, it generates a fingerprint "JA" and set it // as "x-go-casper" cookie value. // // If you access this handler first time, it runs server-push. // But from next time, with same client, it cancels pushing since // cookie indicates asset has already been cached by the client. if _, err := pusher.Push(w, r, []string{"/static/example.js"}, nil); err != nil { log.Printf("[ERROR] Failed to push assets: %s", err) } // ... })
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Casper ¶
type Casper struct {
// contains filtered or unexported fields
}
Casper provides a interface for cache-aware HTTP/2 server push.
func (*Casper) Push ¶
func (c *Casper) Push(w http.ResponseWriter, r *http.Request, targets []string, opts *Options) (*http.Request, error)
Push initiates an HTTP/2 server push using the given targets and options. Internally, it just calls go's standard server push method (which was added from go1.8).
It generates a fingerprint of pushed targets compressed by Golomb-coding1 and sets it as a special cookie value to the given ResponseWriter. We can use this cookie to determine the browser caches the specific targets or not. So from next time when the server receives a request, it checks the cookie and determine to push or not the given targets.