service_config.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. *
  3. * Copyright 2017 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. package grpc
  19. import (
  20. "encoding/json"
  21. "fmt"
  22. "strconv"
  23. "strings"
  24. "time"
  25. "google.golang.org/grpc/grpclog"
  26. )
  27. const maxInt = int(^uint(0) >> 1)
  28. // MethodConfig defines the configuration recommended by the service providers for a
  29. // particular method.
  30. // DEPRECATED: Users should not use this struct. Service config should be received
  31. // through name resolver, as specified here
  32. // https://github.com/grpc/grpc/blob/master/doc/service_config.md
  33. type MethodConfig struct {
  34. // WaitForReady indicates whether RPCs sent to this method should wait until
  35. // the connection is ready by default (!failfast). The value specified via the
  36. // gRPC client API will override the value set here.
  37. WaitForReady *bool
  38. // Timeout is the default timeout for RPCs sent to this method. The actual
  39. // deadline used will be the minimum of the value specified here and the value
  40. // set by the application via the gRPC client API. If either one is not set,
  41. // then the other will be used. If neither is set, then the RPC has no deadline.
  42. Timeout *time.Duration
  43. // MaxReqSize is the maximum allowed payload size for an individual request in a
  44. // stream (client->server) in bytes. The size which is measured is the serialized
  45. // payload after per-message compression (but before stream compression) in bytes.
  46. // The actual value used is the minimum of the value specified here and the value set
  47. // by the application via the gRPC client API. If either one is not set, then the other
  48. // will be used. If neither is set, then the built-in default is used.
  49. MaxReqSize *int
  50. // MaxRespSize is the maximum allowed payload size for an individual response in a
  51. // stream (server->client) in bytes.
  52. MaxRespSize *int
  53. }
  54. // ServiceConfig is provided by the service provider and contains parameters for how
  55. // clients that connect to the service should behave.
  56. // DEPRECATED: Users should not use this struct. Service config should be received
  57. // through name resolver, as specified here
  58. // https://github.com/grpc/grpc/blob/master/doc/service_config.md
  59. type ServiceConfig struct {
  60. // LB is the load balancer the service providers recommends. The balancer specified
  61. // via grpc.WithBalancer will override this.
  62. LB *string
  63. // Methods contains a map for the methods in this service.
  64. // If there is an exact match for a method (i.e. /service/method) in the map, use the corresponding MethodConfig.
  65. // If there's no exact match, look for the default config for the service (/service/) and use the corresponding MethodConfig if it exists.
  66. // Otherwise, the method has no MethodConfig to use.
  67. Methods map[string]MethodConfig
  68. }
  69. func parseDuration(s *string) (*time.Duration, error) {
  70. if s == nil {
  71. return nil, nil
  72. }
  73. if !strings.HasSuffix(*s, "s") {
  74. return nil, fmt.Errorf("malformed duration %q", *s)
  75. }
  76. ss := strings.SplitN((*s)[:len(*s)-1], ".", 3)
  77. if len(ss) > 2 {
  78. return nil, fmt.Errorf("malformed duration %q", *s)
  79. }
  80. // hasDigits is set if either the whole or fractional part of the number is
  81. // present, since both are optional but one is required.
  82. hasDigits := false
  83. var d time.Duration
  84. if len(ss[0]) > 0 {
  85. i, err := strconv.ParseInt(ss[0], 10, 32)
  86. if err != nil {
  87. return nil, fmt.Errorf("malformed duration %q: %v", *s, err)
  88. }
  89. d = time.Duration(i) * time.Second
  90. hasDigits = true
  91. }
  92. if len(ss) == 2 && len(ss[1]) > 0 {
  93. if len(ss[1]) > 9 {
  94. return nil, fmt.Errorf("malformed duration %q", *s)
  95. }
  96. f, err := strconv.ParseInt(ss[1], 10, 64)
  97. if err != nil {
  98. return nil, fmt.Errorf("malformed duration %q: %v", *s, err)
  99. }
  100. for i := 9; i > len(ss[1]); i-- {
  101. f *= 10
  102. }
  103. d += time.Duration(f)
  104. hasDigits = true
  105. }
  106. if !hasDigits {
  107. return nil, fmt.Errorf("malformed duration %q", *s)
  108. }
  109. return &d, nil
  110. }
  111. type jsonName struct {
  112. Service *string
  113. Method *string
  114. }
  115. func (j jsonName) generatePath() (string, bool) {
  116. if j.Service == nil {
  117. return "", false
  118. }
  119. res := "/" + *j.Service + "/"
  120. if j.Method != nil {
  121. res += *j.Method
  122. }
  123. return res, true
  124. }
  125. // TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
  126. type jsonMC struct {
  127. Name *[]jsonName
  128. WaitForReady *bool
  129. Timeout *string
  130. MaxRequestMessageBytes *int64
  131. MaxResponseMessageBytes *int64
  132. }
  133. // TODO(lyuxuan): delete this struct after cleaning up old service config implementation.
  134. type jsonSC struct {
  135. LoadBalancingPolicy *string
  136. MethodConfig *[]jsonMC
  137. }
  138. func parseServiceConfig(js string) (ServiceConfig, error) {
  139. var rsc jsonSC
  140. err := json.Unmarshal([]byte(js), &rsc)
  141. if err != nil {
  142. grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
  143. return ServiceConfig{}, err
  144. }
  145. sc := ServiceConfig{
  146. LB: rsc.LoadBalancingPolicy,
  147. Methods: make(map[string]MethodConfig),
  148. }
  149. if rsc.MethodConfig == nil {
  150. return sc, nil
  151. }
  152. for _, m := range *rsc.MethodConfig {
  153. if m.Name == nil {
  154. continue
  155. }
  156. d, err := parseDuration(m.Timeout)
  157. if err != nil {
  158. grpclog.Warningf("grpc: parseServiceConfig error unmarshaling %s due to %v", js, err)
  159. return ServiceConfig{}, err
  160. }
  161. mc := MethodConfig{
  162. WaitForReady: m.WaitForReady,
  163. Timeout: d,
  164. }
  165. if m.MaxRequestMessageBytes != nil {
  166. if *m.MaxRequestMessageBytes > int64(maxInt) {
  167. mc.MaxReqSize = newInt(maxInt)
  168. } else {
  169. mc.MaxReqSize = newInt(int(*m.MaxRequestMessageBytes))
  170. }
  171. }
  172. if m.MaxResponseMessageBytes != nil {
  173. if *m.MaxResponseMessageBytes > int64(maxInt) {
  174. mc.MaxRespSize = newInt(maxInt)
  175. } else {
  176. mc.MaxRespSize = newInt(int(*m.MaxResponseMessageBytes))
  177. }
  178. }
  179. for _, n := range *m.Name {
  180. if path, valid := n.generatePath(); valid {
  181. sc.Methods[path] = mc
  182. }
  183. }
  184. }
  185. return sc, nil
  186. }
  187. func min(a, b *int) *int {
  188. if *a < *b {
  189. return a
  190. }
  191. return b
  192. }
  193. func getMaxSize(mcMax, doptMax *int, defaultVal int) *int {
  194. if mcMax == nil && doptMax == nil {
  195. return &defaultVal
  196. }
  197. if mcMax != nil && doptMax != nil {
  198. return min(mcMax, doptMax)
  199. }
  200. if mcMax != nil {
  201. return mcMax
  202. }
  203. return doptMax
  204. }
  205. func newInt(b int) *int {
  206. return &b
  207. }