Skip to content

Commit

Permalink
chore: improve implementation httpcache
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Oct 10, 2022
1 parent d0159e9 commit 27bed18
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 134 deletions.
16 changes: 7 additions & 9 deletions pkg/httpcache/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
Expand Down Expand Up @@ -265,7 +264,7 @@ func (h *Handler) passUpstream(w http.ResponseWriter, r *cacheRequest) {
rw.Header().Set(CacheHeader, "SKIP")
return
}
b, err := ioutil.ReadAll(rdr)
b, err := io.ReadAll(rdr)
rdr.Close()
if err != nil {
debugf("error reading stream: %v", err)
Expand Down Expand Up @@ -300,6 +299,9 @@ func correctedAge(h http.Header, reqTime, respTime time.Time) (time.Duration, er

respDelay := respTime.Sub(reqTime)
ageSeconds, err := intHeader("Age", h)
if err != nil {
return time.Duration(0), err
}
age := time.Second * time.Duration(ageSeconds)
correctedAge := age + respDelay

Expand Down Expand Up @@ -480,7 +482,7 @@ func newCacheRequest(r *http.Request) (*cacheRequest, error) {
}

if r.Proto == "HTTP/1.1" && r.Host == "" {
return nil, errors.New("Host header can't be empty")
return nil, errors.New("host header can't be empty")
}

return &cacheRequest{
Expand All @@ -492,11 +494,7 @@ func newCacheRequest(r *http.Request) (*cacheRequest, error) {
}

func (r *cacheRequest) isStateChanging() bool {
if !(r.Method == "POST" || r.Method == "PUT" || r.Method == "DELETE") {
return true
}

return false
return !(r.Method == "POST" || r.Method == "PUT" || r.Method == "DELETE")
}

func (r *cacheRequest) isCacheable() bool {
Expand Down Expand Up @@ -565,7 +563,7 @@ func (rw *responseStreamer) Close() error {
func (rw *responseStreamer) Resource() *Resource {
r, err := rw.Stream.NextReader()
if err == nil {
b, err := ioutil.ReadAll(r)
b, err := io.ReadAll(r)
r.Close()
if err == nil {
return NewResourceBytes(rw.StatusCode, b, rw.Header())
Expand Down
2 changes: 1 addition & 1 deletion pkg/httpcache/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *Resource) Age() (time.Duration, error) {
return Clock().Sub(date) + age, nil
}

return time.Duration(0), errors.New("Unable to calculate age")
return time.Duration(0), errors.New("unable to calculate age")
}

func (r *Resource) MaxAge(shared bool) (time.Duration, error) {
Expand Down
Loading

0 comments on commit 27bed18

Please sign in to comment.