Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

189 строки
5.4 KiB

  1. // Copyright 2018 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. "io"
  17. "sync"
  18. "time"
  19. gax "github.com/googleapis/gax-go"
  20. "golang.org/x/net/context"
  21. pb "google.golang.org/genproto/googleapis/pubsub/v1"
  22. "google.golang.org/grpc"
  23. )
  24. // A pullStream supports the methods of a StreamingPullClient, but re-opens
  25. // the stream on a retryable error.
  26. type pullStream struct {
  27. ctx context.Context
  28. open func() (pb.Subscriber_StreamingPullClient, error)
  29. mu sync.Mutex
  30. spc *pb.Subscriber_StreamingPullClient
  31. err error // permanent error
  32. }
  33. // for testing
  34. type streamingPullFunc func(context.Context, ...gax.CallOption) (pb.Subscriber_StreamingPullClient, error)
  35. func newPullStream(ctx context.Context, streamingPull streamingPullFunc, subName string, ackDeadlineSecs int32) *pullStream {
  36. ctx = withSubscriptionKey(ctx, subName)
  37. return &pullStream{
  38. ctx: ctx,
  39. open: func() (pb.Subscriber_StreamingPullClient, error) {
  40. spc, err := streamingPull(ctx, gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(maxSendRecvBytes)))
  41. if err == nil {
  42. recordStat(ctx, StreamRequestCount, 1)
  43. err = spc.Send(&pb.StreamingPullRequest{
  44. Subscription: subName,
  45. StreamAckDeadlineSeconds: ackDeadlineSecs,
  46. })
  47. }
  48. if err != nil {
  49. return nil, err
  50. }
  51. return spc, nil
  52. },
  53. }
  54. }
  55. // get returns either a valid *StreamingPullClient (SPC), or a permanent error.
  56. // If the argument is nil, this is the first call for an RPC, and the current
  57. // SPC will be returned (or a new one will be opened). Otherwise, this call is a
  58. // request to re-open the stream because of a retryable error, and the argument
  59. // is a pointer to the SPC that returned the error.
  60. func (s *pullStream) get(spc *pb.Subscriber_StreamingPullClient) (*pb.Subscriber_StreamingPullClient, error) {
  61. s.mu.Lock()
  62. defer s.mu.Unlock()
  63. // A stored error is permanent.
  64. if s.err != nil {
  65. return nil, s.err
  66. }
  67. // If the context is done, so are we.
  68. select {
  69. case <-s.ctx.Done():
  70. s.err = s.ctx.Err()
  71. return nil, s.err
  72. default:
  73. }
  74. // TODO(jba): We can use the following instead of the above after we drop support for 1.8:
  75. // s.err = s.ctx.Err()
  76. // if s.err != nil {
  77. // return nil, s.err
  78. // }
  79. // If the current and argument SPCs differ, return the current one. This subsumes two cases:
  80. // 1. We have an SPC and the caller is getting the stream for the first time.
  81. // 2. The caller wants to retry, but they have an older SPC; we've already retried.
  82. if spc != s.spc {
  83. return s.spc, nil
  84. }
  85. // Either this is the very first call on this stream (s.spc == nil), or we have a valid
  86. // retry request. Either way, open a new stream.
  87. // The lock is held here for a long time, but it doesn't matter because no callers could get
  88. // anything done anyway.
  89. s.spc = new(pb.Subscriber_StreamingPullClient)
  90. *s.spc, s.err = s.openWithRetry() // Any error from openWithRetry is permanent.
  91. return s.spc, s.err
  92. }
  93. func (s *pullStream) openWithRetry() (pb.Subscriber_StreamingPullClient, error) {
  94. var bo gax.Backoff
  95. for {
  96. recordStat(s.ctx, StreamOpenCount, 1)
  97. spc, err := s.open()
  98. if err != nil && isRetryable(err) {
  99. recordStat(s.ctx, StreamRetryCount, 1)
  100. if err := gax.Sleep(s.ctx, bo.Pause()); err != nil {
  101. return nil, err
  102. }
  103. continue
  104. }
  105. return spc, err
  106. }
  107. }
  108. func (s *pullStream) call(f func(pb.Subscriber_StreamingPullClient) error) error {
  109. var (
  110. spc *pb.Subscriber_StreamingPullClient
  111. err error
  112. bo gax.Backoff
  113. )
  114. for {
  115. spc, err = s.get(spc)
  116. if err != nil {
  117. return err
  118. }
  119. start := time.Now()
  120. err = f(*spc)
  121. if err != nil {
  122. if isRetryable(err) {
  123. recordStat(s.ctx, StreamRetryCount, 1)
  124. if time.Since(start) < 30*time.Second { // don't sleep if we've been blocked for a while
  125. if err := gax.Sleep(s.ctx, bo.Pause()); err != nil {
  126. return err
  127. }
  128. }
  129. continue
  130. }
  131. s.mu.Lock()
  132. s.err = err
  133. s.mu.Unlock()
  134. }
  135. return err
  136. }
  137. }
  138. func (s *pullStream) Send(req *pb.StreamingPullRequest) error {
  139. return s.call(func(spc pb.Subscriber_StreamingPullClient) error {
  140. recordStat(s.ctx, AckCount, int64(len(req.AckIds)))
  141. zeroes := 0
  142. for _, mds := range req.ModifyDeadlineSeconds {
  143. if mds == 0 {
  144. zeroes++
  145. }
  146. }
  147. recordStat(s.ctx, NackCount, int64(zeroes))
  148. recordStat(s.ctx, ModAckCount, int64(len(req.ModifyDeadlineSeconds)-zeroes))
  149. recordStat(s.ctx, StreamRequestCount, 1)
  150. return spc.Send(req)
  151. })
  152. }
  153. func (s *pullStream) Recv() (*pb.StreamingPullResponse, error) {
  154. var res *pb.StreamingPullResponse
  155. err := s.call(func(spc pb.Subscriber_StreamingPullClient) error {
  156. var err error
  157. recordStat(s.ctx, StreamResponseCount, 1)
  158. res, err = spc.Recv()
  159. if err == nil {
  160. recordStat(s.ctx, PullCount, int64(len(res.ReceivedMessages)))
  161. }
  162. return err
  163. })
  164. return res, err
  165. }
  166. func (s *pullStream) CloseSend() error {
  167. err := s.call(func(spc pb.Subscriber_StreamingPullClient) error {
  168. return spc.CloseSend()
  169. })
  170. s.mu.Lock()
  171. s.err = io.EOF // should not be retried
  172. s.mu.Unlock()
  173. return err
  174. }