diff --git a/src/middleware/withLocator.js b/src/middleware/withLocator.js index 867cdfa..c632d82 100644 --- a/src/middleware/withLocator.js +++ b/src/middleware/withLocator.js @@ -21,9 +21,7 @@ import { Client } from '@storacha/indexing-service-client' */ export function withLocator (handler) { return async (request, env, ctx) => { - const useIndexingService = new URL(request.url).searchParams - .getAll('ff') - .includes('indexing-service') + const useIndexingService = isIndexingServiceEnabled(request, env) const locator = useIndexingService ? new IndexingServiceLocator({ @@ -46,3 +44,22 @@ export function withLocator (handler) { return handler(request, env, { ...ctx, locator }) } } + +/** + * Determines if the indexing service is enabled. It is enabled if the request + * contains the `ff=indexing-service` query parameter or if a random chance + * falls within the ramp-up probability. If `FF_RAMP_UP_PROBABILITY` is not set, + * it defaults to 0%. + * + * @param {Request} request + * @param {LocatorEnvironment} env + * @returns {boolean} + */ +function isIndexingServiceEnabled (request, env) { + const withIndexingServicesArg = new URL(request.url).searchParams + .getAll('ff') + .includes('indexing-service') + const probability = env.FF_RAMP_UP_PROBABILITY ? Number(env.FF_RAMP_UP_PROBABILITY) : 0 + const withIndexerEnabled = Math.random() * 100 <= probability + return withIndexingServicesArg || withIndexerEnabled +} diff --git a/src/middleware/withLocator.types.ts b/src/middleware/withLocator.types.ts index 2b24797..0a88253 100644 --- a/src/middleware/withLocator.types.ts +++ b/src/middleware/withLocator.types.ts @@ -10,6 +10,7 @@ export interface LocatorEnvironment extends MiddlewareEnvironment { CARPARK: R2Bucket CARPARK_PUBLIC_BUCKET_URL?: string INDEXING_SERVICE_URL?: string + FF_RAMP_UP_PROBABILITY?: string } export interface LocatorContext extends MiddlewareContext { diff --git a/wrangler.toml b/wrangler.toml index 87e5c02..99080d6 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -53,6 +53,7 @@ MAX_SHARDS = "825" FF_RATE_LIMITER_ENABLED = "false" FF_EGRESS_TRACKER_ENABLED = "false" FF_TELEMETRY_ENABLED = "true" +FF_RAMP_UP_PROBABILITY = "10" GATEWAY_SERVICE_DID = "did:web:w3s.link" UPLOAD_SERVICE_DID = "did:web:web3.storage" CONTENT_CLAIMS_SERVICE_URL = "https://claims.web3.storage" @@ -80,6 +81,7 @@ FF_RATE_LIMITER_ENABLED = "false" FF_EGRESS_TRACKER_ENABLED = "true" FF_TELEMETRY_ENABLED = "true" FF_DELEGATIONS_STORAGE_ENABLED = "true" +FF_RAMP_UP_PROBABILITY = "100" GATEWAY_SERVICE_DID = "did:web:staging.w3s.link" UPLOAD_SERVICE_DID = "did:web:staging.web3.storage" CONTENT_CLAIMS_SERVICE_URL = "https://staging.claims.web3.storage" @@ -100,6 +102,7 @@ DEBUG = "true" FF_RATE_LIMITER_ENABLED = "false" FF_EGRESS_TRACKER_ENABLED = "false" FF_TELEMETRY_ENABLED = "true" +FF_RAMP_UP_PROBABILITY = "100" MAX_SHARDS = "120" GATEWAY_SERVICE_DID = "did:web:staging.w3s.link" UPLOAD_SERVICE_DID = "did:web:staging.web3.storage" @@ -145,6 +148,7 @@ FF_RATE_LIMITER_ENABLED = "false" FF_EGRESS_TRACKER_ENABLED = "true" FF_TELEMETRY_ENABLED = "true" FF_DELEGATIONS_STORAGE_ENABLED = "true" +FF_RAMP_UP_PROBABILITY = "100" GATEWAY_SERVICE_DID = "did:web:staging.w3s.link" UPLOAD_SERVICE_DID = "did:web:staging.web3.storage" CONTENT_CLAIMS_SERVICE_URL = "https://staging.claims.web3.storage"