Skip to content

Batching

With batch, the HTTP transport sends the calls made together as one JSON-RPC batch: one request and one response for many calls.

import { Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const Http = Transport.http("http://127.0.0.1:8545", { batch: {} }).pipe(
Layer.provide(FetchHttpClient.layer),
);
const [chainId, block, gasPrice] = await Effect.all(
[Eth.chainId(), Eth.blockNumber(), Eth.gasPrice()],
{ concurrency: "unbounded" },
).pipe(Effect.provide(Http), Effect.runPromise);

The request body is one array:

[
{ "jsonrpc": "2.0", "id": 0, "method": "eth_chainId", "params": [] },
{ "jsonrpc": "2.0", "id": 1, "method": "eth_blockNumber", "params": [] },
{ "jsonrpc": "2.0", "id": 2, "method": "eth_gasPrice", "params": [] }
]

Each call still returns its own result, and the program does not change.

By default a batch collects the calls that start in the same tick. Calls that run one after the other never meet.

import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";
// One batch of three: the calls start together.
const together = Effect.all(
[Eth.chainId(), Eth.blockNumber(), Eth.gasPrice()],
{ concurrency: "unbounded" },
);
// Two requests: the second call starts after the first has answered.
const inTurn = Effect.gen(function* () {
const chainId = yield* Eth.chainId();
const block = yield* Eth.blockNumber();
return [chainId, block];
});
How the calls are made Requests
Effect.all([…], { concurrency: "unbounded" }) one batch
Effect.forEach(items, f, { concurrency: "unbounded" }) one batch
Effect.all([…]), which is sequential one per call
yield* after yield* in Effect.gen one per call
two fibers that call in the same tick one batch

With batching on, a lone call is sent as an array of one.

window keeps the batch open for a duration, so calls from different parts of a program can share it.

import { Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
const Http = Transport.http("http://127.0.0.1:8545", {
batch: { window: "10 millis" },
}).pipe(Layer.provide(FetchHttpClient.layer));

Every call then waits up to 10 ms before it leaves. That is the price of the wider batch.

maxSize caps the calls in one batch, 1000 by default. More calls make more batches.

import { Effect, Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Transport } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const Http = Transport.http("http://127.0.0.1:8545", { batch: { maxSize: 100 } }).pipe(
Layer.provide(FetchHttpClient.layer),
);
// 250 calls: three requests, of 100, 100 and 50 calls
const blocks = Effect.forEach(
Array.from({ length: 250 }, (_, index) => BigInt(index)),
(number) => Eth.getBlockByNumber(number, false),
{ concurrency: "unbounded" },
).pipe(Effect.provide(Http));

Hosted endpoints limit the size of a batch. Set maxSize to their limit.

A batch is not all or nothing. Each call succeeds or fails with its own answer.

import { Effect } from "effect";
import { Address } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
const results = Effect.all(
[
Effect.result(Eth.blockNumber()),
Effect.result(Eth.getBalance(Address.zero, 10n ** 12n)), // no such block
],
{ concurrency: "unbounded" },
);
// [Success(…), Failure(RpcError)]: the first call is not affected by the second
The node answers Each call
an array with one entry per call, in any order gets its own result or RpcError, matched by id
an array that lacks the entry of a call that call fails with TransportError and the reason MalformedResponse
an array with an entry that is not JSON-RPC the call that entry names fails with MalformedResponse, the others are answered
one error object instead of an array every call of the batch fails with that RpcError
nothing, or an HTTP error every call of the batch fails with the same TransportError

Effect.result turns a failure into a value, so one failed call does not stop Effect.all.

A call that cannot be written as JSON fails alone too, with the reason Unserializable: the batch leaves without it.

  • Identical calls are not merged. Two Eth.blockNumber() in a batch are two entries with two ids.
  • The transport must be shared. Calls batch inside one transport. A layer provided twice builds two: see Provide it once.
  • HTTP only. A socket transport has no batch option: calls already travel together on its one connection, each with its id.