diff --git a/bar_tobeRemoved.go b/bar_tobeRemoved.go new file mode 100644 index 000000000000..8b6a4fadae69 --- /dev/null +++ b/bar_tobeRemoved.go @@ -0,0 +1,78 @@ +package grpc + +import ( + "google.golang.org/grpc/balancer" + "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/resolver" +) + +////////////////////////////////////////////////////////////////////////////// +////////////////////// To b or not to b. ///////////////////////// +////////////////////////////////////////////////////////////////////////////// + +// newSubConnection adds a new addrConn to clientconn, and returns a wrapper +// subConn for balancer. +func (cc *ClientConn) newSubConnection(addrs []resolver.Address, opts balancer.NewSubConnectionOptions) (*addrConn, error) { + grpclog.Printf("creating new subconn, addrs[0]: %v\n", addrs[0]) + ac, err := cc.resetAddrConn(addrs[0], false, nil) + if err != nil { + // TODO what to do? + grpclog.Printf("neverrrrrrrr got err when resetting addrconn %v", err) + return nil, err + } + return ac, nil +} + +// removeSubConnection removes the addrConn in the subConn from clientConn. +// It also tears down the ac with the given error. +func (cc *ClientConn) removeSubConnection(ac *addrConn, err error) { + grpclog.Printf("removing new subconn") + cc.mu.Lock() + delete(cc.conns, ac) + cc.mu.Unlock() + ac.tearDown(err) +} + +func (cc *ClientConn) updatePicker(p balancer.Picker) { + grpclog.Printf("cc.updatePicker: %p", p) + // TODO add a goroutine and sync it. + cc.pmu.Lock() + cc.picker = p + cc.pmu.Unlock() +} + +// ccBalancerWrapper is a wrapper on top of cc for balancers. +// It implements balancer.ClientConnection interface. +type ccBalancerWrapper struct { + cc *ClientConn +} + +func (ccb *ccBalancerWrapper) NewSubConnection(addrs []resolver.Address, opts balancer.NewSubConnectionOptions) (balancer.SubConnection, error) { + return ccb.cc.newSubConnection(addrs, opts) +} + +func (ccb *ccBalancerWrapper) RemoveSubConnection(sc balancer.SubConnection) { + ac, ok := sc.(*addrConn) + if !ok { + // TODO log or error? + return + } + ccb.cc.removeSubConnection(ac, errConnClosing) +} + +func (ccb *ccBalancerWrapper) UpdateBalancerState(s connectivity.State, p balancer.Picker) { + ccb.cc.updatePicker(p) +} + +func (ccb *ccBalancerWrapper) Target() string { + return ccb.cc.target +} + +////////////////////////////////////////////////////////////////////////////// +////////////////////// To a or not to a. ///////////////////////// +////////////////////////////////////////////////////////////////////////////// + +func (ac *addrConn) UpdateAddresses([]resolver.Address) {} + +func (ac *addrConn) Connect() {} diff --git a/clientconn.go b/clientconn.go index 45ea87eb61e8..db3a25e52541 100644 --- a/clientconn.go +++ b/clientconn.go @@ -673,15 +673,15 @@ func (ac *addrConn) connect(block bool) error { } if !ac.dopts.insecure { if ac.dopts.copts.TransportCredentials == nil { - return errNoTransportSecurity + return nil, errNoTransportSecurity } } else { if ac.dopts.copts.TransportCredentials != nil { - return errCredentialsConflict + return nil, errCredentialsConflict } for _, cd := range ac.dopts.copts.PerRPCCredentials { if cd.RequireTransportSecurity() { - return errTransportCredentialsMissing + return nil, errTransportCredentialsMissing } } } @@ -692,9 +692,9 @@ func (ac *addrConn) connect(block bool) error { ac.tearDown(err) } if e, ok := err.(transport.ConnectionError); ok && !e.Temporary() { - return e.Origin() + return nil, e.Origin() } - return err + return nil, err } // Start to monitor the error status of transport. go ac.transportMonitor() @@ -712,7 +712,7 @@ func (ac *addrConn) connect(block bool) error { ac.transportMonitor() }() } - return nil + return ac, nil } // tryUpdateAddrs tries to update ac.addrs with the new addresses list. @@ -905,7 +905,7 @@ func (ac *addrConn) resetTransport(drain bool) error { return errConnClosing } oldState := ac.state - ac.state = connectivity.Connecting + ac.state = connectivity.TransientFailure ac.csEvltr.recordTransition(oldState, ac.state) if ac.cc.balancer != nil { ac.cc.balancer.HandleSubConnStateChange(ac.acbw, ac.state) diff --git a/doc.go b/doc.go index 187adbb117f2..a5098bb33a7e 100644 --- a/doc.go +++ b/doc.go @@ -22,3 +22,9 @@ Package grpc implements an RPC system called gRPC. See grpc.io for more information about gRPC. */ package grpc // import "google.golang.org/grpc" + +import ( + _ "google.golang.org/grpc/balancer" // test build + _ "google.golang.org/grpc/connectivity" // test build + _ "google.golang.org/grpc/resolver" // test build +)