bxcodec / httpcache

Get a working HTTP Cache in Go (Golang) with only 3 lines of code!!!!
MIT License
58 stars 15 forks source link

Does not take care of the max-age when setting rfcCompliance to true #27

Open kerherve opened 3 years ago

kerherve commented 3 years ago

Hello,

I am testing httpcache with my http client and I encountered an unexpected result.

I send a request to a server that responds me the following response (see the header "Cache-Control: no-transform, max-age=90") :

Hypertext Transfer Protocol
    HTTP/1.1 200 OK\r\n
    Server: openresty/1.15.8.2\r\n
    Date: Thu, 17 Jun 2021 09:19:58 GMT\r\n
    Content-Type: application/json\r\n
    Transfer-Encoding: chunked\r\n
    Connection: keep-alive\r\n
    Vary: Accept-Encoding\r\n
    Cache-Control: no-transform, max-age=90\r\n
    ETag: W/"79736d2d"\r\n
    Content-Encoding: gzip\r\n
    \r\n
...

my request is the following:

Hypertext Transfer Protocol
    GET ... HTTP/1.1\r\n // I change the request by ...
    Host: myserver:27220\r\n
    User-Agent: OpenAPI-Generator/1.0.0/go\r\n
    Accept: application/json\r\n
    Content-Type: application/json\r\n
    Accept-Encoding: gzip\r\n
    \r\n

When rfcCompliance is set to true, max-age is not respected

_, err := httpcache.NewWithInmemoryCache(innerHTTPClient, true, time.Second*120)

see the result:

2021/06/17 11:47:45 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 107426ms
Response time: 87ms
Response time: 67ms
Response time: 49ms
Response time: 63ms
Response time: 50ms
Response time: 54ms
Response time: 55ms
Response time: 52ms
Response time: 55ms
WARNING [discover.go:42] Sleep for 100s                               
Response time: 507ms
Response time: 58ms
Response time: 46ms
Response time: 33ms
Response time: 40ms
Response time: 29ms
Response time: 33ms
Response time: 68ms
Response time: 295ms
Response time: 150ms
WARNING [discover.go:56] Sleep for 130s                               
2021/06/17 11:51:35 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 121455ms
Response time: 304ms
Response time: 190ms
Response time: 279ms
Response time: 196ms
Response time: 140ms
Response time: 123ms
Response time: 107ms
Response time: 727ms
Response time: 304ms

But when rfcCompliance is set to false, max-age is respected

_, err := httpcache.NewWithInmemoryCache(innerHTTPClient, true, time.Second*120)

see the result:

2021/06/17 12:34:06 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 120114ms
Response time: 150ms
Response time: 171ms
Response time: 112ms
Response time: 71ms
Response time: 56ms
Response time: 55ms
Response time: 47ms
Response time: 42ms
Response time: 55ms
WARNING [discover.go:56] Sleep for 80s                                
Response time: 148ms
Response time: 202ms
Response time: 59ms
Response time: 45ms
Response time: 35ms
Response time: 49ms
Response time: 33ms
Response time: 36ms
Response time: 46ms
Response time: 32ms
WARNING [discover.go:70] Sleep for 100s                               
2021/06/17 12:37:36 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 111084ms
Response time: 97ms
Response time: 61ms
Response time: 43ms
Response time: 50ms
Response time: 58ms
Response time: 46ms
Response time: 65ms
Response time: 44ms
Response time: 191ms
WARNING [discover.go:84] Sleep for 130s                               
2021/06/17 12:39:46 Cache item's missing failed to retrieve from cache, trying with a live version
Response time: 110458ms
Response time: 88ms
Response time: 56ms
Response time: 54ms
Response time: 39ms
Response time: 54ms
Response time: 60ms
Response time: 1108ms
Response time: 103ms
Response time: 72ms

In RFC 7234, it is stated "A cache MUST NOT store a response to any request, unless the response contains a max-age response directive". So far, I understand, that in the response from the server, there is a directive max-age, then setting rfcCompliance to true should work.

Could you correct me if I am wrong, or missing something

Many thanks in advance