clientconn.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. *
  3. * Copyright 2014, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. package grpc
  34. import (
  35. "errors"
  36. "fmt"
  37. "net"
  38. "strings"
  39. "sync"
  40. "time"
  41. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  42. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/trace"
  43. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/credentials"
  44. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/grpclog"
  45. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/transport"
  46. )
  47. var (
  48. // ErrUnspecTarget indicates that the target address is unspecified.
  49. ErrUnspecTarget = errors.New("grpc: target is unspecified")
  50. // ErrNoTransportSecurity indicates that there is no transport security
  51. // being set for ClientConn. Users should either set one or explicityly
  52. // call WithInsecure DialOption to disable security.
  53. ErrNoTransportSecurity = errors.New("grpc: no transport security set (use grpc.WithInsecure() explicitly or set credentials)")
  54. // ErrCredentialsMisuse indicates that users want to transmit security infomation
  55. // (e.g., oauth2 token) which requires secure connection on an insecure
  56. // connection.
  57. ErrCredentialsMisuse = errors.New("grpc: the credentials require transport level security (use grpc.WithTransportAuthenticator() to set)")
  58. // ErrClientConnClosing indicates that the operation is illegal because
  59. // the session is closing.
  60. ErrClientConnClosing = errors.New("grpc: the client connection is closing")
  61. // ErrClientConnTimeout indicates that the connection could not be
  62. // established or re-established within the specified timeout.
  63. ErrClientConnTimeout = errors.New("grpc: timed out trying to connect")
  64. // minimum time to give a connection to complete
  65. minConnectTimeout = 20 * time.Second
  66. )
  67. // dialOptions configure a Dial call. dialOptions are set by the DialOption
  68. // values passed to Dial.
  69. type dialOptions struct {
  70. codec Codec
  71. cp Compressor
  72. dc Decompressor
  73. picker Picker
  74. block bool
  75. insecure bool
  76. copts transport.ConnectOptions
  77. }
  78. // DialOption configures how we set up the connection.
  79. type DialOption func(*dialOptions)
  80. // WithCodec returns a DialOption which sets a codec for message marshaling and unmarshaling.
  81. func WithCodec(c Codec) DialOption {
  82. return func(o *dialOptions) {
  83. o.codec = c
  84. }
  85. }
  86. // WithCompressor returns a DialOption which sets a CompressorGenerator for generating message
  87. // compressor.
  88. func WithCompressor(cp Compressor) DialOption {
  89. return func(o *dialOptions) {
  90. o.cp = cp
  91. }
  92. }
  93. // WithDecompressor returns a DialOption which sets a DecompressorGenerator for generating
  94. // message decompressor.
  95. func WithDecompressor(dc Decompressor) DialOption {
  96. return func(o *dialOptions) {
  97. o.dc = dc
  98. }
  99. }
  100. // WithPicker returns a DialOption which sets a picker for connection selection.
  101. func WithPicker(p Picker) DialOption {
  102. return func(o *dialOptions) {
  103. o.picker = p
  104. }
  105. }
  106. // WithBlock returns a DialOption which makes caller of Dial blocks until the underlying
  107. // connection is up. Without this, Dial returns immediately and connecting the server
  108. // happens in background.
  109. func WithBlock() DialOption {
  110. return func(o *dialOptions) {
  111. o.block = true
  112. }
  113. }
  114. // WithInsecure returns a DialOption which disables transport security for this ClientConn.
  115. // Note that transport security is required unless WithInsecure is set.
  116. func WithInsecure() DialOption {
  117. return func(o *dialOptions) {
  118. o.insecure = true
  119. }
  120. }
  121. // WithTransportCredentials returns a DialOption which configures a
  122. // connection level security credentials (e.g., TLS/SSL).
  123. func WithTransportCredentials(creds credentials.TransportAuthenticator) DialOption {
  124. return func(o *dialOptions) {
  125. o.copts.AuthOptions = append(o.copts.AuthOptions, creds)
  126. }
  127. }
  128. // WithPerRPCCredentials returns a DialOption which sets
  129. // credentials which will place auth state on each outbound RPC.
  130. func WithPerRPCCredentials(creds credentials.Credentials) DialOption {
  131. return func(o *dialOptions) {
  132. o.copts.AuthOptions = append(o.copts.AuthOptions, creds)
  133. }
  134. }
  135. // WithTimeout returns a DialOption that configures a timeout for dialing a client connection.
  136. func WithTimeout(d time.Duration) DialOption {
  137. return func(o *dialOptions) {
  138. o.copts.Timeout = d
  139. }
  140. }
  141. // WithDialer returns a DialOption that specifies a function to use for dialing network addresses.
  142. func WithDialer(f func(addr string, timeout time.Duration) (net.Conn, error)) DialOption {
  143. return func(o *dialOptions) {
  144. o.copts.Dialer = f
  145. }
  146. }
  147. // WithUserAgent returns a DialOption that specifies a user agent string for all the RPCs.
  148. func WithUserAgent(s string) DialOption {
  149. return func(o *dialOptions) {
  150. o.copts.UserAgent = s
  151. }
  152. }
  153. // Dial creates a client connection the given target.
  154. func Dial(target string, opts ...DialOption) (*ClientConn, error) {
  155. cc := &ClientConn{
  156. target: target,
  157. }
  158. for _, opt := range opts {
  159. opt(&cc.dopts)
  160. }
  161. if cc.dopts.codec == nil {
  162. // Set the default codec.
  163. cc.dopts.codec = protoCodec{}
  164. }
  165. if cc.dopts.picker == nil {
  166. cc.dopts.picker = &unicastPicker{
  167. target: target,
  168. }
  169. }
  170. if err := cc.dopts.picker.Init(cc); err != nil {
  171. return nil, err
  172. }
  173. colonPos := strings.LastIndex(target, ":")
  174. if colonPos == -1 {
  175. colonPos = len(target)
  176. }
  177. cc.authority = target[:colonPos]
  178. return cc, nil
  179. }
  180. // ConnectivityState indicates the state of a client connection.
  181. type ConnectivityState int
  182. const (
  183. // Idle indicates the ClientConn is idle.
  184. Idle ConnectivityState = iota
  185. // Connecting indicates the ClienConn is connecting.
  186. Connecting
  187. // Ready indicates the ClientConn is ready for work.
  188. Ready
  189. // TransientFailure indicates the ClientConn has seen a failure but expects to recover.
  190. TransientFailure
  191. // Shutdown indicates the ClientConn has started shutting down.
  192. Shutdown
  193. )
  194. func (s ConnectivityState) String() string {
  195. switch s {
  196. case Idle:
  197. return "IDLE"
  198. case Connecting:
  199. return "CONNECTING"
  200. case Ready:
  201. return "READY"
  202. case TransientFailure:
  203. return "TRANSIENT_FAILURE"
  204. case Shutdown:
  205. return "SHUTDOWN"
  206. default:
  207. panic(fmt.Sprintf("unknown connectivity state: %d", s))
  208. }
  209. }
  210. // ClientConn represents a client connection to an RPC service.
  211. type ClientConn struct {
  212. target string
  213. authority string
  214. dopts dialOptions
  215. }
  216. // State returns the connectivity state of cc.
  217. // This is EXPERIMENTAL API.
  218. func (cc *ClientConn) State() (ConnectivityState, error) {
  219. return cc.dopts.picker.State()
  220. }
  221. // WaitForStateChange blocks until the state changes to something other than the sourceState.
  222. // It returns the new state or error.
  223. // This is EXPERIMENTAL API.
  224. func (cc *ClientConn) WaitForStateChange(ctx context.Context, sourceState ConnectivityState) (ConnectivityState, error) {
  225. return cc.dopts.picker.WaitForStateChange(ctx, sourceState)
  226. }
  227. // Close starts to tear down the ClientConn.
  228. func (cc *ClientConn) Close() error {
  229. return cc.dopts.picker.Close()
  230. }
  231. // Conn is a client connection to a single destination.
  232. type Conn struct {
  233. target string
  234. dopts dialOptions
  235. resetChan chan int
  236. shutdownChan chan struct{}
  237. events trace.EventLog
  238. mu sync.Mutex
  239. state ConnectivityState
  240. stateCV *sync.Cond
  241. // ready is closed and becomes nil when a new transport is up or failed
  242. // due to timeout.
  243. ready chan struct{}
  244. transport transport.ClientTransport
  245. }
  246. // NewConn creates a Conn.
  247. func NewConn(cc *ClientConn) (*Conn, error) {
  248. if cc.target == "" {
  249. return nil, ErrUnspecTarget
  250. }
  251. c := &Conn{
  252. target: cc.target,
  253. dopts: cc.dopts,
  254. resetChan: make(chan int, 1),
  255. shutdownChan: make(chan struct{}),
  256. }
  257. if EnableTracing {
  258. c.events = trace.NewEventLog("grpc.ClientConn", c.target)
  259. }
  260. if !c.dopts.insecure {
  261. var ok bool
  262. for _, cd := range c.dopts.copts.AuthOptions {
  263. if _, ok := cd.(credentials.TransportAuthenticator); !ok {
  264. continue
  265. }
  266. ok = true
  267. }
  268. if !ok {
  269. return nil, ErrNoTransportSecurity
  270. }
  271. } else {
  272. for _, cd := range c.dopts.copts.AuthOptions {
  273. if cd.RequireTransportSecurity() {
  274. return nil, ErrCredentialsMisuse
  275. }
  276. }
  277. }
  278. c.stateCV = sync.NewCond(&c.mu)
  279. if c.dopts.block {
  280. if err := c.resetTransport(false); err != nil {
  281. c.Close()
  282. return nil, err
  283. }
  284. // Start to monitor the error status of transport.
  285. go c.transportMonitor()
  286. } else {
  287. // Start a goroutine connecting to the server asynchronously.
  288. go func() {
  289. if err := c.resetTransport(false); err != nil {
  290. grpclog.Printf("Failed to dial %s: %v; please retry.", c.target, err)
  291. c.Close()
  292. return
  293. }
  294. c.transportMonitor()
  295. }()
  296. }
  297. return c, nil
  298. }
  299. // printf records an event in cc's event log, unless cc has been closed.
  300. // REQUIRES cc.mu is held.
  301. func (cc *Conn) printf(format string, a ...interface{}) {
  302. if cc.events != nil {
  303. cc.events.Printf(format, a...)
  304. }
  305. }
  306. // errorf records an error in cc's event log, unless cc has been closed.
  307. // REQUIRES cc.mu is held.
  308. func (cc *Conn) errorf(format string, a ...interface{}) {
  309. if cc.events != nil {
  310. cc.events.Errorf(format, a...)
  311. }
  312. }
  313. // State returns the connectivity state of the Conn
  314. func (cc *Conn) State() ConnectivityState {
  315. cc.mu.Lock()
  316. defer cc.mu.Unlock()
  317. return cc.state
  318. }
  319. // WaitForStateChange blocks until the state changes to something other than the sourceState.
  320. func (cc *Conn) WaitForStateChange(ctx context.Context, sourceState ConnectivityState) (ConnectivityState, error) {
  321. cc.mu.Lock()
  322. defer cc.mu.Unlock()
  323. if sourceState != cc.state {
  324. return cc.state, nil
  325. }
  326. done := make(chan struct{})
  327. var err error
  328. go func() {
  329. select {
  330. case <-ctx.Done():
  331. cc.mu.Lock()
  332. err = ctx.Err()
  333. cc.stateCV.Broadcast()
  334. cc.mu.Unlock()
  335. case <-done:
  336. }
  337. }()
  338. defer close(done)
  339. for sourceState == cc.state {
  340. cc.stateCV.Wait()
  341. if err != nil {
  342. return cc.state, err
  343. }
  344. }
  345. return cc.state, nil
  346. }
  347. // NotifyReset tries to signal the underlying transport needs to be reset due to
  348. // for example a name resolution change in flight.
  349. func (cc *Conn) NotifyReset() {
  350. select {
  351. case cc.resetChan <- 0:
  352. default:
  353. }
  354. }
  355. func (cc *Conn) resetTransport(closeTransport bool) error {
  356. var retries int
  357. start := time.Now()
  358. for {
  359. cc.mu.Lock()
  360. cc.printf("connecting")
  361. if cc.state == Shutdown {
  362. // cc.Close() has been invoked.
  363. cc.mu.Unlock()
  364. return ErrClientConnClosing
  365. }
  366. cc.state = Connecting
  367. cc.stateCV.Broadcast()
  368. cc.mu.Unlock()
  369. if closeTransport {
  370. cc.transport.Close()
  371. }
  372. // Adjust timeout for the current try.
  373. copts := cc.dopts.copts
  374. if copts.Timeout < 0 {
  375. cc.Close()
  376. return ErrClientConnTimeout
  377. }
  378. if copts.Timeout > 0 {
  379. copts.Timeout -= time.Since(start)
  380. if copts.Timeout <= 0 {
  381. cc.Close()
  382. return ErrClientConnTimeout
  383. }
  384. }
  385. sleepTime := backoff(retries)
  386. timeout := sleepTime
  387. if timeout < minConnectTimeout {
  388. timeout = minConnectTimeout
  389. }
  390. if copts.Timeout == 0 || copts.Timeout > timeout {
  391. copts.Timeout = timeout
  392. }
  393. connectTime := time.Now()
  394. addr, err := cc.dopts.picker.PickAddr()
  395. var newTransport transport.ClientTransport
  396. if err == nil {
  397. newTransport, err = transport.NewClientTransport(addr, &copts)
  398. }
  399. if err != nil {
  400. cc.mu.Lock()
  401. if cc.state == Shutdown {
  402. // cc.Close() has been invoked.
  403. cc.mu.Unlock()
  404. return ErrClientConnClosing
  405. }
  406. cc.errorf("transient failure: %v", err)
  407. cc.state = TransientFailure
  408. cc.stateCV.Broadcast()
  409. if cc.ready != nil {
  410. close(cc.ready)
  411. cc.ready = nil
  412. }
  413. cc.mu.Unlock()
  414. sleepTime -= time.Since(connectTime)
  415. if sleepTime < 0 {
  416. sleepTime = 0
  417. }
  418. // Fail early before falling into sleep.
  419. if cc.dopts.copts.Timeout > 0 && cc.dopts.copts.Timeout < sleepTime+time.Since(start) {
  420. cc.mu.Lock()
  421. cc.errorf("connection timeout")
  422. cc.mu.Unlock()
  423. cc.Close()
  424. return ErrClientConnTimeout
  425. }
  426. closeTransport = false
  427. time.Sleep(sleepTime)
  428. retries++
  429. grpclog.Printf("grpc: Conn.resetTransport failed to create client transport: %v; Reconnecting to %q", err, cc.target)
  430. continue
  431. }
  432. cc.mu.Lock()
  433. cc.printf("ready")
  434. if cc.state == Shutdown {
  435. // cc.Close() has been invoked.
  436. cc.mu.Unlock()
  437. newTransport.Close()
  438. return ErrClientConnClosing
  439. }
  440. cc.state = Ready
  441. cc.stateCV.Broadcast()
  442. cc.transport = newTransport
  443. if cc.ready != nil {
  444. close(cc.ready)
  445. cc.ready = nil
  446. }
  447. cc.mu.Unlock()
  448. return nil
  449. }
  450. }
  451. func (cc *Conn) reconnect() bool {
  452. cc.mu.Lock()
  453. if cc.state == Shutdown {
  454. // cc.Close() has been invoked.
  455. cc.mu.Unlock()
  456. return false
  457. }
  458. cc.state = TransientFailure
  459. cc.stateCV.Broadcast()
  460. cc.mu.Unlock()
  461. if err := cc.resetTransport(true); err != nil {
  462. // The ClientConn is closing.
  463. cc.mu.Lock()
  464. cc.printf("transport exiting: %v", err)
  465. cc.mu.Unlock()
  466. grpclog.Printf("grpc: Conn.transportMonitor exits due to: %v", err)
  467. return false
  468. }
  469. return true
  470. }
  471. // Run in a goroutine to track the error in transport and create the
  472. // new transport if an error happens. It returns when the channel is closing.
  473. func (cc *Conn) transportMonitor() {
  474. for {
  475. select {
  476. // shutdownChan is needed to detect the teardown when
  477. // the ClientConn is idle (i.e., no RPC in flight).
  478. case <-cc.shutdownChan:
  479. return
  480. case <-cc.resetChan:
  481. if !cc.reconnect() {
  482. return
  483. }
  484. case <-cc.transport.Error():
  485. if !cc.reconnect() {
  486. return
  487. }
  488. // Tries to drain reset signal if there is any since it is out-dated.
  489. select {
  490. case <-cc.resetChan:
  491. default:
  492. }
  493. }
  494. }
  495. }
  496. // Wait blocks until i) the new transport is up or ii) ctx is done or iii) cc is closed.
  497. func (cc *Conn) Wait(ctx context.Context) (transport.ClientTransport, error) {
  498. for {
  499. cc.mu.Lock()
  500. switch {
  501. case cc.state == Shutdown:
  502. cc.mu.Unlock()
  503. return nil, ErrClientConnClosing
  504. case cc.state == Ready:
  505. ct := cc.transport
  506. cc.mu.Unlock()
  507. return ct, nil
  508. default:
  509. ready := cc.ready
  510. if ready == nil {
  511. ready = make(chan struct{})
  512. cc.ready = ready
  513. }
  514. cc.mu.Unlock()
  515. select {
  516. case <-ctx.Done():
  517. return nil, transport.ContextErr(ctx.Err())
  518. // Wait until the new transport is ready or failed.
  519. case <-ready:
  520. }
  521. }
  522. }
  523. }
  524. // Close starts to tear down the Conn. Returns ErrClientConnClosing if
  525. // it has been closed (mostly due to dial time-out).
  526. // TODO(zhaoq): Make this synchronous to avoid unbounded memory consumption in
  527. // some edge cases (e.g., the caller opens and closes many ClientConn's in a
  528. // tight loop.
  529. func (cc *Conn) Close() error {
  530. cc.mu.Lock()
  531. defer cc.mu.Unlock()
  532. if cc.state == Shutdown {
  533. return ErrClientConnClosing
  534. }
  535. cc.state = Shutdown
  536. cc.stateCV.Broadcast()
  537. if cc.events != nil {
  538. cc.events.Finish()
  539. cc.events = nil
  540. }
  541. if cc.ready != nil {
  542. close(cc.ready)
  543. cc.ready = nil
  544. }
  545. if cc.transport != nil {
  546. cc.transport.Close()
  547. }
  548. if cc.shutdownChan != nil {
  549. close(cc.shutdownChan)
  550. }
  551. return nil
  552. }