2017-10-19 22:09:19 +03:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Copyright 2017 gRPC authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-12-05 21:04:04 +03:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-10-19 22:09:19 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/grpclog"
|
|
|
|
)
|
|
|
|
|
2017-10-26 23:30:15 +03:00
|
|
|
const maxInt = int(^uint(0) >> 1)
|
|
|
|
|
2017-10-19 22:09:19 +03:00
|
|
|
// MethodConfig defines the configuration recommended by the service providers for a
|
|
|
|
// particular method.
|
2018-05-02 18:52:50 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Users should not use this struct. Service config should be received
|
2017-10-19 22:09:19 +03:00
|
|
|
// through name resolver, as specified here
|
|
|
|
// https://github.com/grpc/grpc/blob/master/doc/service_config.md
|
|
|
|
type MethodConfig struct {
|
|
|
|
// WaitForReady indicates whether RPCs sent to this method should wait until
|
|
|
|
// the connection is ready by default (!failfast). The value specified via the
|
|
|
|
// gRPC client API will override the value set here.
|
|
|
|
WaitForReady *bool
|
|
|
|
// Timeout is the default timeout for RPCs sent to this method. The actual
|
|
|
|
// deadline used will be the minimum of the value specified here and the value
|
|
|
|
// set by the application via the gRPC client API. If either one is not set,
|
|
|
|
// then the other will be used. If neither is set, then the RPC has no deadline.
|
|
|
|
Timeout *time.Duration
|
|
|
|
// MaxReqSize is the maximum allowed payload size for an individual request in a
|
|
|
|
// stream (client->server) in bytes. The size which is measured is the serialized
|
|
|
|
// payload after per-message compression (but before stream compression) in bytes.
|
|
|
|
// The actual value used is the minimum of the value specified here and the value set
|
|
|
|
// by the application via the gRPC client API. If either one is not set, then the other
|
|
|
|
// will be used. If neither is set, then the built-in default is used.
|
|
|
|
MaxReqSize *int
|
|
|
|
// MaxRespSize is the maximum allowed payload size for an individual response in a
|
|
|
|
// stream (server->client) in bytes.
|
|
|
|
MaxRespSize *int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceConfig is provided by the service provider and contains parameters for how
|
|
|
|
// clients that connect to the service should behave.
|
2018-05-02 18:52:50 +03:00
|
|
|
//
|
|
|
|
// Deprecated: Users should not use this struct. Service config should be received
|
2017-10-19 22:09:19 +03:00
|
|
|
// through name resolver, as specified here
|
|
|
|
// https://github.com/grpc/grpc/blob/master/doc/service_config.md
|
|
|
|
type ServiceConfig struct {
|
|
|
|
// LB is the load balancer the service providers recommends. The balancer specified
|
|
|
|
// via grpc.WithBalancer will override this.
|
|
|
|
LB *string
|
|
|
|
// Methods contains a map for the methods in this service.
|
|
|
|
// If there is an exact match for a method (i.e. /service/method) in the map, use the corresponding MethodConfig.
|
|
|
|
// If there's no exact match, look for the default config for the service (/service/) and use the corresponding MethodConfig if it exists.
|
|
|
|
// Otherwise, the method has no MethodConfig to use.
|
|
|
|
Methods map[string]MethodConfig
|
|
|
|
}
|
|
|
|
|
2017-12-05 21:04:04 +03:00
|
|
|
func parseDuration(s *string) (*time.Duration, error) {
|
|
|
|
if s == nil {
|
2017-10-19 22:09:19 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
2017-12-05 21:04:04 +03:00
|
|
|
if !strings.HasSuffix(*s, "s") {
|
|
|
|
return nil, fmt.Errorf("malformed duration %q", *s)
|
|
|
|
}
|
|
|
|
ss := strings.SplitN((*s)[:len(*s)-1], ".", 3)
|
|
|
|
if len(ss) > 2 {
|
|
|
|
return nil, fmt.Errorf("malformed duration %q", *s)
|
|
|
|
}
|
|
|
|
// hasDigits is set if either the whole or fractional part of the number is
|
|
|
|
// present, since both are optional but one is required.
|
|
|
|
hasDigits := false
|
|
|
|
var d time.Duration
|
|
|
|
if len(ss[0]) > 0 {
|
|
|
|
i, err := strconv.ParseInt(ss[0], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("malformed duration %q: %v", *s, err)
|
|
|
|
}
|
|
|
|
d = time.Duration(i) * time.Second
|
|
|
|
hasDigits = true
|
|
|
|
}
|
|
|
|
if len(ss) == 2 && len(ss[1]) > 0 {
|
|
|
|
if len(ss[1]) > 9 {
|
|
|
|
return nil, fmt.Errorf("malformed duration %q", *s)
|
|
|
|
}
|
|
|
|
f, err := strconv.ParseInt(ss[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("malformed duration %q: %v", *s, err)
|
|
|
|
}
|
|
|
|
for i := 9; i > len(ss[1]); i-- {
|
|
|
|
f *= 10
|
|
|
|
}
|
|
|
|
d += time.Duration(f)
|
|
|
|
hasDigits = true
|
|
|
|
}
|
|
|
|
if !hasDigits {
|
|
|
|
return nil, fmt.Errorf("malformed duration %q", *s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &d, nil
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonName struct {
|
|
|
|
Service *string
|
|
|
|
Method *string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j jsonName) generatePath() (string, bool) {
|
|
|
|
if j.Service == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
res := "/" + *j.Service + "/"
|
|
|
|
if j.Method != nil {
|
|
|
|
res += *j.Method
|
|
|
|
}
|
|
|
|
return res, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
|
|
|
|
type jsonMC struct {
|
|
|
|
Name *[]jsonName
|
|
|
|
WaitForReady *bool
|
|
|
|
Timeout *string
|
2017-10-26 23:30:15 +03:00
|
|
|
MaxRequestMessageBytes *int64
|
|
|
|
MaxResponseMessageBytes *int64
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
|
|
|
|
type jsonSC struct {
|
2018-06-26 20:02:54 +03:00
|
|
|
LoadBalancingPolicy *string
|
|
|
|
MethodConfig *[]jsonMC
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseServiceConfig(js string) (ServiceConfig, error) {
|
|
|
|
var rsc jsonSC
|
|
|
|
err := json.Unmarshal([]byte(js), &rsc)
|
|
|
|
if err != nil {
|
|
|
|
grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
|
|
|
|
return ServiceConfig{}, err
|
|
|
|
}
|
|
|
|
sc := ServiceConfig{
|
|
|
|
LB: rsc.LoadBalancingPolicy,
|
|
|
|
Methods: make(map[string]MethodConfig),
|
|
|
|
}
|
|
|
|
if rsc.MethodConfig == nil {
|
|
|
|
return sc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range *rsc.MethodConfig {
|
|
|
|
if m.Name == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-12-05 21:04:04 +03:00
|
|
|
d, err := parseDuration(m.Timeout)
|
2017-10-19 22:09:19 +03:00
|
|
|
if err != nil {
|
|
|
|
grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
|
|
|
|
return ServiceConfig{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mc := MethodConfig{
|
|
|
|
WaitForReady: m.WaitForReady,
|
|
|
|
Timeout: d,
|
2017-10-26 23:30:15 +03:00
|
|
|
}
|
|
|
|
if m.MaxRequestMessageBytes != nil {
|
|
|
|
if *m.MaxRequestMessageBytes > int64(maxInt) {
|
|
|
|
mc.MaxReqSize = newInt(maxInt)
|
|
|
|
} else {
|
|
|
|
mc.MaxReqSize = newInt(int(*m.MaxRequestMessageBytes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.MaxResponseMessageBytes != nil {
|
|
|
|
if *m.MaxResponseMessageBytes > int64(maxInt) {
|
|
|
|
mc.MaxRespSize = newInt(maxInt)
|
|
|
|
} else {
|
|
|
|
mc.MaxRespSize = newInt(int(*m.MaxResponseMessageBytes))
|
|
|
|
}
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
for _, n := range *m.Name {
|
|
|
|
if path, valid := n.generatePath(); valid {
|
|
|
|
sc.Methods[path] = mc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc, nil
|
|
|
|
}
|
|
|
|
|
2017-10-25 20:31:00 +03:00
|
|
|
func min(a, b *int) *int {
|
|
|
|
if *a < *b {
|
2017-10-19 22:09:19 +03:00
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
|
|
|
|
if mcMax == nil && doptMax == nil {
|
2017-10-25 20:31:00 +03:00
|
|
|
return &defaultVal
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
if mcMax != nil && doptMax != nil {
|
2017-10-25 20:31:00 +03:00
|
|
|
return min(mcMax, doptMax)
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
if mcMax != nil {
|
2017-10-25 20:31:00 +03:00
|
|
|
return mcMax
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
2017-10-25 20:31:00 +03:00
|
|
|
return doptMax
|
2017-10-19 22:09:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func newInt(b int) *int {
|
|
|
|
return &b
|
|
|
|
}
|