3 * Copyright 2014 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 // writeQuota is a soft limit on the amount of data a stream can
29 // schedule before some of it is written out.
30 type writeQuota struct {
32 // get waits on read from when quota goes less than or equal to zero.
33 // replenish writes on it when quota goes positive again.
35 // done is triggered in error case.
37 // replenish is called by loopyWriter to give quota back to.
38 // It is implemented as a field so that it can be updated
43 func newWriteQuota(sz int32, done <-chan struct{}) *writeQuota {
46 ch: make(chan struct{}, 1),
49 w.replenish = w.realReplenish
53 func (w *writeQuota) get(sz int32) error {
55 if atomic.LoadInt32(&w.quota) > 0 {
56 atomic.AddInt32(&w.quota, -sz)
68 func (w *writeQuota) realReplenish(n int) {
70 a := atomic.AddInt32(&w.quota, sz)
74 case w.ch <- struct{}{}:
80 type trInFlow struct {
83 effectiveWindowSize uint32
86 func (f *trInFlow) newLimit(n uint32) uint32 {
89 f.updateEffectiveWindowSize()
93 func (f *trInFlow) onData(n uint32) uint32 {
95 if f.unacked >= f.limit/4 {
98 f.updateEffectiveWindowSize()
101 f.updateEffectiveWindowSize()
105 func (f *trInFlow) reset() uint32 {
108 f.updateEffectiveWindowSize()
112 func (f *trInFlow) updateEffectiveWindowSize() {
113 atomic.StoreUint32(&f.effectiveWindowSize, f.limit-f.unacked)
116 func (f *trInFlow) getSize() uint32 {
117 return atomic.LoadUint32(&f.effectiveWindowSize)
120 // TODO(mmukhi): Simplify this code.
121 // inFlow deals with inbound flow control
124 // The inbound flow control limit for pending data.
126 // pendingData is the overall data which have been received but not been
127 // consumed by applications.
129 // The amount of data the application has consumed but grpc has not sent
130 // window update for them. Used to reduce window update frequency.
132 // delta is the extra window update given by receiver when an application
133 // is reading data bigger in size than the inFlow limit.
137 // newLimit updates the inflow window to a new value n.
138 // It assumes that n is always greater than the old limit.
139 func (f *inFlow) newLimit(n uint32) uint32 {
147 func (f *inFlow) maybeAdjust(n uint32) uint32 {
148 if n > uint32(math.MaxInt32) {
149 n = uint32(math.MaxInt32)
152 // estSenderQuota is the receiver's view of the maximum number of bytes the sender
153 // can send without a window update.
154 estSenderQuota := int32(f.limit - (f.pendingData + f.pendingUpdate))
155 // estUntransmittedData is the maximum number of bytes the sends might not have put
156 // on the wire yet. A value of 0 or less means that we have already received all or
157 // more bytes than the application is requesting to read.
158 estUntransmittedData := int32(n - f.pendingData) // Casting into int32 since it could be negative.
159 // This implies that unless we send a window update, the sender won't be able to send all the bytes
160 // for this message. Therefore we must send an update over the limit since there's an active read
161 // request from the application.
162 if estUntransmittedData > estSenderQuota {
163 // Sender's window shouldn't go more than 2^31 - 1 as specified in the HTTP spec.
164 if f.limit+n > maxWindowSize {
165 f.delta = maxWindowSize - f.limit
167 // Send a window update for the whole message and not just the difference between
168 // estUntransmittedData and estSenderQuota. This will be helpful in case the message
169 // is padded; We will fallback on the current available window(at least a 1/4th of the limit).
179 // onData is invoked when some data frame is received. It updates pendingData.
180 func (f *inFlow) onData(n uint32) error {
183 if f.pendingData+f.pendingUpdate > f.limit+f.delta {
185 rcvd := f.pendingData + f.pendingUpdate
187 return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", rcvd, limit)
193 // onRead is invoked when the application reads the data. It returns the window size
194 // to be sent to the peer.
195 func (f *inFlow) onRead(n uint32) uint32 {
197 if f.pendingData == 0 {
210 if f.pendingUpdate >= f.limit/4 {
211 wu := f.pendingUpdate