Skip to content

Commit 58cd5f1

Browse files
committed
Add Server function to connection
1 parent f5235eb commit 58cd5f1

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

cluster.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ func (c *Cluster) Exec(q Query) (err error) {
7070
return node.Exec(q)
7171
}
7272

73+
// Server returns the server name and server UUID being used by a connection.
74+
func (c *Cluster) Server() (ServerResponse, error) {
75+
node, err := c.GetRandomNode()
76+
if err != nil {
77+
return ServerResponse{}, err
78+
}
79+
80+
return node.Server()
81+
}
82+
7383
// SetMaxIdleConns sets the maximum number of connections in the idle
7484
// connection pool.
7585
func (c *Cluster) SetMaxIdleConns(n int) {

connection.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *Connection) Query(q Query) (*Response, *Cursor, error) {
115115
}
116116

117117
// Add token if query is a START/NOREPLY_WAIT
118-
if q.Type == p.Query_START || q.Type == p.Query_NOREPLY_WAIT {
118+
if q.Type == p.Query_START || q.Type == p.Query_NOREPLY_WAIT || q.Type == p.Query_SERVER_INFO {
119119
q.Token = c.nextToken()
120120
if c.opts.Database != "" {
121121
var err error
@@ -154,6 +154,33 @@ func (c *Connection) Query(q Query) (*Response, *Cursor, error) {
154154
}
155155
}
156156

157+
type ServerResponse struct {
158+
ID string `gorethink:"id"`
159+
Name string `gorethink:"name"`
160+
}
161+
162+
// Server returns the server name and server UUID being used by a connection.
163+
func (c *Connection) Server() (ServerResponse, error) {
164+
var response ServerResponse
165+
166+
_, cur, err := c.Query(Query{
167+
Type: p.Query_SERVER_INFO,
168+
})
169+
if err != nil {
170+
return response, err
171+
}
172+
173+
if err = cur.One(&response); err != nil {
174+
return response, err
175+
}
176+
177+
if err = cur.Close(); err != nil {
178+
return response, err
179+
}
180+
181+
return response, nil
182+
}
183+
157184
// sendQuery marshals the Query and sends the JSON to the server.
158185
func (c *Connection) sendQuery(q Query) error {
159186
// Build query
@@ -231,7 +258,7 @@ func (c *Connection) processResponse(q Query, response *Response) (*Response, *C
231258
return c.processErrorResponse(q, response, RQLCompileError{rqlResponseError{response, q.Term}})
232259
case p.Response_RUNTIME_ERROR:
233260
return c.processErrorResponse(q, response, RQLRuntimeError{rqlResponseError{response, q.Term}})
234-
case p.Response_SUCCESS_ATOM:
261+
case p.Response_SUCCESS_ATOM, p.Response_SERVER_INFO:
235262
return c.processAtomResponse(q, response)
236263
case p.Response_SUCCESS_PARTIAL:
237264
return c.processPartialResponse(q, response)

node.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ func (n *Node) Exec(q Query) (err error) {
140140
return err
141141
}
142142

143+
// Server returns the server name and server UUID being used by a connection.
144+
func (n *Node) Server() (ServerResponse, error) {
145+
var response ServerResponse
146+
var err error
147+
148+
if n.Closed() {
149+
return response, ErrInvalidNode
150+
}
151+
152+
response, err = n.pool.Server()
153+
if err != nil {
154+
n.DecrementHealth()
155+
}
156+
157+
return response, err
158+
}
159+
143160
// Refresh attempts to connect to the node and check that it is still connected
144161
// to the cluster.
145162
//

pool.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,38 @@ func (p *Pool) queryConn(pc *poolConn, releaseConn func(error), q Query) (*Curso
531531
return cursor, nil
532532
}
533533

534+
// Server returns the server name and server UUID being used by a connection.
535+
func (p *Pool) Server() (ServerResponse, error) {
536+
var response ServerResponse
537+
var err error
538+
539+
for i := 0; i < maxBadConnRetries; i++ {
540+
response, err = p.server()
541+
if err != ErrBadConn {
542+
break
543+
}
544+
}
545+
return response, err
546+
}
547+
func (p *Pool) server() (ServerResponse, error) {
548+
var response ServerResponse
549+
var err error
550+
551+
pc, err := p.conn()
552+
if err != nil {
553+
return response, err
554+
}
555+
defer func() {
556+
p.putConn(pc, err)
557+
}()
558+
559+
pc.Lock()
560+
response, err = pc.ci.Server()
561+
pc.Unlock()
562+
563+
return response, err
564+
}
565+
534566
// Helper functions
535567

536568
func stack() string {

session.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ func (s *Session) Exec(q Query) error {
226226
return s.cluster.Exec(q)
227227
}
228228

229+
// Server returns the server name and server UUID being used by a connection.
230+
func (s *Session) Server() (ServerResponse, error) {
231+
return s.cluster.Server()
232+
}
233+
229234
// SetHosts resets the hosts used when connecting to the RethinkDB cluster
230235
func (s *Session) SetHosts(hosts []Host) {
231236
s.mu.Lock()

session_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ func (s *RethinkSuite) TestSessionClose(c *test.C) {
7575
c.Assert(err, test.NotNil)
7676
}
7777

78+
func (s *RethinkSuite) TestSessionServer(c *test.C) {
79+
session, err := Connect(ConnectOpts{
80+
Address: url,
81+
AuthKey: os.Getenv("RETHINKDB_AUTHKEY"),
82+
})
83+
c.Assert(err, test.IsNil)
84+
85+
server, err := session.Server()
86+
c.Assert(err, test.IsNil)
87+
88+
c.Assert(len(server.ID) > 0, test.Equals, true)
89+
c.Assert(len(server.Name) > 0, test.Equals, true)
90+
}
91+
7892
func (s *RethinkSuite) TestSessionConnectDatabase(c *test.C) {
7993
session, err := Connect(ConnectOpts{
8094
Address: url,

0 commit comments

Comments
 (0)