Rate-limit unary gRPC requests
Use this example to keep an application within known T-Invest quotas for unary requests. The application supplies a limiter and can set quotas for an individual SDK instance. The SDK waits for a permit before sending a request. The application controls the limiter's algorithm, coordination, and lifecycle.
Enable a limiter
In the examples, token and endpoint are your application's connection settings.
The SDK sends requests immediately by default. To rate-limit them within one process, pass createInMemoryUnaryLimiter() as the unaryLimiter option.
import { createInMemoryUnaryLimiter, TInvestNodeSDK } from '@woodger/t-invest-node-sdk';
const sdk = new TInvestNodeSDK({
token,
endpoint,
unaryLimiter: createInMemoryUnaryLimiter()
});
The limiter spaces permits evenly over time using a monotonic clock. It serves waiting requests in FIFO order.
One object can share state among SDK instances in a process. Conflicting configurations for the same bucket are rejected.
Each in-memory limiter coordinates within its own process. It uses the configured quotas and handles unary requests. The application can choose another limiter or leave limiting disabled.
Override quotas
Use unaryLimits to replace a package rule for one SDK instance. defineUnaryLimits() converts a nested declaration to flat UnaryLimits. The nested declaration type is exported as UnaryLimitsDefinition, and the flat form is supported too. Specify both window parameters.
import {
defineUnaryLimits,
TInvestNodeSDK,
type TInvestUnaryLimiter
} from '@woodger/t-invest-node-sdk';
declare const unaryLimiter: TInvestUnaryLimiter;
const sdk = new TInvestNodeSDK({
token,
endpoint,
unaryLimiter,
unaryLimits: defineUnaryLimits({
UsersService: {
default: {
maxRequests: 50,
windowMs: 60_000
}
},
OrdersService: {
methods: {
PostOrder: {
maxRequests: 10,
windowMs: 1_000
}
}
}
})
});
The SDK takes the other rules from defaultConfig.unaryLimits. Changing the quota for one method moves it out of the package's shared quota group, so each bucket has consistent settings. Changing all methods in a group identically preserves their shared bucket.
The SDK validates quota overrides when creating the instance, even without a limiter. Set unaryLimiter to wait for quota before requests.
Allocate a fixed share of the quota
If you know how many independent limiters use one quota, assign a fixed share to each.
const unaryLimiter = createInMemoryUnaryLimiter({
quotaShare: 0.5
});
quotaShare accepts a finite number in the range [0.2, 1] and defaults to 1. At the minimum share and the package's minimum quota of 5 requests per minute, each original window still has at least one request permit.
For shares below one, the limiter calculates maxRequests * quotaShare separately for every quota. A result of one or greater is rounded down. For example, with a share of 0.5, 600 requests per 60_000 ms becomes 300, and 15 per 1_000 ms becomes 7.
For a custom quota whose result is below one, the limiter uses a fractional rate and increases the spacing between permits. With 1 request per 60_000 ms and a share of 0.2, it grants one permit every 300_000 ms. Low custom quotas remain valid during RPCs.
TInvestUnaryLimitContext keeps its original values. A custom limiter still receives the original T-Invest maxRequests and windowMs.
This setting is static and applies independently in each process. A value of 0.5 splits known quotas evenly between two independently configured processes. One remaining process uses half the quota. Adding a third exceeds the assumed allocation. Multiple SDK instances with one shared limiter already share its counters and can use the same share.
See the limits policy for provider quota values and the custom limiter guide for other coordination scopes or waiting algorithms.