Skip to content
Documentation
Config

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 loading proto 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 NameValid ValuesDescription
keepCasetrue or falsePreserve field names. By default, they are changed to camel case.
longsString or NumberType used to represent long values. Default is the Long object type.
enumsStringType used to represent enum values. Default is a numeric type.
bytesArray or StringType used to represent bytes values. Default is Buffer.
defaultstrue or falseSet default values on the output object. Default is false.
arraystrue or falseSet empty arrays for missing array values, even if defaults is false. Default is false.
objectstrue or falseSet empty objects for missing object values, even if defaults is false. Default is false.
oneofstrue or falseSet virtual oneof properties as field names. Default is false.
jsontrue or falseRepresent Infinity and NaN as strings in float fields and automatically decode google.protobuf.Any values. Default is false.
includeDirsArray of stringsList 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']
    }
  })
}