Config
gRPCity
has two important configurations related to proto: loadOptions
and channelOptions
. Both are used in init()
, while initClients()
and initServer()
use only channelOptions
.
loadOptions
: Corresponds to the rule configuration when loadingproto
files.channelOptions
: Corresponds to the rule configuration for communication between the server and the client.
Default Options
import { defaultLoadOptions, defaultChannelOptions } from 'grpcity'
loadOptions
The fields included in loadOptions
are described as follows:
Field Name | Valid Values | Description |
---|---|---|
keepCase | true or false | Preserve field names. By default, they are changed to camel case. |
longs | String or Number | Type used to represent long values. Default is the Long object type. |
enums | String | Type used to represent enum values. Default is a numeric type. |
bytes | Array or String | Type used to represent bytes values. Default is Buffer . |
defaults | true or false | Set default values on the output object. Default is false . |
arrays | true or false | Set empty arrays for missing array values, even if defaults is false . Default is false . |
objects | true or false | Set empty objects for missing object values, even if defaults is false . Default is false . |
oneofs | true or false | Set virtual oneof properties as field names. Default is false . |
json | true or false | Represent Infinity and NaN as strings in float fields and automatically decode google.protobuf.Any values. Default is false . |
includeDirs | Array of strings | List of search paths for imported .proto files. |
The default loadOptions
for gRPCity
is:
{
keepCase: true,
longs: String,
enums: String,
defaults: false,
oneofs: true
}
channelOptions
channelOptions
aligns with the grpc-js
official specifications, and you can see all the channel
configuration options here. The currently supported configuration options can be found here.
The default channelOptions
for gRPCity
is:
{
// grpc library options accept only numeric and string types
// Configure grpc reconnect time (1s to 10s random number)
'grpc.min_reconnect_backoff_ms': 1000,
'grpc.max_reconnect_backoff_ms': 10000,
// Communication timeout
'grpc.grpclb_call_timeout_ms': 5000,
// The following 3 parameters address the issue of long-idle gRPC clients not promptly detecting TCP connection breaks
// After waiting for a duration of this time, if the keepalive ping sender does not receive the ping ack, it will close the transport.
'grpc.keepalive_timeout_ms': (20 * 1000),
// duration of this time the client/server pings its peer to see if the transport is still alive
'grpc.keepalive_time_ms': (120 * 1000),
'grpc.keepalive_permit_without_calls': 1,
// Communication failure allows grpc library retry
'grpc.enable_retries': 1,
// Configure retry parameters
// Reference: https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config
'grpc.service_config': JSON.stringify({
retryPolicy: {
// 1 normal send + 3 failed retries, each retry has the opportunity to be sent on a different subchannel than the original failure
maxAttempts: 4,
// Initial retry time interval: random(0, initialBackoff) seconds
// The nth retry time; random(0, min(initialBackoff*backoffMultiplier^(n-1), maxBackoff)) seconds
initialBackoff: '0.1s',
maxBackoff: '1s',
backoffMultiplier: 2,
// Retry only in the case of network unavailability
retryableStatusCodes: ['UNAVAILABLE']
}
})
}