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{}{}:
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)
153
// estSenderQuota is the receiver's view of the maximum number of bytes the sender
154
// can send without a window update.
155
estSenderQuota := int32(f.limit - (f.pendingData + f.pendingUpdate))
156
// estUntransmittedData is the maximum number of bytes the sends might not have put
157
// on the wire yet. A value of 0 or less means that we have already received all or
158
// more bytes than the application is requesting to read.
159
estUntransmittedData := int32(n - f.pendingData) // Casting into int32 since it could be negative.
160
// This implies that unless we send a window update, the sender won't be able to send all the bytes
161
// for this message. Therefore we must send an update over the limit since there's an active read
162
// request from the application.
163
if estUntransmittedData > estSenderQuota {
164
// Sender's window shouldn't go more than 2^31 - 1 as specified in the HTTP spec.
165
if f.limit+n > maxWindowSize {
166
f.delta = maxWindowSize - f.limit
168
// Send a window update for the whole message and not just the difference between
169
// estUntransmittedData and estSenderQuota. This will be helpful in case the message
170
// is padded; We will fallback on the current available window(at least a 1/4th of the limit).
178
// onData is invoked when some data frame is received. It updates pendingData.
179
func (f *inFlow) onData(n uint32) error {
182
if f.pendingData+f.pendingUpdate > f.limit+f.delta {
184
rcvd := f.pendingData + f.pendingUpdate
186
return fmt.Errorf("received %d-bytes data exceeding the limit %d bytes", rcvd, limit)
192
// onRead is invoked when the application reads the data. It returns the window size
193
// to be sent to the peer.
194
func (f *inFlow) onRead(n uint32) uint32 {
196
if f.pendingData == 0 {
209
if f.pendingUpdate >= f.limit/4 {
210
wu := f.pendingUpdate