Skip to content

Option to respect range during cache streaming #1184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions runtime/fastly/builtins/cache-core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ JS::Result<host_api::CacheLookupOptions> parseLookupOptions(JSContext *cx,
return JS::Result<host_api::CacheLookupOptions>(JS::Error());
}
JS::RootedObject options_obj(cx, &options_val.toObject());

JS::RootedValue headers_val(cx);
if (!JS_GetProperty(cx, options_obj, "headers", &headers_val)) {
return JS::Result<host_api::CacheLookupOptions>(JS::Error());
Expand Down Expand Up @@ -79,6 +80,20 @@ JS::Result<host_api::CacheLookupOptions> parseLookupOptions(JSContext *cx,
}
options.request_headers = host_api::HttpReq(request_handle);
}

JS::RootedValue always_use_requested_range_val(cx);
if (!JS_GetProperty(cx, options_obj, "always_use_requested_range",
&always_use_requested_range_val)) {
return JS::Result<host_api::CacheLookupOptions>(JS::Error());
}
// always_use_requested_range property is optional
if (!always_use_requested_range_val.isUndefined()) {
if (!always_use_requested_range_val.isBoolean()) {
JS_ReportErrorASCII(cx, "always_use_requested_range must be a boolean");
return JS::Result<host_api::CacheLookupOptions>(JS::Error());
}
options.always_use_requested_range = always_use_requested_range_val.toBoolean();
}
}
return options;
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/fastly/host-api/fastly.h
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,9 @@ int purge_surrogate_key(char *surrogate_key, size_t surrogate_key_len, uint32_t

#define FASTLY_CACHE_LOOKUP_OPTIONS_MASK_RESERVED (1 << 0)
#define FASTLY_CACHE_LOOKUP_OPTIONS_MASK_REQUEST_HEADERS (1 << 1)
// Note: SERVICE_ID for internal use only.
#define FASTLY_CACHE_LOOKUP_OPTIONS_MASK_SERVICE_ID (1 << 2)
#define FASTLY_CACHE_LOOKUP_OPTIONS_MASK_ALWAYS_USE_REQUESTED_RANGE (1 << 3)

// Extensible options for cache lookup operations currently used for both `lookup` and
// `transaction_lookup`.
Expand Down
6 changes: 6 additions & 0 deletions runtime/fastly/host-api/host_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,9 @@ Result<CacheHandle> CacheHandle::lookup(std::string_view key, const CacheLookupO
os.request_headers = opts.request_headers.handle;
options_mask |= FASTLY_CACHE_LOOKUP_OPTIONS_MASK_REQUEST_HEADERS;
}
if (opts.always_use_requested_range) {
options_mask |= FASTLY_CACHE_LOOKUP_OPTIONS_MASK_ALWAYS_USE_REQUESTED_RANGE;
}

if (!convert_result(fastly::cache_lookup(reinterpret_cast<char *>(key_str.ptr), key_str.len,
options_mask, &os, &handle),
Expand Down Expand Up @@ -2979,6 +2982,9 @@ Result<CacheHandle> CacheHandle::transaction_lookup(std::string_view key,
os.request_headers = opts.request_headers.handle;
options_mask |= FASTLY_CACHE_LOOKUP_OPTIONS_MASK_REQUEST_HEADERS;
}
if (opts.always_use_requested_range) {
options_mask |= FASTLY_CACHE_LOOKUP_OPTIONS_MASK_ALWAYS_USE_REQUESTED_RANGE;
}

if (!convert_result(fastly::cache_transaction_lookup(reinterpret_cast<char *>(key_str.ptr),
key_str.len, options_mask, &os, &handle),
Expand Down
10 changes: 10 additions & 0 deletions runtime/fastly/host-api/host_api_fastly.h
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,16 @@ class SecretStore final {
struct CacheLookupOptions final {
/// A full request handle, used only for its headers.
HttpReq request_headers;

/// When a range is provided to get_body,
/// respect the requested range even when the body length is unknown
/// (e.g. streaming). If "false", when the body length is unknown,
/// the entire body will be returned instead of just the requested range.
///
/// The default is "false" to preserve the legacy behavior.
/// However, if using ranged get_body, Fastly recommends setting this to "true".
/// The default may change in a future release.
bool always_use_requested_range;
};

struct CacheGetBodyOptions final {
Expand Down
Loading