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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 9 additions & 6 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 11 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)