Do not cache incomplete requests

This commit is contained in:
Moritz Marquardt 2024-04-16 22:38:48 +02:00
parent 18d09a163c
commit f6ed4285bc

View File

@ -100,11 +100,15 @@ type writeCacheReader struct {
cache cache.ICache cache cache.ICache
hasError bool hasError bool
doNotCache bool doNotCache bool
complete bool
} }
func (t *writeCacheReader) Read(p []byte) (n int, err error) { func (t *writeCacheReader) Read(p []byte) (n int, err error) {
log.Trace().Msgf("[cache] read %q", t.cacheKey) log.Trace().Msgf("[cache] read %q", t.cacheKey)
n, err = t.originalReader.Read(p) n, err = t.originalReader.Read(p)
if err == io.EOF {
t.complete = true
}
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Trace().Err(err).Msgf("[cache] original reader for %q has returned an error", t.cacheKey) log.Trace().Err(err).Msgf("[cache] original reader for %q has returned an error", t.cacheKey)
t.hasError = true t.hasError = true
@ -120,7 +124,7 @@ func (t *writeCacheReader) Read(p []byte) (n int, err error) {
} }
func (t *writeCacheReader) Close() error { func (t *writeCacheReader) Close() error {
doWrite := !t.hasError && !t.doNotCache doWrite := !t.hasError && !t.doNotCache && t.complete
fc := *t.fileResponse fc := *t.fileResponse
fc.Body = t.buffer.Bytes() fc.Body = t.buffer.Bytes()
if doWrite { if doWrite {