gopkg.in/rethinkdb/rethinkdb-go.v6@v6.2.2/node.go (about) 1 package rethinkdb 2 3 import ( 4 "sync" 5 6 "golang.org/x/net/context" 7 p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2" 8 ) 9 10 // Node represents a database server in the cluster 11 type Node struct { 12 ID string 13 Host Host 14 aliases []Host 15 16 pool *Pool 17 18 mu sync.RWMutex 19 closed bool 20 } 21 22 func newNode(id string, aliases []Host, pool *Pool) *Node { 23 node := &Node{ 24 ID: id, 25 Host: aliases[0], 26 aliases: aliases, 27 pool: pool, 28 } 29 30 return node 31 } 32 33 // Closed returns true if the node is connClosed 34 func (n *Node) Closed() bool { 35 n.mu.RLock() 36 defer n.mu.RUnlock() 37 38 return n.closed 39 } 40 41 // Close closes the session 42 func (n *Node) Close(optArgs ...CloseOpts) error { 43 n.mu.Lock() 44 defer n.mu.Unlock() 45 46 if n.closed { 47 return nil 48 } 49 50 if len(optArgs) >= 1 { 51 if optArgs[0].NoReplyWait { 52 n.NoReplyWait() 53 } 54 } 55 56 if n.pool != nil { 57 n.pool.Close() 58 } 59 n.pool = nil 60 n.closed = true 61 62 return nil 63 } 64 65 // SetInitialPoolCap sets the initial capacity of the connection pool. 66 func (n *Node) SetInitialPoolCap(idleConns int) { 67 n.pool.SetInitialPoolCap(idleConns) 68 } 69 70 // SetMaxIdleConns sets the maximum number of connections in the idle 71 // connection pool. 72 func (n *Node) SetMaxIdleConns(idleConns int) { 73 n.pool.SetMaxIdleConns(idleConns) 74 } 75 76 // SetMaxOpenConns sets the maximum number of open connections to the database. 77 func (n *Node) SetMaxOpenConns(openConns int) { 78 n.pool.SetMaxOpenConns(openConns) 79 } 80 81 // NoReplyWait ensures that previous queries with the noreply flag have been 82 // processed by the server. Note that this guarantee only applies to queries 83 // run on the given connection 84 func (n *Node) NoReplyWait() error { 85 return n.pool.Exec(nil, Query{ // nil = connection opts' timeout 86 Type: p.Query_NOREPLY_WAIT, 87 }) 88 } 89 90 // Query executes a ReQL query using this nodes connection pool. 91 func (n *Node) Query(ctx context.Context, q Query) (cursor *Cursor, err error) { 92 if n.Closed() { 93 return nil, ErrInvalidNode 94 } 95 96 return n.pool.Query(ctx, q) 97 } 98 99 // Exec executes a ReQL query using this nodes connection pool. 100 func (n *Node) Exec(ctx context.Context, q Query) (err error) { 101 if n.Closed() { 102 return ErrInvalidNode 103 } 104 105 return n.pool.Exec(ctx, q) 106 } 107 108 // Server returns the server name and server UUID being used by a connection. 109 func (n *Node) Server() (ServerResponse, error) { 110 var response ServerResponse 111 112 if n.Closed() { 113 return response, ErrInvalidNode 114 } 115 116 return n.pool.Server() 117 } 118 119 type nodeStatus struct { 120 ID string `rethinkdb:"id"` 121 Name string `rethinkdb:"name"` 122 Network nodeStatusNetwork `rethinkdb:"network"` 123 } 124 125 type nodeStatusNetwork struct { 126 Hostname string `rethinkdb:"hostname"` 127 ClusterPort int64 `rethinkdb:"cluster_port"` 128 ReqlPort int64 `rethinkdb:"reql_port"` 129 CanonicalAddresses []nodeStatusNetworkAddr `rethinkdb:"canonical_addresses"` 130 } 131 132 type nodeStatusNetworkAddr struct { 133 Host string `rethinkdb:"host"` 134 Port int64 `rethinkdb:"port"` 135 }