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

108 lines
3.3 KiB

  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 actual ids in
  26. // acknowledgement or modifyAckDeadline requests. A serialized
  27. // AcknowledgeRequest proto has a small constant overhead, plus the size of the
  28. // subscription name, plus 3 bytes per ID (a tag byte and two size bytes). A
  29. // ModifyAckDeadlineRequest has an additional few bytes for the deadline. We
  30. // don't know the subscription name here, so we just assume the size exclusive
  31. // of ids is 100 bytes.
  32. //
  33. // With gRPC there is no way for the client to know the server's max message size (it is
  34. // configurable on the server). We know from experience that it
  35. // it 512K.
  36. const (
  37. maxPayload = 512 * 1024
  38. reqFixedOverhead = 100
  39. overheadPerID = 3
  40. maxSendRecvBytes = 20 * 1024 * 1024 // 20M
  41. )
  42. func convertMessages(rms []*pb.ReceivedMessage) ([]*Message, error) {
  43. msgs := make([]*Message, 0, len(rms))
  44. for i, m := range rms {
  45. msg, err := toMessage(m)
  46. if err != nil {
  47. return nil, fmt.Errorf("pubsub: cannot decode the retrieved message at index: %d, message: %+v", i, m)
  48. }
  49. msgs = append(msgs, msg)
  50. }
  51. return msgs, nil
  52. }
  53. func trunc32(i int64) int32 {
  54. if i > math.MaxInt32 {
  55. i = math.MaxInt32
  56. }
  57. return int32(i)
  58. }
  59. type defaultRetryer struct {
  60. bo gax.Backoff
  61. }
  62. // Logic originally from
  63. // 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
  64. func (r *defaultRetryer) Retry(err error) (pause time.Duration, shouldRetry bool) {
  65. s, ok := status.FromError(err)
  66. if !ok { // includes io.EOF, normal stream close, which causes us to reopen
  67. return r.bo.Pause(), true
  68. }
  69. switch s.Code() {
  70. case codes.DeadlineExceeded, codes.Internal, codes.ResourceExhausted, codes.Aborted:
  71. return r.bo.Pause(), true
  72. case codes.Unavailable:
  73. c := strings.Contains(s.Message(), "Server shutdownNow invoked")
  74. if !c {
  75. return r.bo.Pause(), true
  76. }
  77. return 0, false
  78. default:
  79. return 0, false
  80. }
  81. }
  82. type streamingPullRetryer struct {
  83. defaultRetryer gax.Retryer
  84. }
  85. // Does not retry ResourceExhausted. See: https://github.com/GoogleCloudPlatform/google-cloud-go/issues/1166#issuecomment-443744705
  86. func (r *streamingPullRetryer) Retry(err error) (pause time.Duration, shouldRetry bool) {
  87. s, ok := status.FromError(err)
  88. if !ok { // call defaultRetryer so that its backoff can be used
  89. return r.defaultRetryer.Retry(err)
  90. }
  91. switch s.Code() {
  92. case codes.ResourceExhausted:
  93. return 0, false
  94. default:
  95. return r.defaultRetryer.Retry(err)
  96. }
  97. }