@@ -159,24 +159,15 @@ const (
159159 cacheStorageActionInvalid = 0xffff
160160)
161161
162- func httpCacheWait (c * fastly.HTTPCacheHandle ) error {
163- _ , err := fastly .HTTPCacheGetState (c )
164- if err != nil {
165- return fmt .Errorf ("get state: %w" , err )
166- }
167- return nil
168- }
169-
170- func httpCacheMustInsertOrUpdate (c * fastly.HTTPCacheHandle ) (bool , error ) {
162+ func httpCacheWait (c * fastly.HTTPCacheHandle ) (fastly.CacheLookupState , error ) {
171163 state , err := fastly .HTTPCacheGetState (c )
172164 if err != nil {
173- return false , fmt .Errorf ("get state: %w" , err )
174-
165+ return 0 , fmt .Errorf ("get state: %w" , err )
175166 }
176- return state & fastly . CacheLookupStateMustInsertOrUpdate == fastly . CacheLookupStateMustInsertOrUpdate , nil
167+ return state , nil
177168}
178169
179- func httpCacheGetFoundResponse (c * fastly.HTTPCacheHandle , req * Request , backend string , transformForClient bool ) (* Response , error ) {
170+ func httpCacheGetFoundResponse (c * fastly.HTTPCacheHandle , req * Request , backend string , transformForClient bool , wasHit bool ) (* Response , error ) {
180171 abiResp , abiBody , err := fastly .HTTPCacheGetFoundResponse (c , transformForClient )
181172 if err != nil {
182173 if status , ok := fastly .IsFastlyError (err ); ok && status == fastly .FastlyStatusNone {
@@ -185,9 +176,13 @@ func httpCacheGetFoundResponse(c *fastly.HTTPCacheHandle, req *Request, backend
185176 return nil , fmt .Errorf ("get found response: %w" , err )
186177 }
187178
188- hits , err := fastly .HTTPCacheGetHits (c )
189- if err != nil {
190- return nil , fmt .Errorf ("get hits: %w" , err )
179+ var hits uint64
180+ if wasHit {
181+ h , err := fastly .HTTPCacheGetHits (c )
182+ if err != nil {
183+ return nil , fmt .Errorf ("get hits: %w" , err )
184+ }
185+ hits = uint64 (h )
191186 }
192187
193188 var opts cacheWriteOptions
@@ -204,7 +199,7 @@ func httpCacheGetFoundResponse(c *fastly.HTTPCacheHandle, req *Request, backend
204199 resp .cacheResponse = cacheResponse {
205200 cacheWriteOptions : opts ,
206201 storageAction : cacheStorageActionInvalid ,
207- hits : uint64 ( hits ) ,
202+ hits : hits ,
208203 }
209204 return resp , nil
210205}
@@ -710,7 +705,7 @@ func (candidateResponse *CandidateResponse) applyAndStreamBack(req *Request) (*R
710705 }
711706 body .Close ()
712707
713- resp , err = httpCacheGetFoundResponse (readback , req , "" , false )
708+ resp , err = httpCacheGetFoundResponse (readback , req , "" , false , true )
714709 if err != nil {
715710 return nil , fmt .Errorf ("cache get found response: %w" , err )
716711 }
@@ -722,7 +717,7 @@ func (candidateResponse *CandidateResponse) applyAndStreamBack(req *Request) (*R
722717 }
723718 defer fastly .HTTPCacheTransactionClose (newch )
724719
725- resp , err = httpCacheGetFoundResponse (newch , req , "" , true )
720+ resp , err = httpCacheGetFoundResponse (newch , req , "" , true , true )
726721 if err != nil {
727722 return nil , fmt .Errorf ("cache get found response: %w" , err )
728723 }
0 commit comments