service_config.go 6.7 KB

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