From 244bc75d79d47d6306cb35437ae8dfb327155ab7 Mon Sep 17 00:00:00 2001 From: iamqizhao Date: Fri, 31 Jul 2015 14:16:02 -0700 Subject: [PATCH] Switch ALPN/NPN to advertise only h2 --- clientconn.go | 40 ++++++++++++++++++++++++++++++++++++-- credentials/credentials.go | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/clientconn.go b/clientconn.go index 4ef00553..42221882 100644 --- a/clientconn.go +++ b/clientconn.go @@ -144,6 +144,7 @@ func Dial(target string, opts ...DialOption) (*ClientConn, error) { // Set the default codec. cc.dopts.codec = protoCodec{} } + cc.stateCV = sync.NewCond(&cc.mu) if cc.dopts.block { if err := cc.resetTransport(false); err != nil { cc.Close() @@ -188,8 +189,9 @@ type ClientConn struct { dopts dialOptions shutdownChan chan struct{} - mu sync.Mutex - state ConnectivityState + mu sync.Mutex + state ConnectivityState + stateCV *sync.Cond // ready is closed and becomes nil when a new transport is up or failed // due to timeout. ready chan struct{} @@ -200,6 +202,40 @@ type ClientConn struct { transport transport.ClientTransport } +// State returns the connectivity state of the ClientConn +func (cc *ClientConn) State() ConnectivityState { + cc.mu.Lock() + defer cc.mu.Unlock() + return cc.state +} + +// WaitForStateChange returns true when the state changes to something other than the +// sourceState and false if timeout fires. +func (cc *ClientConn) WaitForStateChange(timeout time.Duration, sourceState ConnectivityState) bool { + cc.mu.Lock() + defer cc.mu.Unlock() + if sourceState != cc.state { + return true + } + // Shutdown state is a sink -- once it is entered, no furhter state change could happen. + if sourceState == Shutdown { + return false + } + done := make(chan struct{}) + go func() { + select { + case <-time.After(timeout): + cc.stateCV.Broadcast() + case <-done: + } + }() + for sourceState == cc.state { + cc.stateCV.Wait() + } + close(done) + return true +} + func (cc *ClientConn) resetTransport(closeTransport bool) error { var retries int start := time.Now() diff --git a/credentials/credentials.go b/credentials/credentials.go index 0c2b24c0..c1a331e8 100644 --- a/credentials/credentials.go +++ b/credentials/credentials.go @@ -51,7 +51,7 @@ import ( var ( // alpnProtoStr are the specified application level protocols for gRPC. - alpnProtoStr = []string{"h2", "h2-14", "h2-15", "h2-16"} + alpnProtoStr = []string{"h2"} ) // Credentials defines the common interface all supported credentials must