Skip to content

Commit ebe1351

Browse files
Switch default
1 parent 51e9a7d commit ebe1351

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

cache/core/core.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -259,30 +259,26 @@ type LookupOptions struct {
259259
// value of 0 means to read to the end of the object.
260260
To uint64
261261

262-
// AlwaysUseRequestedRange forces the provided range to be used during streaming.
262+
// LegacyReturnWholeBody restores a legacy behavior around range requests.
263263
//
264-
// If false, under certain circumstances the entire body will be returned
265-
// instead of just the requested range.
266-
//
267-
// If:
268-
//
269-
// - AlwaysUseRequestedRange is false
264+
// In SDK v1.4.2 and earlier, under certain circumstances the requested range
265+
// (From/To) would be ignored. Specifically, if:
266+
// - The lookup (reader) is concurrent with the writer of the body
270267
// - The size of the cached item's body was not provided by the writer
271268
// - The reader requests a specific range of the cached item's body
272269
// (`From` and `To` are provided in LookupOptions or GetBodyOptions)
273-
// - The writer and reader are concurrent, i.e. the body is streamed from
274-
// the writer to the reader
275-
//
276-
// then the core cache API will ignore the requested range, and provide
277-
// the entire body.
270+
// then the core cache API would ignore the requested range and provide
271+
// the entire body, instead of providing just the requested range.
278272
//
279-
// Setting AlwaysUseRequestedRange to true changes this behavior:
280-
// - The reader will block until the start of the requested range has been
281-
// written by the writer. This blocking happens within the
282-
// TransactionLookup or Lookup call (if the range is provided in
283-
// LookupOptions) or in GetBody (if the range is provided to GetBody).
273+
// In SDK v1.4.3, the default behavior changed. In a concurrent read/write:
274+
// - If From and To are nonzero, the reader will block until the start of
275+
// the requested range has been provided by the writer.
284276
// - Only the requested range will be returned to the reader.
285-
AlwaysUseRequestedRange bool
277+
//
278+
// Note that the full body is still provided if the range is invalid.
279+
//
280+
// LegacyReturnWholeBody restores the v1.4.2 behavior.
281+
LegacyReturnWholeBody bool
286282
}
287283

288284
func abiLookupOptions(opts LookupOptions) (fastly.CacheLookupOptions, error) {
@@ -297,7 +293,7 @@ func abiLookupOptions(opts LookupOptions) (fastly.CacheLookupOptions, error) {
297293
abiOpts.SetRequest(req)
298294
}
299295

300-
abiOpts.SetAlwaysUseRequestedRange(opts.AlwaysUseRequestedRange)
296+
abiOpts.SetAlwaysUseRequestedRange(!opts.LegacyReturnWholeBody)
301297

302298
return abiOpts, nil
303299
}

cache/core/core_test.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func ExampleFound_GetRange() {
209209
fmt.Printf("The cached value was: %s", cachedStr)
210210
}
211211

212-
func ExampleLookupOptions_AlwaysUseRequestedRange() {
212+
func ExampleLookupOptions_LegacyReturnWholeBody() {
213213
const (
214214
key = "my_key"
215215
contents = "my cached object"
@@ -227,25 +227,45 @@ func ExampleLookupOptions_AlwaysUseRequestedRange() {
227227
}
228228

229229
// With the write still outstanding, start a lookup for a specific range.
230-
f, err := core.Lookup([]byte("my_key"), core.LookupOptions{
231-
From: 3,
232-
To: 8,
233-
AlwaysUseRequestedRange: true,
230+
legacy, err := core.Lookup([]byte(key), core.LookupOptions{
231+
From: 3,
232+
To: 8,
233+
LegacyReturnWholeBody: true,
234234
})
235235
if err != nil {
236236
panic(err)
237237
}
238+
// With the write still outstanding, start a lookup for a specific range.
239+
updated, err := core.Lookup([]byte(key), core.LookupOptions{
240+
From: 3,
241+
To: 8,
242+
})
243+
if err != nil {
244+
panic(err)
245+
}
246+
247+
// The read and write are concurrent, and with an unknown length.
248+
// In the legacy mode, we'll see the whole body;
249+
// in the updated mode, we'll see just the range.
238250

239-
// Write and flush:
251+
// Finish writing the body:
240252
if _, err := io.WriteString(w, contents); err != nil {
241253
panic(err)
242254
}
243255

244-
cachedStr, err := io.ReadAll(f.Body)
256+
legacyStr, err := io.ReadAll(legacy.Body)
257+
if err != nil {
258+
panic(err)
259+
}
260+
updatedStr, err := io.ReadAll(updated.Body)
245261
if err != nil {
246262
panic(err)
247263
}
248-
if string(cachedStr) != "cached" {
249-
panic(fmt.Sprintf("got: %q, want: %q", cachedStr, "cached"))
264+
265+
if got, want := string(legacyStr), "my cached object"; got != want {
266+
panic(fmt.Sprintf("got: %q, want: %q", got, want))
267+
}
268+
if got, want := string(updatedStr), "cached"; got != want {
269+
panic(fmt.Sprintf("got: %q, want: %q", got, want))
250270
}
251271
}

0 commit comments

Comments
 (0)