Skip to content

Commit

Permalink
resolve hanging when file cache is enabled. (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
sillygod authored Jun 19, 2020
1 parent 313959e commit c0ea663
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ func (e *Entry) writePublicResponse(w http.ResponseWriter) error {
}

defer reader.Close()
w.WriteHeader(e.Response.Code)

// In io.copy will write the status code.
// https://golang.org/pkg/net/http/#ResponseWriter
_, err = io.Copy(w, reader)
return err
}
Expand Down
2 changes: 1 addition & 1 deletion caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
defaultStatusHeader = "X-Cache-Status"
defaultLockTimeout = time.Duration(5) * time.Minute
defaultMaxAge = time.Duration(5) * time.Minute
defaultPath = ""
defaultPath = "/tmp/caddy_cache"
defaultCacheType = file
defaultcacheBucketsNum = 256
defaultCacheMaxMemorySize = GB // default is 1 GB
Expand Down
3 changes: 2 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
return caddyhttp.Error(entry.Response.Code, err)
}

return nil
}

err = h.respond(w, entry, cacheSkip)
Expand All @@ -328,7 +329,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyht
return caddyhttp.Error(entry.Response.Code, err)
}

return err
return nil
}

var (
Expand Down
19 changes: 9 additions & 10 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Response struct {
wroteHeader bool
bodyComplete bool

bodyChan chan struct{}
bodyChan chan struct{} // indicates whether the backend is set or not.
bodyCompleteChan chan struct{}
closedChan chan struct{}
headersChan chan struct{}
Expand Down Expand Up @@ -85,7 +85,6 @@ func (r *Response) writeHeader(b []byte, str string) {
// Write writes the upstream's content in the backend's storage
func (r *Response) Write(buf []byte) (int, error) {

// debug.PrintStack()
if !r.wroteHeader {
r.writeHeader(buf, "")
}
Expand Down Expand Up @@ -202,16 +201,16 @@ func (r *Response) Flush() {
// Close indicate the data is completely written to the body
// so that we can close it.
func (r *Response) Close() error {
if r.body == nil {
<-r.bodyChan
}

defer func() {
r.bodyComplete = true
r.bodyCompleteChan <- struct{}{}
r.closedChan <- struct{}{}
}()
r.body.Close()

r.bodyComplete = true
r.bodyCompleteChan <- struct{}{}
r.closedChan <- struct{}{}

if r.body != nil {
return r.body.Close()
}
return nil
}

Expand Down

0 comments on commit c0ea663

Please sign in to comment.