blocking when max concurrent stream limit is reached.
This commit is contained in:
@ -795,17 +795,23 @@ func testExceedMaxStreamsLimit(t *testing.T, e env) {
|
|||||||
s, cc := setUp(1, e)
|
s, cc := setUp(1, e)
|
||||||
tc := testpb.NewTestServiceClient(cc)
|
tc := testpb.NewTestServiceClient(cc)
|
||||||
defer tearDown(s, cc)
|
defer tearDown(s, cc)
|
||||||
var err error
|
// Perform an unary RPC to make sure the new settings were propagated to the client.
|
||||||
for {
|
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
|
||||||
time.Sleep(2 * time.Millisecond)
|
t.Fatalf("fhaof")
|
||||||
_, err = tc.StreamingInputCall(context.Background())
|
|
||||||
// Loop until the settings of max concurrent streams is
|
|
||||||
// received by the client.
|
|
||||||
if err != nil {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
// Initiate the 1st stream
|
||||||
|
if _, err := tc.StreamingInputCall(context.Background()); err != nil {
|
||||||
|
t.Fatalf("faf")
|
||||||
}
|
}
|
||||||
if grpc.Code(err) != codes.Unavailable {
|
var wg sync.WaitGroup
|
||||||
t.Fatalf("got %v, want error code %d", err, codes.Unavailable)
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
// The 2nd stream should block until its deadline exceeds.
|
||||||
|
ctx, _ := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
if _, err := tc.StreamingInputCall(ctx); grpc.Code(err) != codes.DeadlineExceeded {
|
||||||
|
t.Fatalf("1414")
|
||||||
}
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,7 @@ type http2Client struct {
|
|||||||
fc *inFlow
|
fc *inFlow
|
||||||
// sendQuotaPool provides flow control to outbound message.
|
// sendQuotaPool provides flow control to outbound message.
|
||||||
sendQuotaPool *quotaPool
|
sendQuotaPool *quotaPool
|
||||||
|
streamsQuota *quotaPool
|
||||||
|
|
||||||
// The scheme used: https if TLS is on, http otherwise.
|
// The scheme used: https if TLS is on, http otherwise.
|
||||||
scheme string
|
scheme string
|
||||||
@ -89,7 +90,7 @@ type http2Client struct {
|
|||||||
state transportState // the state of underlying connection
|
state transportState // the state of underlying connection
|
||||||
activeStreams map[uint32]*Stream
|
activeStreams map[uint32]*Stream
|
||||||
// The max number of concurrent streams
|
// The max number of concurrent streams
|
||||||
maxStreams uint32
|
maxStreams int
|
||||||
// the per-stream outbound flow control window size set by the peer.
|
// the per-stream outbound flow control window size set by the peer.
|
||||||
streamSendQuota uint32
|
streamSendQuota uint32
|
||||||
}
|
}
|
||||||
@ -174,8 +175,8 @@ func newHTTP2Client(addr string, opts *ConnectOptions) (_ ClientTransport, err e
|
|||||||
scheme: scheme,
|
scheme: scheme,
|
||||||
state: reachable,
|
state: reachable,
|
||||||
activeStreams: make(map[uint32]*Stream),
|
activeStreams: make(map[uint32]*Stream),
|
||||||
maxStreams: math.MaxUint32,
|
|
||||||
authCreds: opts.AuthOptions,
|
authCreds: opts.AuthOptions,
|
||||||
|
maxStreams: math.MaxInt32,
|
||||||
streamSendQuota: defaultWindowSize,
|
streamSendQuota: defaultWindowSize,
|
||||||
}
|
}
|
||||||
go t.controller()
|
go t.controller()
|
||||||
@ -236,19 +237,26 @@ func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (_ *Strea
|
|||||||
authData[k] = v
|
authData[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, err := wait(ctx, t.shutdownChan, t.writableChan); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
if t.state != reachable {
|
if t.state != reachable {
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
return nil, ErrConnClosing
|
return nil, ErrConnClosing
|
||||||
}
|
}
|
||||||
if uint32(len(t.activeStreams)) >= t.maxStreams {
|
if t.streamsQuota != nil {
|
||||||
|
q, err := wait(ctx, t.shutdownChan, t.streamsQuota.acquire())
|
||||||
|
if err != nil {
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
t.writableChan <- 0
|
return nil, err
|
||||||
return nil, StreamErrorf(codes.Unavailable, "transport: failed to create new stream because the limit has been reached.")
|
|
||||||
}
|
}
|
||||||
|
if q > 1 {
|
||||||
|
t.streamsQuota.add(q-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.mu.Unlock()
|
||||||
|
if _, err := wait(ctx, t.shutdownChan, t.writableChan); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
t.mu.Lock()
|
||||||
s := t.newStream(ctx, callHdr)
|
s := t.newStream(ctx, callHdr)
|
||||||
t.activeStreams[s.id] = s
|
t.activeStreams[s.id] = s
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
@ -318,6 +326,9 @@ func (t *http2Client) NewStream(ctx context.Context, callHdr *CallHdr) (_ *Strea
|
|||||||
func (t *http2Client) CloseStream(s *Stream, err error) {
|
func (t *http2Client) CloseStream(s *Stream, err error) {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
delete(t.activeStreams, s.id)
|
delete(t.activeStreams, s.id)
|
||||||
|
if t.streamsQuota != nil {
|
||||||
|
t.streamsQuota.add(1)
|
||||||
|
}
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
if q := s.fc.restoreConn(); q > 0 {
|
if q := s.fc.restoreConn(); q > 0 {
|
||||||
@ -558,7 +569,18 @@ func (t *http2Client) handleSettings(f *http2.SettingsFrame) {
|
|||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
switch s.ID {
|
switch s.ID {
|
||||||
case http2.SettingMaxConcurrentStreams:
|
case http2.SettingMaxConcurrentStreams:
|
||||||
t.maxStreams = v
|
// TODO(zhaoq): This is a hack to avoid significant refactoring of the
|
||||||
|
// code to deal with int32 overflow. Have a better way to handle this
|
||||||
|
// later.
|
||||||
|
if v > math.MaxInt32 {
|
||||||
|
v = math.MaxInt32
|
||||||
|
}
|
||||||
|
if t.streamsQuota == nil {
|
||||||
|
t.streamsQuota = newQuotaPool(int(v))
|
||||||
|
} else {
|
||||||
|
t.streamsQuota.reset(int(v) - t.maxStreams)
|
||||||
|
}
|
||||||
|
t.maxStreams = int(v)
|
||||||
case http2.SettingInitialWindowSize:
|
case http2.SettingInitialWindowSize:
|
||||||
for _, s := range t.activeStreams {
|
for _, s := range t.activeStreams {
|
||||||
// Adjust the sending quota for each s.
|
// Adjust the sending quota for each s.
|
||||||
|
@ -451,3 +451,14 @@ func wait(ctx context.Context, closing <-chan struct{}, proceed <-chan int) (int
|
|||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func wait64(ctx context.Context, closing <-chan struct{}, proceed <-chan int64) (int64, error) {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return 0, ContextErr(ctx.Err())
|
||||||
|
case <-closing:
|
||||||
|
return 0, ErrConnClosing
|
||||||
|
case i := <-proceed:
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user