Skip to content

Commit

Permalink
refactor: disable transport ranking by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Apr 19, 2023
1 parent 8f6317a commit 03816ec
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 68 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-waves-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Disabled `fallback` transport ranking by default.
28 changes: 14 additions & 14 deletions site/docs/clients/transports/fallback.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,25 @@ const client = createPublicClient({

### Transport Ranking

By default, each of the Transports passed to the `fallback` Transport are automatically ranked based on their **latency** & **stability** via a weighted moving score algorithm.
Transport Ranking enables each of the Transports passed to the `fallback` Transport are automatically ranked based on their **latency** & **stability** via a weighted moving score algorithm.

Every 10 seconds (`interval`), the `fallback` Transport will ping each transport in the list. For the past 10 pings (`sampleCount`), they will be ranked based on if they responded (stability) and how fast they responded (latency). The algorithm applies a weight of `0.7` to the stability score, and a weight of `0.3` to the latency score to derive the final score which it is ranked on.

The Transport that has the best latency & stability score over the sample period is prioritized first.

You can modify the default rank config using the `rank` parameter:
You can turn on automated ranking with the `rank` option:

```ts
const client = createPublicClient({
chain: mainnet,
transport: fallback(
[alchemy, infura],
{ rank: true } // [!code focus]
),
})
```

You can also modify the default rank config:

```ts
const client = createPublicClient({
Expand All @@ -71,18 +83,6 @@ const client = createPublicClient({
})
```

Or you can turn it off automated ranking by passing `false` to `rank`:

```ts
const client = createPublicClient({
chain: mainnet,
transport: fallback(
[alchemy, infura],
{ rank: false } // [!code focus]
),
})
```

## Parameters

### rank (optional)
Expand Down
4 changes: 1 addition & 3 deletions src/actions/public/createBlockFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ test('fallback client: scopes request', async () => {
})

const fallbackClient = createPublicClient({
transport: fallback([http(server1.url), http(server2.url)], {
rank: false,
}),
transport: fallback([http(server1.url), http(server2.url)]),
})
const filter = await createBlockFilter(fallbackClient)
expect(filter).toBeDefined()
Expand Down
4 changes: 1 addition & 3 deletions src/actions/public/createContractEventFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ test('fallback client: scopes request', async () => {
})

const fallbackClient = createPublicClient({
transport: fallback([http(server1.url), http(server2.url)], {
rank: false,
}),
transport: fallback([http(server1.url), http(server2.url)]),
})
const filter = await createContractEventFilter(fallbackClient, {
abi: usdcContractConfig.abi,
Expand Down
4 changes: 1 addition & 3 deletions src/actions/public/createEventFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ test('fallback client: scopes request', async () => {
})

const fallbackClient = createPublicClient({
transport: fallback([http(server1.url), http(server2.url)], {
rank: false,
}),
transport: fallback([http(server1.url), http(server2.url)]),
})
const filter = await createEventFilter(fallbackClient)
expect(filter).toBeDefined()
Expand Down
4 changes: 1 addition & 3 deletions src/actions/public/createPendingTransactionFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ test('fallback client: scopes request', async () => {
})

const fallbackClient = createPublicClient({
transport: fallback([http(server1.url), http(server2.url)], {
rank: false,
}),
transport: fallback([http(server1.url), http(server2.url)]),
})
const filter = await createPendingTransactionFilter(fallbackClient)
expect(filter).toBeDefined()
Expand Down
63 changes: 22 additions & 41 deletions src/clients/transports/fallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ describe('request', () => {
res.end(JSON.stringify({ result: '0x1' }))
})

let transport = fallback([http(server1.url), http(server3.url)], {
rank: false,
})({
let transport = fallback([http(server1.url), http(server3.url)])({
chain: localhost,
})
expect(await transport.request({ method: 'eth_blockNumber' })).toBe('0x1')
Expand All @@ -116,12 +114,11 @@ describe('request', () => {
expect(count).toBe(2)

count = 0
transport = fallback(
[http(server1.url), http(server2.url), http(server3.url)],
{
rank: false,
},
)({
transport = fallback([
http(server1.url),
http(server2.url),
http(server3.url),
])({
chain: localhost,
})
expect(await transport.request({ method: 'eth_blockNumber' })).toBe('0x1')
Expand All @@ -130,9 +127,7 @@ describe('request', () => {
expect(count).toBe(3)

count = 0
transport = fallback([http(server1.url), http(server2.url)], {
rank: false,
})({
transport = fallback([http(server1.url), http(server2.url)])({
chain: localhost,
})
await expect(() =>
Expand All @@ -159,12 +154,11 @@ describe('request', () => {
res.end(JSON.stringify({ result: '0x1' }))
})

const transport = fallback(
[http(server1.url), http(server2.url), http(server3.url)],
{
rank: false,
},
)({
const transport = fallback([
http(server1.url),
http(server2.url),
http(server3.url),
])({
chain: localhost,
})

Expand Down Expand Up @@ -234,12 +228,11 @@ describe('request', () => {
res.end(JSON.stringify({ result: '0x1' }))
})

const transport = fallback(
[http(server1.url), http(server2.url), http(server3.url)],
{
rank: false,
},
)({
const transport = fallback([
http(server1.url),
http(server2.url),
http(server3.url),
])({
chain: localhost,
})
await expect(() =>
Expand All @@ -266,9 +259,7 @@ describe('request', () => {
res.end(JSON.stringify({ result: '0x1' }))
})

const transport = fallback([http(server1.url), http(server2.url)], {
rank: false,
})({
const transport = fallback([http(server1.url), http(server2.url)])({
chain: localhost,
})
expect(
Expand All @@ -291,9 +282,7 @@ describe('request', () => {
res.end()
})

const transport = fallback([http(server1.url), http(server2.url)], {
rank: false,
})({
const transport = fallback([http(server1.url), http(server2.url)])({
chain: localhost,
})
await expect(() =>
Expand Down Expand Up @@ -321,9 +310,7 @@ describe('request', () => {
res.end(JSON.stringify({ error: { code: -32603, message: 'sad times' } }))
})

const transport = fallback([http(server1.url), http(server2.url)], {
rank: false,
})({
const transport = fallback([http(server1.url), http(server2.url)])({
chain: localhost,
})
await expect(() =>
Expand All @@ -348,7 +335,6 @@ describe('request', () => {

const transport = fallback([http(server1.url), http(server2.url)], {
retryCount: 1,
rank: false,
})({
chain: localhost,
})
Expand Down Expand Up @@ -380,7 +366,6 @@ describe('request', () => {
http(server2.url, { retryCount: 2 }),
],
{
rank: false,
retryCount: 0,
},
)({
Expand Down Expand Up @@ -534,9 +519,7 @@ describe('client', () => {
res.end(JSON.stringify({ result: '0x1' }))
})

const transport = fallback([http(server1.url), http(server2.url)], {
rank: false,
})
const transport = fallback([http(server1.url), http(server2.url)])
const client = createPublicClient({ chain: localhost, transport })

expect(await getBlockNumber(client)).toBe(1n)
Expand All @@ -560,9 +543,7 @@ describe('client', () => {
res.end(JSON.stringify({ error: { code: -32603, message: 'sad times' } }))
})

const transport = fallback([http(server1.url), http(server2.url)], {
rank: false,
})
const transport = fallback([http(server1.url), http(server2.url)])
const client = createPublicClient({ chain: localhost, transport })

await expect(
Expand Down
2 changes: 1 addition & 1 deletion src/clients/transports/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function fallback(
const {
key = 'fallback',
name = 'Fallback',
rank = true,
rank = false,
retryCount,
retryDelay,
} = config
Expand Down

2 comments on commit 03816ec

@vercel
Copy link

@vercel vercel bot commented on 03816ec Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viem-playground – ./playgrounds/browser

viem-playground.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 03816ec Apr 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.