You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.go 3.0 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package pubsub
  15. import (
  16. "fmt"
  17. "math"
  18. "strings"
  19. "time"
  20. gax "github.com/googleapis/gax-go/v2"
  21. pb "google.golang.org/genproto/googleapis/pubsub/v1"
  22. "google.golang.org/grpc/codes"
  23. "google.golang.org/grpc/status"
  24. )
  25. // maxPayload is the maximum number of bytes to devote to the
  26. // encoded AcknowledgementRequest / ModifyAckDeadline proto message.
  27. //
  28. // With gRPC there is no way for the client to know the server's max message size (it is
  29. // configurable on the server). We know from experience that it
  30. // it 512K.
  31. const (
  32. maxPayload = 512 * 1024
  33. maxSendRecvBytes = 20 * 1024 * 1024 // 20M
  34. )
  35. func convertMessages(rms []*pb.ReceivedMessage) ([]*Message, error) {
  36. msgs := make([]*Message, 0, len(rms))
  37. for i, m := range rms {
  38. msg, err := toMessage(m)
  39. if err != nil {
  40. return nil, fmt.Errorf("pubsub: cannot decode the retrieved message at index: %d, message: %+v", i, m)
  41. }
  42. msgs = append(msgs, msg)
  43. }
  44. return msgs, nil
  45. }
  46. func trunc32(i int64) int32 {
  47. if i > math.MaxInt32 {
  48. i = math.MaxInt32
  49. }
  50. return int32(i)
  51. }
  52. type defaultRetryer struct {
  53. bo gax.Backoff
  54. }
  55. // Logic originally from
  56. // https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-clients/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/v1/StatusUtil.java
  57. func (r *defaultRetryer) Retry(err error) (pause time.Duration, shouldRetry bool) {
  58. s, ok := status.FromError(err)
  59. if !ok { // includes io.EOF, normal stream close, which causes us to reopen
  60. return r.bo.Pause(), true
  61. }
  62. switch s.Code() {
  63. case codes.DeadlineExceeded, codes.Internal, codes.ResourceExhausted, codes.Aborted:
  64. return r.bo.Pause(), true
  65. case codes.Unavailable:
  66. c := strings.Contains(s.Message(), "Server shutdownNow invoked")
  67. if !c {
  68. return r.bo.Pause(), true
  69. }
  70. return 0, false
  71. default:
  72. return 0, false
  73. }
  74. }
  75. type streamingPullRetryer struct {
  76. defaultRetryer gax.Retryer
  77. }
  78. // Does not retry ResourceExhausted. See: https://github.com/GoogleCloudPlatform/google-cloud-go/issues/1166#issuecomment-443744705
  79. func (r *streamingPullRetryer) Retry(err error) (pause time.Duration, shouldRetry bool) {
  80. s, ok := status.FromError(err)
  81. if !ok { // call defaultRetryer so that its backoff can be used
  82. return r.defaultRetryer.Retry(err)
  83. }
  84. switch s.Code() {
  85. case codes.ResourceExhausted:
  86. return 0, false
  87. default:
  88. return r.defaultRetryer.Retry(err)
  89. }
  90. }