Skip to content

Commit 6bd1dd1

Browse files
committed
code health: fix all places highlighted by linter
Removed error's suppression in check.yaml. Suppressed the highlighting of errorcheck in all methods, having encodeXxx inside. For now these methods are not able to return any error cause of internal implementation of writer interface. For future, if the implementation of writer will be changed, and we will have to check errors, we must think about how to say to compiler that the error check is 'unlikely'. Fixed the use of time package API, all places with calls of time.Now().Sub(). Now time package propose the explicit time.Until(). Replaced some calls of Errorf() with Fatalf() in tests. That change prevents nil dereferences below in the code. Suppressed the highlighting of all unused constants and functions. Fixed calling of Fatalf from non-testing goroutine in queue tests. It is not a valid way to stop test from another goroutine. Relates to #142
1 parent 1d06618 commit 6bd1dd1

16 files changed

+95
-35
lines changed

.github/workflows/check.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ jobs:
3939
- name: golangci-lint
4040
uses: golangci/golangci-lint-action@v2
4141
with:
42-
args: --issues-exit-code=0 -E gofmt
42+
args: -E gofmt

client_tools.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type IntKey struct {
1010
I int
1111
}
1212

13+
//nolint: errcheck
1314
func (k IntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
1415
enc.EncodeSliceLen(1)
1516
enc.EncodeInt(k.I)
@@ -22,6 +23,7 @@ type UintKey struct {
2223
I uint
2324
}
2425

26+
//nolint: errcheck
2527
func (k UintKey) EncodeMsgpack(enc *msgpack.Encoder) error {
2628
enc.EncodeSliceLen(1)
2729
enc.EncodeUint(k.I)
@@ -34,6 +36,7 @@ type StringKey struct {
3436
S string
3537
}
3638

39+
//nolint: errcheck
3740
func (k StringKey) EncodeMsgpack(enc *msgpack.Encoder) error {
3841
enc.EncodeSliceLen(1)
3942
enc.EncodeString(k.S)
@@ -46,6 +49,7 @@ type IntIntKey struct {
4649
I1, I2 int
4750
}
4851

52+
//nolint: errcheck
4953
func (k IntIntKey) EncodeMsgpack(enc *msgpack.Encoder) error {
5054
enc.EncodeSliceLen(2)
5155
enc.EncodeInt(k.I1)
@@ -60,6 +64,7 @@ type Op struct {
6064
Arg interface{}
6165
}
6266

67+
//nolint: errcheck
6368
func (o Op) EncodeMsgpack(enc *msgpack.Encoder) error {
6469
enc.EncodeSliceLen(3)
6570
enc.EncodeString(o.Op)
@@ -75,6 +80,7 @@ type OpSplice struct {
7580
Replace string
7681
}
7782

83+
//nolint: errcheck
7884
func (o OpSplice) EncodeMsgpack(enc *msgpack.Encoder) error {
7985
enc.EncodeSliceLen(5)
8086
enc.EncodeString(o.Op)

connection.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ type connShard struct {
147147
bufmut sync.Mutex
148148
buf smallWBuf
149149
enc *msgpack.Encoder
150-
_pad [16]uint64
150+
_pad [16]uint64 //nolint: structcheck,unused
151151
}
152152

153153
// Greeting is a message sent by Tarantool on connect.
@@ -228,6 +228,7 @@ type Opts struct {
228228
// - If opts.Reconnect is non-zero, then error will be returned only if authorization
229229
// fails. But if Tarantool is not reachable, then it will make an attempt to reconnect later
230230
// and will not finish to make attempts on authorization failures.
231+
//nolint: errcheck
231232
func Connect(addr string, opts Opts) (conn *Connection, err error) {
232233
conn = &Connection{
233234
addr: addr,
@@ -495,7 +496,7 @@ func (conn *Connection) createConnection(reconnect bool) (err error) {
495496
conn.notify(ReconnectFailed)
496497
reconnects++
497498
conn.mutex.Unlock()
498-
time.Sleep(now.Add(conn.opts.Reconnect).Sub(time.Now()))
499+
time.Sleep(time.Until(now.Add(conn.opts.Reconnect)))
499500
conn.mutex.Lock()
500501
}
501502
if conn.state == connClosed {
@@ -538,6 +539,7 @@ func (conn *Connection) closeConnection(neterr error, forever bool) (err error)
538539
return
539540
}
540541

542+
//nolint: errcheck
541543
func (conn *Connection) reconnect(neterr error, c net.Conn) {
542544
conn.mutex.Lock()
543545
defer conn.mutex.Unlock()
@@ -567,6 +569,7 @@ func (conn *Connection) unlockShards() {
567569
}
568570
}
569571

572+
//nolint: errcheck
570573
func (conn *Connection) pinger() {
571574
to := conn.opts.Timeout
572575
if to == 0 {
@@ -688,7 +691,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
688691
*pair.last = fut
689692
pair.last = &fut.next
690693
if conn.opts.Timeout > 0 {
691-
fut.timeout = time.Now().Sub(epoch) + conn.opts.Timeout
694+
fut.timeout = time.Until(epoch) + conn.opts.Timeout
692695
}
693696
shard.rmut.Unlock()
694697
if conn.rlimit != nil && conn.opts.RLimitAction == RLimitWait {
@@ -796,9 +799,9 @@ func (conn *Connection) timeouts() {
796799
return
797800
case <-t.C:
798801
}
799-
minNext := time.Now().Sub(epoch) + timeout
802+
minNext := time.Until(epoch) + timeout
800803
for i := range conn.shard {
801-
nowepoch = time.Now().Sub(epoch)
804+
nowepoch = time.Until(epoch)
802805
shard := &conn.shard[i]
803806
for pos := range shard.requests {
804807
shard.rmut.Lock()
@@ -825,7 +828,7 @@ func (conn *Connection) timeouts() {
825828
shard.rmut.Unlock()
826829
}
827830
}
828-
nowepoch = time.Now().Sub(epoch)
831+
nowepoch = time.Until(epoch)
829832
if nowepoch+time.Microsecond < minNext {
830833
t.Reset(minNext - nowepoch)
831834
} else {

deadline_io.go

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type DeadlineIO struct {
1010
c net.Conn
1111
}
1212

13+
//nolint: errcheck
1314
func (d *DeadlineIO) Write(b []byte) (n int, err error) {
1415
if d.to > 0 {
1516
d.c.SetWriteDeadline(time.Now().Add(d.to))
@@ -18,6 +19,7 @@ func (d *DeadlineIO) Write(b []byte) (n int, err error) {
1819
return
1920
}
2021

22+
//nolint: errcheck
2123
func (d *DeadlineIO) Read(b []byte) (n int, err error) {
2224
if d.to > 0 {
2325
d.c.SetReadDeadline(time.Now().Add(d.to))

example_custom_unpacking_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ type Tuple2 struct {
1616

1717
// Same effect in a "magic" way, but slower.
1818
type Tuple3 struct {
19-
_msgpack struct{} `msgpack:",asArray"`
19+
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
2020

2121
Cid uint
2222
Orig string
2323
Members []Member
2424
}
2525

26+
//nolint: errcheck
2627
func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
2728
if err := e.EncodeSliceLen(3); err != nil {
2829
return err
@@ -37,6 +38,7 @@ func (c *Tuple2) EncodeMsgpack(e *msgpack.Encoder) error {
3738
return nil
3839
}
3940

41+
//nolint: errcheck
4042
func (c *Tuple2) DecodeMsgpack(d *msgpack.Decoder) error {
4143
var err error
4244
var l int

example_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
type Tuple struct {
1111
// Instruct msgpack to pack this struct as array, so no custom packer
1212
// is needed.
13-
_msgpack struct{} `msgpack:",asArray"`
13+
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
1414
Id uint
1515
Msg string
1616
Name string
@@ -24,6 +24,7 @@ func example_connect() *tarantool.Connection {
2424
return conn
2525
}
2626

27+
//nolint: errcheck
2728
func ExampleConnection_Select() {
2829
conn := example_connect()
2930
defer conn.Close()
@@ -70,6 +71,7 @@ func ExampleConnection_SelectTyped() {
7071
// response is [{{} 1111 hello world}]
7172
}
7273

74+
//nolint
7375
func ExampleConnection_SelectAsync() {
7476
conn := example_connect()
7577
defer conn.Close()
@@ -118,6 +120,7 @@ func ExampleConnection_Ping() {
118120
// Ping Error <nil>
119121
}
120122

123+
//nolint: errcheck
121124
func ExampleConnection_Insert() {
122125
conn := example_connect()
123126
defer conn.Close()
@@ -151,6 +154,7 @@ func ExampleConnection_Insert() {
151154

152155
}
153156

157+
//nolint: errcheck
154158
func ExampleConnection_Delete() {
155159
conn := example_connect()
156160
defer conn.Close()
@@ -184,6 +188,7 @@ func ExampleConnection_Delete() {
184188
// Data [[36 test one]]
185189
}
186190

191+
//nolint: errcheck
187192
func ExampleConnection_Replace() {
188193
conn := example_connect()
189194
defer conn.Close()
@@ -233,6 +238,7 @@ func ExampleConnection_Replace() {
233238
// Data [[13 test twelve]]
234239
}
235240

241+
//nolint: errcheck
236242
func ExampleConnection_Update() {
237243
conn := example_connect()
238244
defer conn.Close()

queue/example_msgpack_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (c *dummyData) EncodeMsgpack(e *msgpack.Encoder) error {
4646
// - If you use the connection timeout and call Take, we return an error if we
4747
// cannot take the task out of the queue within the time corresponding to the
4848
// connection timeout.
49+
//nolint: errcheck,ineffassign
4950
func Example_simpleQueueCustomMsgPack() {
5051
opts := tarantool.Opts{
5152
Reconnect: time.Second,

queue/example_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/tarantool/go-tarantool/queue"
1717
)
1818

19+
//nolint
1920
// Example demonstrates an operations like Put and Take with queue.
2021
func Example_simpleQueue() {
2122
cfg := queue.Cfg{

queue/queue.go

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ type queueData struct {
339339
result interface{}
340340
}
341341

342+
//nolint
342343
func (qd *queueData) DecodeMsgpack(d *msgpack.Decoder) error {
343344
var err error
344345
var l int

queue/queue_test.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -794,16 +794,22 @@ func TestUtube_Put(t *testing.T) {
794794
t.Fatalf("Failed put task to queue: %s", err.Error())
795795
}
796796

797+
errChan := make(chan struct{})
797798
go func() {
798799
t1, err := q.TakeTimeout(2 * time.Second)
799800
if err != nil {
800-
t.Fatalf("Failed to take task from utube: %s", err.Error())
801+
t.Errorf("Failed to take task from utube: %s", err.Error())
802+
errChan <- struct{}{}
803+
return
801804
}
802805

803806
time.Sleep(2 * time.Second)
804807
if err := t1.Ack(); err != nil {
805-
t.Fatalf("Failed to ack task: %s", err.Error())
808+
t.Errorf("Failed to ack task: %s", err.Error())
809+
errChan <- struct{}{}
810+
return
806811
}
812+
close(errChan)
807813
}()
808814

809815
time.Sleep(100 * time.Millisecond)
@@ -817,6 +823,9 @@ func TestUtube_Put(t *testing.T) {
817823
t.Fatalf("Failed to ack task: %s", err.Error())
818824
}
819825
end := time.Now()
826+
if _, ok := <-errChan; ok {
827+
t.Fatalf("One of take failed")
828+
}
820829
if math.Abs(float64(end.Sub(start)-2*time.Second)) > float64(200*time.Millisecond) {
821830
t.Fatalf("Blocking time is less than expected: actual = %.2fs, expected = 1s", end.Sub(start).Seconds())
822831
}

request.go

+14
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ type Future struct {
1919
}
2020

2121
// Ping sends empty request to Tarantool to check connection.
22+
//nolint: errcheck
2223
func (conn *Connection) Ping() (resp *Response, err error) {
2324
future := conn.newFuture(PingRequest)
2425
return future.send(conn, func(enc *msgpack.Encoder) error { enc.EncodeMapLen(0); return nil }).Get()
2526
}
2627

28+
//nolint: errcheck
2729
func (req *Future) fillSearch(enc *msgpack.Encoder, spaceNo, indexNo uint32, key interface{}) error {
2830
enc.EncodeUint64(KeySpaceNo)
2931
enc.EncodeUint64(uint64(spaceNo))
@@ -33,6 +35,7 @@ func (req *Future) fillSearch(enc *msgpack.Encoder, spaceNo, indexNo uint32, key
3335
return enc.Encode(key)
3436
}
3537

38+
//nolint: errcheck
3639
func (req *Future) fillIterator(enc *msgpack.Encoder, offset, limit, iterator uint32) {
3740
enc.EncodeUint64(KeyIterator)
3841
enc.EncodeUint64(uint64(iterator))
@@ -42,6 +45,7 @@ func (req *Future) fillIterator(enc *msgpack.Encoder, offset, limit, iterator ui
4245
enc.EncodeUint64(uint64(limit))
4346
}
4447

48+
//nolint: errcheck
4549
func (req *Future) fillInsert(enc *msgpack.Encoder, spaceNo uint32, tuple interface{}) error {
4650
enc.EncodeUint64(KeySpaceNo)
4751
enc.EncodeUint64(uint64(spaceNo))
@@ -213,6 +217,7 @@ func (conn *Connection) EvalTyped(expr string, args interface{}, result interfac
213217
}
214218

215219
// SelectAsync sends select request to Tarantool and returns Future.
220+
//nolint: errcheck
216221
func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, iterator uint32, key interface{}) *Future {
217222
future := conn.newFuture(SelectRequest)
218223
spaceNo, indexNo, err := conn.Schema.resolveSpaceIndex(space, index)
@@ -228,6 +233,7 @@ func (conn *Connection) SelectAsync(space, index interface{}, offset, limit, ite
228233

229234
// InsertAsync sends insert action to Tarantool and returns Future.
230235
// Tarantool will reject Insert when tuple with same primary key exists.
236+
//nolint: errcheck
231237
func (conn *Connection) InsertAsync(space interface{}, tuple interface{}) *Future {
232238
future := conn.newFuture(InsertRequest)
233239
spaceNo, _, err := conn.Schema.resolveSpaceIndex(space, nil)
@@ -242,6 +248,7 @@ func (conn *Connection) InsertAsync(space interface{}, tuple interface{}) *Futur
242248

243249
// ReplaceAsync sends "insert or replace" action to Tarantool and returns Future.
244250
// If tuple with same primary key exists, it will be replaced.
251+
//nolint: errcheck
245252
func (conn *Connection) ReplaceAsync(space interface{}, tuple interface{}) *Future {
246253
future := conn.newFuture(ReplaceRequest)
247254
spaceNo, _, err := conn.Schema.resolveSpaceIndex(space, nil)
@@ -256,6 +263,7 @@ func (conn *Connection) ReplaceAsync(space interface{}, tuple interface{}) *Futu
256263

257264
// DeleteAsync sends deletion action to Tarantool and returns Future.
258265
// Future's result will contain array with deleted tuple.
266+
//nolint: errcheck
259267
func (conn *Connection) DeleteAsync(space, index interface{}, key interface{}) *Future {
260268
future := conn.newFuture(DeleteRequest)
261269
spaceNo, indexNo, err := conn.Schema.resolveSpaceIndex(space, index)
@@ -270,6 +278,7 @@ func (conn *Connection) DeleteAsync(space, index interface{}, key interface{}) *
270278

271279
// Update sends deletion of a tuple by key and returns Future.
272280
// Future's result will contain array with updated tuple.
281+
//nolint: errcheck
273282
func (conn *Connection) UpdateAsync(space, index interface{}, key, ops interface{}) *Future {
274283
future := conn.newFuture(UpdateRequest)
275284
spaceNo, indexNo, err := conn.Schema.resolveSpaceIndex(space, index)
@@ -288,6 +297,7 @@ func (conn *Connection) UpdateAsync(space, index interface{}, key, ops interface
288297

289298
// UpsertAsync sends "update or insert" action to Tarantool and returns Future.
290299
// Future's sesult will not contain any tuple.
300+
//nolint: errcheck
291301
func (conn *Connection) UpsertAsync(space interface{}, tuple interface{}, ops interface{}) *Future {
292302
future := conn.newFuture(UpsertRequest)
293303
spaceNo, _, err := conn.Schema.resolveSpaceIndex(space, nil)
@@ -309,6 +319,7 @@ func (conn *Connection) UpsertAsync(space interface{}, tuple interface{}, ops in
309319

310320
// CallAsync sends a call to registered Tarantool function and returns Future.
311321
// It uses request code for Tarantool 1.6, so future's result is always array of arrays
322+
//nolint: errcheck
312323
func (conn *Connection) CallAsync(functionName string, args interface{}) *Future {
313324
future := conn.newFuture(CallRequest)
314325
return future.send(conn, func(enc *msgpack.Encoder) error {
@@ -323,6 +334,7 @@ func (conn *Connection) CallAsync(functionName string, args interface{}) *Future
323334
// Call17Async sends a call to registered Tarantool function and returns Future.
324335
// It uses request code for Tarantool 1.7, so future's result will not be converted
325336
// (though, keep in mind, result is always array)
337+
//nolint: errcheck
326338
func (conn *Connection) Call17Async(functionName string, args interface{}) *Future {
327339
future := conn.newFuture(Call17Request)
328340
return future.send(conn, func(enc *msgpack.Encoder) error {
@@ -335,6 +347,7 @@ func (conn *Connection) Call17Async(functionName string, args interface{}) *Futu
335347
}
336348

337349
// EvalAsync sends a Lua expression for evaluation and returns Future.
350+
//nolint: errcheck
338351
func (conn *Connection) EvalAsync(expr string, args interface{}) *Future {
339352
future := conn.newFuture(EvalRequest)
340353
return future.send(conn, func(enc *msgpack.Encoder) error {
@@ -350,6 +363,7 @@ func (conn *Connection) EvalAsync(expr string, args interface{}) *Future {
350363
// private
351364
//
352365

366+
//nolint: errcheck
353367
func (fut *Future) pack(h *smallWBuf, enc *msgpack.Encoder, body func(*msgpack.Encoder) error) (err error) {
354368
rid := fut.requestId
355369
hl := h.Len()

0 commit comments

Comments
 (0)