cubefs
162 строки · 5.8 Кб
1/*
2*
3* Copyright 2020 gRPC authors.
4*
5* Licensed under the Apache License, Version 2.0 (the "License");
6* you may not use this file except in compliance with the License.
7* You may obtain a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS,
13* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14* See the License for the specific language governing permissions and
15* limitations under the License.
16*
17*/
18
19// Package serviceconfig contains utility functions to parse service config.
20package serviceconfig21
22import (23"encoding/json"24"fmt"25"time"26
27"google.golang.org/grpc/balancer"28"google.golang.org/grpc/codes"29"google.golang.org/grpc/grpclog"30externalserviceconfig "google.golang.org/grpc/serviceconfig"31)
32
33var logger = grpclog.Component("core")34
35// BalancerConfig wraps the name and config associated with one load balancing
36// policy. It corresponds to a single entry of the loadBalancingConfig field
37// from ServiceConfig.
38//
39// It implements the json.Unmarshaler interface.
40//
41// https://github.com/grpc/grpc-proto/blob/54713b1e8bc6ed2d4f25fb4dff527842150b91b2/grpc/service_config/service_config.proto#L247
42type BalancerConfig struct {43Name string44Config externalserviceconfig.LoadBalancingConfig45}
46
47type intermediateBalancerConfig []map[string]json.RawMessage48
49// UnmarshalJSON implements the json.Unmarshaler interface.
50//
51// ServiceConfig contains a list of loadBalancingConfigs, each with a name and
52// config. This method iterates through that list in order, and stops at the
53// first policy that is supported.
54// - If the config for the first supported policy is invalid, the whole service
55// config is invalid.
56// - If the list doesn't contain any supported policy, the whole service config
57// is invalid.
58func (bc *BalancerConfig) UnmarshalJSON(b []byte) error {59var ir intermediateBalancerConfig60err := json.Unmarshal(b, &ir)61if err != nil {62return err63}64
65for i, lbcfg := range ir {66if len(lbcfg) != 1 {67return fmt.Errorf("invalid loadBalancingConfig: entry %v does not contain exactly 1 policy/config pair: %q", i, lbcfg)68}69
70var (71name string72jsonCfg json.RawMessage73)74// Get the key:value pair from the map. We have already made sure that75// the map contains a single entry.76for name, jsonCfg = range lbcfg {77}78
79builder := balancer.Get(name)80if builder == nil {81// If the balancer is not registered, move on to the next config.82// This is not an error.83continue84}85bc.Name = name86
87parser, ok := builder.(balancer.ConfigParser)88if !ok {89if string(jsonCfg) != "{}" {90logger.Warningf("non-empty balancer configuration %q, but balancer does not implement ParseConfig", string(jsonCfg))91}92// Stop at this, though the builder doesn't support parsing config.93return nil94}95
96cfg, err := parser.ParseConfig(jsonCfg)97if err != nil {98return fmt.Errorf("error parsing loadBalancingConfig for policy %q: %v", name, err)99}100bc.Config = cfg101return nil102}103// This is reached when the for loop iterates over all entries, but didn't104// return. This means we had a loadBalancingConfig slice but did not105// encounter a registered policy. The config is considered invalid in this106// case.107return fmt.Errorf("invalid loadBalancingConfig: no supported policies found")108}
109
110// MethodConfig defines the configuration recommended by the service providers for a
111// particular method.
112type MethodConfig struct {113// WaitForReady indicates whether RPCs sent to this method should wait until114// the connection is ready by default (!failfast). The value specified via the115// gRPC client API will override the value set here.116WaitForReady *bool117// Timeout is the default timeout for RPCs sent to this method. The actual118// deadline used will be the minimum of the value specified here and the value119// set by the application via the gRPC client API. If either one is not set,120// then the other will be used. If neither is set, then the RPC has no deadline.121Timeout *time.Duration122// MaxReqSize is the maximum allowed payload size for an individual request in a123// stream (client->server) in bytes. The size which is measured is the serialized124// payload after per-message compression (but before stream compression) in bytes.125// The actual value used is the minimum of the value specified here and the value set126// by the application via the gRPC client API. If either one is not set, then the other127// will be used. If neither is set, then the built-in default is used.128MaxReqSize *int129// MaxRespSize is the maximum allowed payload size for an individual response in a130// stream (server->client) in bytes.131MaxRespSize *int132// RetryPolicy configures retry options for the method.133RetryPolicy *RetryPolicy134}
135
136// RetryPolicy defines the go-native version of the retry policy defined by the
137// service config here:
138// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#integration-with-service-config
139type RetryPolicy struct {140// MaxAttempts is the maximum number of attempts, including the original RPC.141//142// This field is required and must be two or greater.143MaxAttempts int144
145// Exponential backoff parameters. The initial retry attempt will occur at146// random(0, initialBackoff). In general, the nth attempt will occur at147// random(0,148// min(initialBackoff*backoffMultiplier**(n-1), maxBackoff)).149//150// These fields are required and must be greater than zero.151InitialBackoff time.Duration152MaxBackoff time.Duration153BackoffMultiplier float64154
155// The set of status codes which may be retried.156//157// Status codes are specified as strings, e.g., "UNAVAILABLE".158//159// This field is required and must be non-empty.160// Note: a set is used to store this for easy lookup.161RetryableStatusCodes map[codes.Code]bool162}
163