Skip to content

Commit a507478

Browse files
authored
refactor: replace logrus and cleanup dead code (#508)
1 parent cd20432 commit a507478

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+220
-307
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Thanks to everybody who got involved with this project over the last ~4 years an
110110
### Changed
111111

112112
- Moved project to `gorethink` organisation
113-
- Fixed behaviour when unmarshaling nil slices
113+
- Fixed behaviour when unmarshalling nil slices
114114

115115
### Fixed
116116

@@ -356,7 +356,7 @@ r.Connect(
356356
- Added more documentation and examples for `GetAll`.
357357

358358
### Fixed
359-
- Fixed `RunWrite` not defering its call to `Cursor.Close()`. This could cause issues if an error occurred when decoding the result.
359+
- Fixed `RunWrite` not deferring its call to `Cursor.Close()`. This could cause issues if an error occurred when decoding the result.
360360
- Fixed panic when calling `Error()` on a RethinkDB-go `rqlError`.
361361

362362
## v1.3.0 - 2016-01-11
@@ -557,7 +557,7 @@ For more details checkout the [README](https://github.com/gorethink/gorethink/bl
557557
## v0.6.1 - 2015-02-13
558558

559559
- Reduce GC by using buffers when reading and writing
560-
- Fixed encoding `time.Time` ignoring millseconds
560+
- Fixed encoding `time.Time` ignoring milliseconds
561561
- Fixed pointers in structs that implement the `Marshaler`/`Unmarshaler` interfaces being ignored
562562

563563
## v0.6.0 - 2015-01-01

cluster.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ package rethinkdb
33
import (
44
"errors"
55
"fmt"
6+
"log/slog"
67
"sort"
7-
"strings"
88
"sync"
99
"sync/atomic"
1010
"time"
1111

12+
"context"
13+
14+
"github.com/cenkalti/backoff/v4"
1215
"github.com/hailocab/go-hostpool"
13-
"github.com/sirupsen/logrus"
14-
"golang.org/x/net/context"
15-
"gopkg.in/cenkalti/backoff.v2"
1616
)
1717

1818
var errClusterClosed = errors.New("rethinkdb: cluster is closed")
@@ -31,6 +31,7 @@ const (
3131
// This should hopefully soon be replaced by a backoff system.
3232
type Cluster struct {
3333
opts *ConnectOpts
34+
log *slog.Logger
3435

3536
mu sync.RWMutex
3637
seeds []Host // Initial host nodes specified by user.
@@ -45,12 +46,18 @@ type Cluster struct {
4546

4647
// NewCluster creates a new cluster by connecting to the given hosts.
4748
func NewCluster(hosts []Host, opts *ConnectOpts) (*Cluster, error) {
49+
log := slog.Default()
50+
if opts != nil && opts.Log != nil {
51+
log = opts.Log
52+
}
53+
4854
c := &Cluster{
4955
hp: newHostPool(opts),
5056
seeds: hosts,
5157
opts: opts,
5258
closed: clusterWorking,
5359
connFactory: NewConnection,
60+
log: log,
5461
}
5562

5663
err := c.run()
@@ -217,7 +224,7 @@ func (c *Cluster) discover() {
217224

218225
return c.listenForNodeChanges()
219226
}, b, func(err error, wait time.Duration) {
220-
Log.Debugf("Error discovering hosts %s, waiting: %s", err, wait)
227+
c.log.Debug("Error discovering hosts", "waiting", wait, "error", err)
221228
})
222229
}
223230
}
@@ -237,7 +244,7 @@ func (c *Cluster) listenForNodeChanges() error {
237244
c.opts,
238245
)
239246
if err != nil {
240-
return fmt.Errorf("Error building query: %s", err)
247+
return fmt.Errorf("Error building query: %w", err)
241248
}
242249

243250
cursor, err := node.Query(context.Background(), q) // no need for timeout due to Changes()
@@ -253,9 +260,6 @@ func (c *Cluster) listenForNodeChanges() error {
253260
OldVal *nodeStatus `rethinkdb:"old_val"`
254261
}
255262
for cursor.Next(&result) {
256-
addr := fmt.Sprintf("%s:%d", result.NewVal.Network.Hostname, result.NewVal.Network.ReqlPort)
257-
addr = strings.ToLower(addr)
258-
259263
if result.NewVal != nil && result.OldVal == nil {
260264
// added new node
261265
if !c.nodeExists(result.NewVal.ID) {
@@ -268,11 +272,7 @@ func (c *Cluster) listenForNodeChanges() error {
268272
node, err := c.connectNodeWithStatus(result.NewVal)
269273
if err == nil {
270274
c.addNode(node)
271-
272-
Log.WithFields(logrus.Fields{
273-
"id": node.ID,
274-
"host": node.Host.String(),
275-
}).Debug("Connected to node")
275+
c.log.Debug("Connected to node", "id", node.ID, "host", node.Host.String())
276276
}
277277
return err
278278
}, b)
@@ -306,14 +306,14 @@ func (c *Cluster) connectCluster() error {
306306
conn, err := c.connFactory(host.String(), c.opts)
307307
if err != nil {
308308
attemptErr = err
309-
Log.Warnf("Error creating connection: %s", err.Error())
309+
c.log.Warn("Error creating connection", "error", err)
310310
continue
311311
}
312312

313313
svrRsp, err := conn.Server()
314314
if err != nil {
315315
attemptErr = err
316-
Log.Warnf("Error fetching server ID: %s", err)
316+
c.log.Warn("Error fetching server ID", "error", err)
317317
_ = conn.Close()
318318

319319
continue
@@ -323,19 +323,15 @@ func (c *Cluster) connectCluster() error {
323323
node, err := c.connectNode(svrRsp.ID, []Host{host})
324324
if err != nil {
325325
attemptErr = err
326-
Log.Warnf("Error connecting to node: %s", err)
326+
c.log.Warn("Error connecting to node", "error", err)
327327
continue
328328
}
329329

330330
if _, ok := nodeSet[node.ID]; !ok {
331-
Log.WithFields(logrus.Fields{
332-
"id": node.ID,
333-
"host": node.Host.String(),
334-
}).Debug("Connected to node")
335-
331+
c.log.Debug("Connected to node", "id", node.ID, "host", node.Host.String())
336332
nodeSet[node.ID] = node
337333
} else {
338-
// dublicate node
334+
// duplicate node
339335
_ = node.Close()
340336
}
341337
}

cluster_test.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"encoding/binary"
66
"encoding/json"
77
"fmt"
8+
"io"
9+
"log/slog"
10+
"net"
11+
"time"
12+
813
"github.com/stretchr/testify/mock"
914
test "gopkg.in/check.v1"
1015
"gopkg.in/rethinkdb/rethinkdb-go.v6/encoding"
1116
p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2"
12-
"io"
13-
"net"
14-
"time"
1517
)
1618

1719
type ClusterSuite struct{}
@@ -40,6 +42,7 @@ func (s *ClusterSuite) TestCluster_NewSingle_NoDiscover_Ok(c *test.C) {
4042
opts: opts,
4143
closed: clusterWorking,
4244
connFactory: mockedConnectionFactory(dialMock),
45+
log: slog.Default(),
4346
}
4447

4548
err := cluster.run()
@@ -84,6 +87,7 @@ func (s *ClusterSuite) TestCluster_NewMultiple_NoDiscover_Ok(c *test.C) {
8487
opts: opts,
8588
closed: clusterWorking,
8689
connFactory: mockedConnectionFactory(dialMock),
90+
log: slog.Default(),
8791
}
8892

8993
err := cluster.run()
@@ -115,6 +119,7 @@ func (s *ClusterSuite) TestCluster_NewSingle_NoDiscover_DialFail(c *test.C) {
115119
opts: opts,
116120
closed: clusterWorking,
117121
connFactory: mockedConnectionFactory(dialMock),
122+
log: slog.Default(),
118123
}
119124

120125
err := cluster.run()
@@ -146,6 +151,7 @@ func (s *ClusterSuite) TestCluster_NewMultiple_NoDiscover_DialHalfFail(c *test.C
146151
opts: opts,
147152
closed: clusterWorking,
148153
connFactory: mockedConnectionFactory(dialMock),
154+
log: slog.Default(),
149155
}
150156

151157
err := cluster.run()
@@ -175,6 +181,7 @@ func (s *ClusterSuite) TestCluster_NewMultiple_NoDiscover_DialFail(c *test.C) {
175181
opts: opts,
176182
closed: clusterWorking,
177183
connFactory: mockedConnectionFactory(dialMock),
184+
log: slog.Default(),
178185
}
179186

180187
err := cluster.run()
@@ -200,6 +207,7 @@ func (s *ClusterSuite) TestCluster_NewSingle_NoDiscover_ServerFail(c *test.C) {
200207
opts: opts,
201208
closed: clusterWorking,
202209
connFactory: mockedConnectionFactory(dialMock),
210+
log: slog.Default(),
203211
}
204212

205213
err := cluster.run()
@@ -233,6 +241,7 @@ func (s *ClusterSuite) TestCluster_NewSingle_NoDiscover_PingFail(c *test.C) {
233241
opts: opts,
234242
closed: clusterWorking,
235243
connFactory: mockedConnectionFactory(dialMock),
244+
log: slog.Default(),
236245
}
237246

238247
err := cluster.run()
@@ -275,6 +284,7 @@ func (s *ClusterSuite) TestCluster_NewSingle_Discover_Ok(c *test.C) {
275284
closed: clusterWorking,
276285
connFactory: mockedConnectionFactory(dialMock),
277286
discoverInterval: 10 * time.Second,
287+
log: slog.Default(),
278288
}
279289

280290
err := cluster.run()
@@ -333,6 +343,7 @@ func (s *ClusterSuite) TestCluster_NewMultiple_Discover_Ok(c *test.C) {
333343
closed: clusterWorking,
334344
connFactory: mockedConnectionFactory(dialMock),
335345
discoverInterval: 10 * time.Second,
346+
log: slog.Default(),
336347
}
337348

338349
err := cluster.run()
@@ -487,7 +498,7 @@ func expectServerStatus(conn *connMock, token int64, nodeIDs []string, hosts []H
487498
})
488499

489500
rawQ2 := makeContinueQueryRaw(token)
490-
// maybe - connection may be closed until cursor fetchs next batch
501+
// maybe - connection may be closed until cursor fetches next batch
491502
conn.On("Write", rawQ2).Return(0, nil, nil).Maybe().Run(func(args mock.Arguments) {
492503
<-readRChan
493504
})

connection.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010
"time"
1111

1212
"bytes"
13+
"context"
14+
"sync"
15+
1316
"github.com/opentracing/opentracing-go"
1417
"github.com/opentracing/opentracing-go/ext"
1518
"github.com/opentracing/opentracing-go/log"
16-
"golang.org/x/net/context"
1719
p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2"
18-
"sync"
1920
)
2021

2122
const (
@@ -163,7 +164,7 @@ func (c *Connection) Query(ctx context.Context, q Query) (*Response, *Cursor, er
163164
c.setBad()
164165
return nil, nil, ErrConnectionClosed
165166
}
166-
if ctx == nil {
167+
if ctx == nil || ctx == context.TODO() {
167168
ctx = c.contextFromConnectionOpts()
168169
}
169170

@@ -280,7 +281,7 @@ func (c *Connection) processResponses() {
280281
var ok bool
281282

282283
select {
283-
case respPair, openned := <-c.responseChan:
284+
case respPair, opened := <-c.responseChan:
284285
if respPair.err != nil {
285286
// Transport socket error, can't continue to work
286287
// Don't know return to who (no token) - return to all
@@ -289,7 +290,7 @@ func (c *Connection) processResponses() {
289290
_ = c.Close() // next `if` will be called indirect cascade by closing chans
290291
continue
291292
}
292-
if !openned { // responseChan is connClosed (stopReadChan is closed too)
293+
if !opened { // responseChan is connClosed (stopReadChan is closed too)
293294
close(c.stopProcessingChan)
294295
broadcastError(readRequests, ErrConnectionClosed)
295296
c.cursors = nil

connection_handshake.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func (c *connectionHandshakeV0_4) writeHandshakeReq() error {
8080

8181
// Send the protocol type as a 4-byte little-endian-encoded integer
8282
binary.LittleEndian.PutUint32(data[pos:], uint32(p.VersionDummy_JSON))
83-
pos += 4
8483

8584
return c.conn.writeData(data)
8685
}
@@ -90,7 +89,7 @@ func (c *connectionHandshakeV0_4) readHandshakeSuccess() error {
9089
line, err := reader.ReadBytes('\x00')
9190
if err != nil {
9291
if err == io.EOF {
93-
return fmt.Errorf("Unexpected EOF: %s", string(line))
92+
return fmt.Errorf("unexpected EOF: %s", string(line))
9493
}
9594
return err
9695
}

connection_helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package rethinkdb
22

33
import (
4-
"golang.org/x/net/context"
4+
"context"
55
"io"
66
)
77

connection_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package rethinkdb
22

33
import (
4+
"context"
45
"encoding/binary"
56
"encoding/json"
7+
"io"
8+
"sync"
9+
"time"
10+
611
"github.com/opentracing/opentracing-go"
712
"github.com/opentracing/opentracing-go/mocktracer"
813
"github.com/stretchr/testify/mock"
9-
"golang.org/x/net/context"
1014
test "gopkg.in/check.v1"
1115
p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2"
12-
"io"
13-
"sync"
14-
"time"
1516
)
1617

1718
func runConnection(c *Connection) <-chan struct{} {
@@ -109,15 +110,15 @@ func (s *ConnectionSuite) TestConnection_Query_DefaultDBOk(c *test.C) {
109110
}
110111

111112
func (s *ConnectionSuite) TestConnection_Query_Nil(c *test.C) {
112-
response, cursor, err := (*Connection)(nil).Query(nil, Query{})
113+
response, cursor, err := (*Connection)(nil).Query(context.TODO(), Query{})
113114
c.Assert(err, test.Equals, ErrConnectionClosed)
114115
c.Assert(response, test.IsNil)
115116
c.Assert(cursor, test.IsNil)
116117
}
117118

118119
func (s *ConnectionSuite) TestConnection_Query_NilConn(c *test.C) {
119120
connection := newConnection(nil, "addr", &ConnectOpts{Database: "db"})
120-
response, cursor, err := connection.Query(nil, Query{})
121+
response, cursor, err := connection.Query(context.TODO(), Query{})
121122
c.Assert(err, test.Equals, ErrConnectionClosed)
122123
c.Assert(response, test.IsNil)
123124
c.Assert(cursor, test.IsNil)
@@ -157,7 +158,7 @@ func (s *ConnectionSuite) TestConnection_Query_NoReplyOk(c *test.C) {
157158

158159
connection := newConnection(conn, "addr", &ConnectOpts{})
159160
done := runConnection(connection)
160-
response, cursor, err := connection.Query(nil, q)
161+
response, cursor, err := connection.Query(context.TODO(), q)
161162
connection.Close()
162163
<-done
163164

0 commit comments

Comments
 (0)