Skip to content

Commit 59d4e92

Browse files
committed
http2: add ClientConnState, ClientConn.State accessor
So ClientConnPool can be implemented with the same functionality as the built-in one. And for observability. Change-Id: I855fbac3d9e4c02ab213a3e6a80b00f857648264 Reviewed-on: https://go-review.googlesource.com/c/net/+/354351 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Damien Neil <[email protected]> Trust: Brad Fitzpatrick <[email protected]>
1 parent 62292e8 commit 59d4e92

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

http2/transport.go

+55
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,61 @@ func (cc *ClientConn) ReserveNewRequest() bool {
759759
return true
760760
}
761761

762+
// ClientConnState describes the state of a ClientConn.
763+
type ClientConnState struct {
764+
// Closed is whether the connection is closed.
765+
Closed bool
766+
767+
// Closing is whether the connection is in the process of
768+
// closing. It may be closing due to shutdown, being a
769+
// single-use connection, being marked as DoNotReuse, or
770+
// having received a GOAWAY frame.
771+
Closing bool
772+
773+
// StreamsActive is how many streams are active.
774+
StreamsActive int
775+
776+
// StreamsReserved is how many streams have been reserved via
777+
// ClientConn.ReserveNewRequest.
778+
StreamsReserved int
779+
780+
// StreamsPending is how many requests have been sent in excess
781+
// of the peer's advertised MaxConcurrentStreams setting and
782+
// are waiting for other streams to complete.
783+
StreamsPending int
784+
785+
// MaxConcurrentStreams is how many concurrent streams the
786+
// peer advertised as acceptable. Zero means no SETTINGS
787+
// frame has been received yet.
788+
MaxConcurrentStreams uint32
789+
790+
// LastIdle, if non-zero, is when the connection last
791+
// transitioned to idle state.
792+
LastIdle time.Time
793+
}
794+
795+
// State returns a snapshot of cc's state.
796+
func (cc *ClientConn) State() ClientConnState {
797+
cc.wmu.Lock()
798+
maxConcurrent := cc.maxConcurrentStreams
799+
if !cc.seenSettings {
800+
maxConcurrent = 0
801+
}
802+
cc.wmu.Unlock()
803+
804+
cc.mu.Lock()
805+
defer cc.mu.Unlock()
806+
return ClientConnState{
807+
Closed: cc.closed,
808+
Closing: cc.closing || cc.singleUse || cc.doNotReuse || cc.goAway != nil,
809+
StreamsActive: len(cc.streams),
810+
StreamsReserved: cc.streamsReserved,
811+
StreamsPending: cc.pendingRequests,
812+
LastIdle: cc.lastIdle,
813+
MaxConcurrentStreams: maxConcurrent,
814+
}
815+
}
816+
762817
// clientConnIdleState describes the suitability of a client
763818
// connection to initiate a new RoundTrip request.
764819
type clientConnIdleState struct {

0 commit comments

Comments
 (0)