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.
 
 
 

79 lines
2.5 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. pb "google.golang.org/genproto/googleapis/pubsub/v1"
  20. "google.golang.org/grpc/codes"
  21. "google.golang.org/grpc/status"
  22. )
  23. // maxPayload is the maximum number of bytes to devote to actual ids in
  24. // acknowledgement or modifyAckDeadline requests. A serialized
  25. // AcknowledgeRequest proto has a small constant overhead, plus the size of the
  26. // subscription name, plus 3 bytes per ID (a tag byte and two size bytes). A
  27. // ModifyAckDeadlineRequest has an additional few bytes for the deadline. We
  28. // don't know the subscription name here, so we just assume the size exclusive
  29. // of ids is 100 bytes.
  30. //
  31. // With gRPC there is no way for the client to know the server's max message size (it is
  32. // configurable on the server). We know from experience that it
  33. // it 512K.
  34. const (
  35. maxPayload = 512 * 1024
  36. reqFixedOverhead = 100
  37. overheadPerID = 3
  38. maxSendRecvBytes = 20 * 1024 * 1024 // 20M
  39. )
  40. func convertMessages(rms []*pb.ReceivedMessage) ([]*Message, error) {
  41. msgs := make([]*Message, 0, len(rms))
  42. for i, m := range rms {
  43. msg, err := toMessage(m)
  44. if err != nil {
  45. return nil, fmt.Errorf("pubsub: cannot decode the retrieved message at index: %d, message: %+v", i, m)
  46. }
  47. msgs = append(msgs, msg)
  48. }
  49. return msgs, nil
  50. }
  51. func trunc32(i int64) int32 {
  52. if i > math.MaxInt32 {
  53. i = math.MaxInt32
  54. }
  55. return int32(i)
  56. }
  57. // Logic from 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
  58. func isRetryable(err error) bool {
  59. s, ok := status.FromError(err)
  60. if !ok { // includes io.EOF, normal stream close, which causes us to reopen
  61. return true
  62. }
  63. switch s.Code() {
  64. case codes.DeadlineExceeded, codes.Internal, codes.Canceled, codes.ResourceExhausted:
  65. return true
  66. case codes.Unavailable:
  67. return !strings.Contains(s.Message(), "Server shutdownNow invoked")
  68. default:
  69. return false
  70. }
  71. }