|
| 1 | +/** |
| 2 | + * This package allows developers to create custom RPC transports. Using these primitives, |
| 3 | + * developers can create custom transports that perform transforms on messages sent and received, |
| 4 | + * attempt retries, and implement routing strategies between multiple transports. |
| 5 | + * |
| 6 | + * ## Augmenting Transports |
| 7 | + * |
| 8 | + * Using this core transport, you can implement specialized functionality for leveraging multiple |
| 9 | + * transports, attempting/handling retries, and more. |
| 10 | + * |
| 11 | + * ### Round Robin |
| 12 | + * |
| 13 | + * Here’s an example of how someone might implement a “round robin” approach to distribute requests |
| 14 | + * to multiple transports: |
| 15 | + * |
| 16 | + * ```ts |
| 17 | + * import { RpcTransport } from '@solana/rpc-spec'; |
| 18 | + * import { RpcResponse } from '@solana/rpc-spec-types'; |
| 19 | + * import { createHttpTransport } from '@solana/rpc-transport-http'; |
| 20 | + * |
| 21 | + * // Create a transport for each RPC server |
| 22 | + * const transports = [ |
| 23 | + * createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' }), |
| 24 | + * createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }), |
| 25 | + * createHttpTransport({ url: 'https://mainnet-beta.my-server-3.com' }), |
| 26 | + * ]; |
| 27 | + * |
| 28 | + * // Create a wrapper transport that distributes requests to them |
| 29 | + * let nextTransport = 0; |
| 30 | + * async function roundRobinTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<RpcResponse<TResponse>> { |
| 31 | + * const transport = transports[nextTransport]; |
| 32 | + * nextTransport = (nextTransport + 1) % transports.length; |
| 33 | + * return await transport(...args); |
| 34 | + * } |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * ### Sharding |
| 38 | + * |
| 39 | + * Another example of a possible customization for a transport is to shard requests |
| 40 | + * deterministically among a set of servers. Here’s an example: |
| 41 | + * |
| 42 | + * Perhaps your application needs to make a large number of requests, or needs to fan request for |
| 43 | + * different methods out to different servers. Here’s an example of an implementation that does the |
| 44 | + * latter: |
| 45 | + * |
| 46 | + * ```ts |
| 47 | + * import { RpcTransport } from '@solana/rpc-spec'; |
| 48 | + * import { RpcResponse } from '@solana/rpc-spec-types'; |
| 49 | + * import { createHttpTransport } from '@solana/rpc-transport-http'; |
| 50 | + * |
| 51 | + * // Create multiple transports |
| 52 | + * const transportA = createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' }); |
| 53 | + * const transportB = createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }); |
| 54 | + * const transportC = createHttpTransport({ url: 'https://mainnet-beta.my-server-3.com' }); |
| 55 | + * const transportD = createHttpTransport({ url: 'https://mainnet-beta.my-server-4.com' }); |
| 56 | + * |
| 57 | + * // Function to determine which shard to use based on the request method |
| 58 | + * function selectShard(method: string): RpcTransport { |
| 59 | + * switch (method) { |
| 60 | + * case 'getAccountInfo': |
| 61 | + * case 'getBalance': |
| 62 | + * return transportA; |
| 63 | + * case 'getLatestBlockhash': |
| 64 | + * case 'getTransaction': |
| 65 | + * return transportB; |
| 66 | + * case 'sendTransaction': |
| 67 | + * return transportC; |
| 68 | + * default: |
| 69 | + * return transportD; |
| 70 | + * } |
| 71 | + * } |
| 72 | + * |
| 73 | + * async function shardingTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<RpcResponse<TResponse>> { |
| 74 | + * const payload = args[0].payload as { method: string }; |
| 75 | + * const selectedTransport = selectShard(payload.method); |
| 76 | + * return await selectedTransport(...args); |
| 77 | + * } |
| 78 | + * ``` |
| 79 | + * |
| 80 | + * ### Retry Logic |
| 81 | + * |
| 82 | + * The transport library can also be used to implement custom retry logic on any request: |
| 83 | + * |
| 84 | + * ```ts |
| 85 | + * import { RpcTransport } from '@solana/rpc-spec'; |
| 86 | + * import { RpcResponse } from '@solana/rpc-spec-types'; |
| 87 | + * import { createHttpTransport } from '@solana/rpc-transport-http'; |
| 88 | + * |
| 89 | + * // Set the maximum number of attempts to retry a request |
| 90 | + * const MAX_ATTEMPTS = 4; |
| 91 | + * |
| 92 | + * // Create the default transport |
| 93 | + * const defaultTransport = createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' }); |
| 94 | + * |
| 95 | + * // Sleep function to wait for a given number of milliseconds |
| 96 | + * function sleep(ms: number): Promise<void> { |
| 97 | + * return new Promise(resolve => setTimeout(resolve, ms)); |
| 98 | + * } |
| 99 | + * |
| 100 | + * // Calculate the delay for a given attempt |
| 101 | + * function calculateRetryDelay(attempt: number): number { |
| 102 | + * // Exponential backoff with a maximum of 1.5 seconds |
| 103 | + * return Math.min(100 * Math.pow(2, attempt), 1500); |
| 104 | + * } |
| 105 | + * |
| 106 | + * // A retrying transport that will retry up to `MAX_ATTEMPTS` times before failing |
| 107 | + * async function retryingTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<RpcResponse<TResponse>> { |
| 108 | + * let requestError; |
| 109 | + * for (let attempts = 0; attempts < MAX_ATTEMPTS; attempts++) { |
| 110 | + * try { |
| 111 | + * return await defaultTransport(...args); |
| 112 | + * } catch (err) { |
| 113 | + * requestError = err; |
| 114 | + * // Only sleep if we have more attempts remaining |
| 115 | + * if (attempts < MAX_ATTEMPTS - 1) { |
| 116 | + * const retryDelay = calculateRetryDelay(attempts); |
| 117 | + * await sleep(retryDelay); |
| 118 | + * } |
| 119 | + * } |
| 120 | + * } |
| 121 | + * throw requestError; |
| 122 | + * } |
| 123 | + * ``` |
| 124 | + * |
| 125 | + * ### Failover |
| 126 | + * |
| 127 | + * Here’s an example of some failover logic integrated into a transport: |
| 128 | + * |
| 129 | + * ```ts |
| 130 | + * import { RpcTransport } from '@solana/rpc-spec'; |
| 131 | + * import { RpcResponse } from '@solana/rpc-spec-types'; |
| 132 | + * import { createHttpTransport } from '@solana/rpc-transport-http'; |
| 133 | + * |
| 134 | + * // Create a transport for each RPC server |
| 135 | + * const transports = [ |
| 136 | + * createHttpTransport({ url: 'https://mainnet-beta.my-server-1.com' }), |
| 137 | + * createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }), |
| 138 | + * createHttpTransport({ url: 'https://mainnet-beta.my-server-2.com' }), |
| 139 | + * ]; |
| 140 | + * |
| 141 | + * // A failover transport that will try each transport in order until one succeeds before failing |
| 142 | + * async function failoverTransport<TResponse>(...args: Parameters<RpcTransport>): Promise<RpcResponse<TResponse>> { |
| 143 | + * let requestError; |
| 144 | + * |
| 145 | + * for (const transport of transports) { |
| 146 | + * try { |
| 147 | + * return await transport(...args); |
| 148 | + * } catch (err) { |
| 149 | + * requestError = err; |
| 150 | + * console.error(err); |
| 151 | + * } |
| 152 | + * } |
| 153 | + * throw requestError; |
| 154 | + * } |
| 155 | + * ``` |
| 156 | + * |
| 157 | + * @packageDocumentation |
| 158 | + */ |
1 | 159 | export * from './http-transport';
|
2 | 160 | export * from './http-transport-for-solana-rpc';
|
0 commit comments