如果在 Node.jsgRPC 使用Solana gRPC 处理海量数据,可能会导致单个 CPU 不堪重负。如果您正在构建需要处理繁忙程序的所有交易或同时跟踪多个程序的系统,请考虑以下高性能替代方案:
-
使用多线程。Node.js 有两个模块:
集群(多进程,每个进程一个线程)以及工人(单进程、多线程)。这两种方式都能解决节点导致单个内核过载的问题,但集群由于每个进程都具有内存隔离性,因此这种方式更为简单。将Solana gRPC 分配给多个实例,集群或工人以分散负荷。 -
gRPC GogRPC 使用Solana gRPC (另请参阅《GogRPC Solana gRPC 指南》)。
概述
TypeScript is a strongly-typed superset of JavaScript, offering improved developer experience and tooling support. This document provides a step-by-step process for setting up a TypeScript environment for interacting with Solana gRPC (Yellowstone-compatible Geyser gRPC), including setting up a project, configuring dependencies, and implementing authentication mechanisms.
Authentication Required for TypeScript
Creates a client in order to communicate with Solana gRPC. It uses an endpoint URL and an authentication token for secure access.
import Client from '@triton-one/yellowstone-grpc';
// For HTTP Provider URL: https://example-guide-demo.solana-mainnet.quiknode.pro/123456789/
const ENDPOINT = 'https://example-guide-demo.solana-mainnet.quiknode.pro:443';
const TOKEN = '123456789';
const client = new Client(ENDPOINT, TOKEN, {
grpcDefaultCompressionAlgorithm: 0, // 0 = gzip, 1 = zstd
});
await client.connect();
The token passed to the Client constructor is sent as the x-token metadata header on every request.
gzip and zstd are selected with the grpcDefaultCompressionAlgorithm channel option. See Compression for per-language configuration.
To learn how to split your Solana gRPC-enabled URL into endpoint 和 token, refer to this section - Endpoint and Token Configuration
Setting up TypeScript
To run our TypeScript code examples, you'll need to have Node.js installed, as TypeScript is built on top of JavaScript. Ensure you also have TypeScript installed globally by running:
npm install -g typescript
Install Yellowstone gRPC Node Package
npm install @triton-one/yellowstone-grpc
These examples target grpc v7.0.1 or later.
其他资源
For more information about working with Solana gRPC in TypeScript, refer to Monitor Solana Programs with Solana gRPC (TypeScript). This guide provides an in-depth overview of setting up gRPC endpoints, managing authentication tokens, and understanding key concepts like commitment levels.