@@ -6,6 +6,7 @@ import { randomUUID } from "node:crypto";
66import { createRedisClient , RedisClient , RedisWithClusterOptions } from "~/redis.server" ;
77import { longPollingFetch } from "~/utils/longPollingFetch" ;
88import { logger } from "./logger.server" ;
9+ import { jumpHash } from "@trigger.dev/core/v3/serverOnly" ;
910
1011export interface CachedLimitProvider {
1112 getCachedLimit : ( organizationId : string , defaultValue : number ) => Promise < number | undefined > ;
@@ -45,7 +46,7 @@ const RESERVED_COLUMNS = ["id", "taskIdentifier", "friendlyId", "status", "creat
4546const RESERVED_SEARCH_PARAMS = [ "createdAt" , "tags" , "skipColumns" ] ;
4647
4748export type RealtimeClientOptions = {
48- electricOrigin : string ;
49+ electricOrigin : string | string [ ] ;
4950 redis : RedisWithClusterOptions ;
5051 cachedLimitProvider : CachedLimitProvider ;
5152 keyPrefix : string ;
@@ -221,15 +222,26 @@ export class RealtimeClient {
221222 whereClause : string ,
222223 clientVersion ?: string
223224 ) {
224- const electricUrl = this . #constructRunsElectricUrl( url , whereClause , clientVersion ) ;
225+ const electricUrl = this . #constructRunsElectricUrl(
226+ url ,
227+ environment ,
228+ whereClause ,
229+ clientVersion
230+ ) ;
225231
226232 return this . #performElectricRequest( electricUrl , environment , undefined , clientVersion ) ;
227233 }
228234
229- #constructRunsElectricUrl( url : URL | string , whereClause : string , clientVersion ?: string ) : URL {
235+ #constructRunsElectricUrl(
236+ url : URL | string ,
237+ environment : RealtimeEnvironment ,
238+ whereClause : string ,
239+ clientVersion ?: string
240+ ) : URL {
230241 const $url = new URL ( url . toString ( ) ) ;
231242
232- const electricUrl = new URL ( `${ this . options . electricOrigin } /v1/shape` ) ;
243+ const electricOrigin = this . #resolveElectricOrigin( environment . id ) ;
244+ const electricUrl = new URL ( `${ electricOrigin } /v1/shape` ) ;
233245
234246 // Copy over all the url search params to the electric url
235247 $url . searchParams . forEach ( ( value , key ) => {
@@ -428,6 +440,16 @@ export class RealtimeClient {
428440 return `${ this . options . keyPrefix } :${ environmentId } ` ;
429441 }
430442
443+ #resolveElectricOrigin( environmentId : string ) {
444+ if ( typeof this . options . electricOrigin === "string" ) {
445+ return this . options . electricOrigin ;
446+ }
447+
448+ const index = jumpHash ( environmentId , this . options . electricOrigin . length ) ;
449+
450+ return this . options . electricOrigin [ index ] ?? this . options . electricOrigin [ 0 ] ;
451+ }
452+
431453 #registerCommands( ) {
432454 this . redis . defineCommand ( "incrementAndCheckConcurrency" , {
433455 numberOfKeys : 1 ,
0 commit comments