1*cc02d7e2SAndroid Build Coastguard WorkerService Config in gRPC 2*cc02d7e2SAndroid Build Coastguard Worker====================== 3*cc02d7e2SAndroid Build Coastguard Worker 4*cc02d7e2SAndroid Build Coastguard Worker# Objective 5*cc02d7e2SAndroid Build Coastguard Worker 6*cc02d7e2SAndroid Build Coastguard WorkerThe service config is a mechanism that allows service owners to publish 7*cc02d7e2SAndroid Build Coastguard Workerparameters to be automatically used by all clients of their service. 8*cc02d7e2SAndroid Build Coastguard Worker 9*cc02d7e2SAndroid Build Coastguard Worker# Format 10*cc02d7e2SAndroid Build Coastguard Worker 11*cc02d7e2SAndroid Build Coastguard WorkerThe fields of the service config are defined by the 12*cc02d7e2SAndroid Build Coastguard Worker[`grpc.service_config.ServiceConfig` protocol buffer 13*cc02d7e2SAndroid Build Coastguard Workermessage](https://github.com/grpc/grpc-proto/blob/master/grpc/service_config/service_config.proto). 14*cc02d7e2SAndroid Build Coastguard WorkerNote that new fields may be added in the future as new functionality is 15*cc02d7e2SAndroid Build Coastguard Workerintroduced. 16*cc02d7e2SAndroid Build Coastguard Worker 17*cc02d7e2SAndroid Build Coastguard WorkerInternally, gRPC uses the service config in JSON form. The JSON 18*cc02d7e2SAndroid Build Coastguard Workerrepresentation is the result of converting the protobuf form into JSON 19*cc02d7e2SAndroid Build Coastguard Workerusing the normal [protobuf to JSON translation 20*cc02d7e2SAndroid Build Coastguard Workerrules](https://developers.google.com/protocol-buffers/docs/proto3#json). 21*cc02d7e2SAndroid Build Coastguard WorkerIn particular, this means: 22*cc02d7e2SAndroid Build Coastguard Worker- Field names are converted from `snake_case` to `camelCase`. 23*cc02d7e2SAndroid Build Coastguard Worker- Field values are converted as per the documented translation rules: 24*cc02d7e2SAndroid Build Coastguard Worker - Strings, 32-bit integers, and bools are converted into the 25*cc02d7e2SAndroid Build Coastguard Worker corresponding JSON types. 26*cc02d7e2SAndroid Build Coastguard Worker - 64-bit integers are converted into strings (e.g., `"251"`). 27*cc02d7e2SAndroid Build Coastguard Worker - The value of a repeated field will be represented as a JSON array. 28*cc02d7e2SAndroid Build Coastguard Worker - The value of a `google.protobuf.Duration` will be represented as a 29*cc02d7e2SAndroid Build Coastguard Worker string containing a decimal number of seconds (e.g., `"1.000340012s"`). 30*cc02d7e2SAndroid Build Coastguard Worker 31*cc02d7e2SAndroid Build Coastguard WorkerFor more details, see the protobuf docs linked above. 32*cc02d7e2SAndroid Build Coastguard Worker 33*cc02d7e2SAndroid Build Coastguard WorkerNote that the JSON representation has one advantage over the protobuf 34*cc02d7e2SAndroid Build Coastguard Workerrepresentation, which is that it is possible to encode configurations 35*cc02d7e2SAndroid Build Coastguard Workerfor [LB policies](load-balancing.md) that are not known to gRPC. In 36*cc02d7e2SAndroid Build Coastguard Workerprotobuf form, the `loadBalancingConfig` field contains a `oneof` 37*cc02d7e2SAndroid Build Coastguard Workersupporting only the built-in LB policies. However, in JSON form, the 38*cc02d7e2SAndroid Build Coastguard Workerfield inside the `oneof` is encoded as a string that indicates the LB 39*cc02d7e2SAndroid Build Coastguard Workerpolicy name. In JSON form, that string can be any arbitrary value, not 40*cc02d7e2SAndroid Build Coastguard Workerjust one of the supported policies inside of the `oneof`, so third-party 41*cc02d7e2SAndroid Build Coastguard Workerpolicies can be selected. 42*cc02d7e2SAndroid Build Coastguard Worker 43*cc02d7e2SAndroid Build Coastguard Worker# Architecture 44*cc02d7e2SAndroid Build Coastguard Worker 45*cc02d7e2SAndroid Build Coastguard WorkerA service config is associated with a server name. The [name 46*cc02d7e2SAndroid Build Coastguard Workerresolver](naming.md) plugin, when asked to resolve a particular server 47*cc02d7e2SAndroid Build Coastguard Workername, will return both the resolved addresses and the service config. 48*cc02d7e2SAndroid Build Coastguard Worker 49*cc02d7e2SAndroid Build Coastguard WorkerThe name resolver returns the service config to the gRPC client in JSON form. 50*cc02d7e2SAndroid Build Coastguard WorkerIndividual resolver implementations determine where and in what format the 51*cc02d7e2SAndroid Build Coastguard Workerservice config is stored. If the resolver implemention obtains the 52*cc02d7e2SAndroid Build Coastguard Workerservice config in protobuf form, it must convert it to JSON. 53*cc02d7e2SAndroid Build Coastguard WorkerAlternatively, a resolver implementation may obtain the service config 54*cc02d7e2SAndroid Build Coastguard Workeralready in JSON form, in which case it may return it directly. Or it 55*cc02d7e2SAndroid Build Coastguard Workermay construct the JSON dynamically from some other source data. 56*cc02d7e2SAndroid Build Coastguard Worker 57*cc02d7e2SAndroid Build Coastguard WorkerFor details of how the DNS resolver plugin supports service configs, see 58*cc02d7e2SAndroid Build Coastguard Worker[gRFC A2: Service Config via 59*cc02d7e2SAndroid Build Coastguard WorkerDNS](https://github.com/grpc/proposal/blob/master/A2-service-configs-in-dns.md). 60*cc02d7e2SAndroid Build Coastguard Worker 61*cc02d7e2SAndroid Build Coastguard Worker# Example 62*cc02d7e2SAndroid Build Coastguard Worker 63*cc02d7e2SAndroid Build Coastguard WorkerHere is an example service config in protobuf form: 64*cc02d7e2SAndroid Build Coastguard Worker 65*cc02d7e2SAndroid Build Coastguard Worker```textproto 66*cc02d7e2SAndroid Build Coastguard Worker{ 67*cc02d7e2SAndroid Build Coastguard Worker # Use round_robin LB policy. 68*cc02d7e2SAndroid Build Coastguard Worker load_balancing_config: { round_robin: {} } 69*cc02d7e2SAndroid Build Coastguard Worker # This method config applies to method "foo/bar" and to all methods 70*cc02d7e2SAndroid Build Coastguard Worker # of service "baz". 71*cc02d7e2SAndroid Build Coastguard Worker method_config: { 72*cc02d7e2SAndroid Build Coastguard Worker name: { 73*cc02d7e2SAndroid Build Coastguard Worker service: "foo" 74*cc02d7e2SAndroid Build Coastguard Worker method: "bar" 75*cc02d7e2SAndroid Build Coastguard Worker } 76*cc02d7e2SAndroid Build Coastguard Worker name: { 77*cc02d7e2SAndroid Build Coastguard Worker service: "baz" 78*cc02d7e2SAndroid Build Coastguard Worker } 79*cc02d7e2SAndroid Build Coastguard Worker # Default timeout for matching methods. 80*cc02d7e2SAndroid Build Coastguard Worker timeout: { 81*cc02d7e2SAndroid Build Coastguard Worker seconds: 1 82*cc02d7e2SAndroid Build Coastguard Worker nanos: 1 83*cc02d7e2SAndroid Build Coastguard Worker } 84*cc02d7e2SAndroid Build Coastguard Worker } 85*cc02d7e2SAndroid Build Coastguard Worker} 86*cc02d7e2SAndroid Build Coastguard Worker``` 87*cc02d7e2SAndroid Build Coastguard Worker 88*cc02d7e2SAndroid Build Coastguard WorkerHere is the same example service config in JSON form: 89*cc02d7e2SAndroid Build Coastguard Worker 90*cc02d7e2SAndroid Build Coastguard Worker```json 91*cc02d7e2SAndroid Build Coastguard Worker{ 92*cc02d7e2SAndroid Build Coastguard Worker "loadBalancingConfig": [ { "round_robin": {} } ], 93*cc02d7e2SAndroid Build Coastguard Worker "methodConfig": [ 94*cc02d7e2SAndroid Build Coastguard Worker { 95*cc02d7e2SAndroid Build Coastguard Worker "name": [ 96*cc02d7e2SAndroid Build Coastguard Worker { "service": "foo", "method": "bar" }, 97*cc02d7e2SAndroid Build Coastguard Worker { "service": "baz" } 98*cc02d7e2SAndroid Build Coastguard Worker ], 99*cc02d7e2SAndroid Build Coastguard Worker "timeout": "1.000000001s" 100*cc02d7e2SAndroid Build Coastguard Worker } 101*cc02d7e2SAndroid Build Coastguard Worker ] 102*cc02d7e2SAndroid Build Coastguard Worker} 103*cc02d7e2SAndroid Build Coastguard Worker``` 104*cc02d7e2SAndroid Build Coastguard Worker 105*cc02d7e2SAndroid Build Coastguard Worker# APIs 106*cc02d7e2SAndroid Build Coastguard Worker 107*cc02d7e2SAndroid Build Coastguard WorkerThe service config is used in the following APIs: 108*cc02d7e2SAndroid Build Coastguard Worker 109*cc02d7e2SAndroid Build Coastguard Worker- In the resolver API, used by resolver plugins to return the service 110*cc02d7e2SAndroid Build Coastguard Worker config to the gRPC client. 111*cc02d7e2SAndroid Build Coastguard Worker- In the gRPC client API, where users can query the channel to obtain 112*cc02d7e2SAndroid Build Coastguard Worker the service config associated with the channel (for debugging 113*cc02d7e2SAndroid Build Coastguard Worker purposes). 114*cc02d7e2SAndroid Build Coastguard Worker- In the gRPC client API, where users can set the service config 115*cc02d7e2SAndroid Build Coastguard Worker explicitly. This can be used to set the config in unit tests. It can 116*cc02d7e2SAndroid Build Coastguard Worker also be used to set the default config that will be used if the 117*cc02d7e2SAndroid Build Coastguard Worker resolver plugin does not return a service config. 118