Skip to content

Commit 5820237

Browse files
committed
code health: fix all places highlighted by linter
Changed error's suppression in check.yaml. The suppression of rule `errcheck` may be removed after fixing errors check in all methods with calling encodeXxx inside. See details below. The rules `structcheck` and `unused` highlight the 'hack' with using _msgpack struct{} `msgpack:",asArray"`. The workaround with `//nolint` is not the best way to suppress it, cause this comment gets rendered by godoc and there is no way to make it invisible. For details see golang/go#20925. Suppressed the highlighting of error check in all methods, having encodeXxx inside. For now these methods are not able to return any error cause of internal implementation of writer interface (see smallbuf.go). For future, if the implementation of writer will be changed, and there will be a need 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 all calls of Errorf() with Fatalf() in tests. That change prevents nil dereferences below in the code and stops test function execution, where it is expected in tests. Suppressed the highlighting of all unused constants and functions (Rules structcheck,unused in golangci-lint). Fixed calling of Fatalf from non-testing goroutine in queue tests. It is not a valid way to stop test from another goroutine. Fixed gofmt-highlighted places in test_helpers/pool_helper.go and connection_pool/const.go. Closes #142 Closes #150
1 parent e9b9ba1 commit 5820237

17 files changed

+194
-164
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 -D errcheck,unused,structcheck

connection.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func (conn *Connection) createConnection(reconnect bool) (err error) {
495495
conn.notify(ReconnectFailed)
496496
reconnects++
497497
conn.mutex.Unlock()
498-
time.Sleep(now.Add(conn.opts.Reconnect).Sub(time.Now()))
498+
time.Sleep(time.Until(now.Add(conn.opts.Reconnect)))
499499
conn.mutex.Lock()
500500
}
501501
if conn.state == connClosed {
@@ -688,7 +688,7 @@ func (conn *Connection) newFuture(requestCode int32) (fut *Future) {
688688
*pair.last = fut
689689
pair.last = &fut.next
690690
if conn.opts.Timeout > 0 {
691-
fut.timeout = time.Now().Sub(epoch) + conn.opts.Timeout
691+
fut.timeout = time.Until(epoch) + conn.opts.Timeout
692692
}
693693
shard.rmut.Unlock()
694694
if conn.rlimit != nil && conn.opts.RLimitAction == RLimitWait {
@@ -796,9 +796,9 @@ func (conn *Connection) timeouts() {
796796
return
797797
case <-t.C:
798798
}
799-
minNext := time.Now().Sub(epoch) + timeout
799+
minNext := time.Until(epoch) + timeout
800800
for i := range conn.shard {
801-
nowepoch = time.Now().Sub(epoch)
801+
nowepoch = time.Until(epoch)
802802
shard := &conn.shard[i]
803803
for pos := range shard.requests {
804804
shard.rmut.Lock()
@@ -825,7 +825,7 @@ func (conn *Connection) timeouts() {
825825
shard.rmut.Unlock()
826826
}
827827
}
828-
nowepoch = time.Now().Sub(epoch)
828+
nowepoch = time.Until(epoch)
829829
if nowepoch+time.Microsecond < minNext {
830830
t.Reset(minNext - nowepoch)
831831
} else {

connection_pool/connection_pool_test.go

+67-67
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func TestConnSuccessfully(t *testing.T) {
6565
defer connPool.Close()
6666

6767
args := test_helpers.CheckStatusesArgs{
68-
ConnPool: connPool,
69-
Mode: connection_pool.ANY,
70-
Servers: []string{server},
68+
ConnPool: connPool,
69+
Mode: connection_pool.ANY,
70+
Servers: []string{server},
7171
ExpectedPoolStatus: true,
7272
ExpectedStatuses: map[string]bool{
7373
server: true,
@@ -90,9 +90,9 @@ func TestReconnect(t *testing.T) {
9090
test_helpers.StopTarantoolWithCleanup(instances[0])
9191

9292
args := test_helpers.CheckStatusesArgs{
93-
ConnPool: connPool,
94-
Mode: connection_pool.ANY,
95-
Servers: []string{server},
93+
ConnPool: connPool,
94+
Mode: connection_pool.ANY,
95+
Servers: []string{server},
9696
ExpectedPoolStatus: true,
9797
ExpectedStatuses: map[string]bool{
9898
server: false,
@@ -106,9 +106,9 @@ func TestReconnect(t *testing.T) {
106106
require.Nilf(t, err, "failed to restart tarantool")
107107

108108
args = test_helpers.CheckStatusesArgs{
109-
ConnPool: connPool,
110-
Mode: connection_pool.ANY,
111-
Servers: []string{server},
109+
ConnPool: connPool,
110+
Mode: connection_pool.ANY,
111+
Servers: []string{server},
112112
ExpectedPoolStatus: true,
113113
ExpectedStatuses: map[string]bool{
114114
server: true,
@@ -133,9 +133,9 @@ func TestDisconnectAll(t *testing.T) {
133133
test_helpers.StopTarantoolWithCleanup(instances[1])
134134

135135
args := test_helpers.CheckStatusesArgs{
136-
ConnPool: connPool,
137-
Mode: connection_pool.ANY,
138-
Servers: []string{server1, server2},
136+
ConnPool: connPool,
137+
Mode: connection_pool.ANY,
138+
Servers: []string{server1, server2},
139139
ExpectedPoolStatus: false,
140140
ExpectedStatuses: map[string]bool{
141141
server1: false,
@@ -153,9 +153,9 @@ func TestDisconnectAll(t *testing.T) {
153153
require.Nilf(t, err, "failed to restart tarantool")
154154

155155
args = test_helpers.CheckStatusesArgs{
156-
ConnPool: connPool,
157-
Mode: connection_pool.ANY,
158-
Servers: []string{server1, server2},
156+
ConnPool: connPool,
157+
Mode: connection_pool.ANY,
158+
Servers: []string{server1, server2},
159159
ExpectedPoolStatus: true,
160160
ExpectedStatuses: map[string]bool{
161161
server1: true,
@@ -176,9 +176,9 @@ func TestClose(t *testing.T) {
176176
require.NotNilf(t, connPool, "conn is nil after Connect")
177177

178178
args := test_helpers.CheckStatusesArgs{
179-
ConnPool: connPool,
180-
Mode: connection_pool.ANY,
181-
Servers: []string{server1, server2},
179+
ConnPool: connPool,
180+
Mode: connection_pool.ANY,
181+
Servers: []string{server1, server2},
182182
ExpectedPoolStatus: true,
183183
ExpectedStatuses: map[string]bool{
184184
server1: true,
@@ -192,9 +192,9 @@ func TestClose(t *testing.T) {
192192
connPool.Close()
193193

194194
args = test_helpers.CheckStatusesArgs{
195-
ConnPool: connPool,
196-
Mode: connection_pool.ANY,
197-
Servers: []string{server1, server2},
195+
ConnPool: connPool,
196+
Mode: connection_pool.ANY,
197+
Servers: []string{server1, server2},
198198
ExpectedPoolStatus: false,
199199
ExpectedStatuses: map[string]bool{
200200
server1: false,
@@ -353,8 +353,8 @@ func TestRoundRobinStrategy(t *testing.T) {
353353
args := test_helpers.ListenOnInstanceArgs{
354354
ServersNumber: serversNumber,
355355
ExpectedPorts: allPorts,
356-
ConnPool: connPool,
357-
Mode: connection_pool.ANY,
356+
ConnPool: connPool,
357+
Mode: connection_pool.ANY,
358358
}
359359

360360
err = test_helpers.ProcessListenOnInstance(args)
@@ -364,8 +364,8 @@ func TestRoundRobinStrategy(t *testing.T) {
364364
args = test_helpers.ListenOnInstanceArgs{
365365
ServersNumber: serversNumber,
366366
ExpectedPorts: masterPorts,
367-
ConnPool: connPool,
368-
Mode: connection_pool.RW,
367+
ConnPool: connPool,
368+
Mode: connection_pool.RW,
369369
}
370370

371371
err = test_helpers.ProcessListenOnInstance(args)
@@ -375,8 +375,8 @@ func TestRoundRobinStrategy(t *testing.T) {
375375
args = test_helpers.ListenOnInstanceArgs{
376376
ServersNumber: serversNumber,
377377
ExpectedPorts: replicaPorts,
378-
ConnPool: connPool,
379-
Mode: connection_pool.RO,
378+
ConnPool: connPool,
379+
Mode: connection_pool.RO,
380380
}
381381

382382
err = test_helpers.ProcessListenOnInstance(args)
@@ -386,8 +386,8 @@ func TestRoundRobinStrategy(t *testing.T) {
386386
args = test_helpers.ListenOnInstanceArgs{
387387
ServersNumber: serversNumber,
388388
ExpectedPorts: masterPorts,
389-
ConnPool: connPool,
390-
Mode: connection_pool.PreferRW,
389+
ConnPool: connPool,
390+
Mode: connection_pool.PreferRW,
391391
}
392392

393393
err = test_helpers.ProcessListenOnInstance(args)
@@ -397,8 +397,8 @@ func TestRoundRobinStrategy(t *testing.T) {
397397
args = test_helpers.ListenOnInstanceArgs{
398398
ServersNumber: serversNumber,
399399
ExpectedPorts: replicaPorts,
400-
ConnPool: connPool,
401-
Mode: connection_pool.PreferRO,
400+
ConnPool: connPool,
401+
Mode: connection_pool.PreferRO,
402402
}
403403

404404
err = test_helpers.ProcessListenOnInstance(args)
@@ -435,8 +435,8 @@ func TestRoundRobinStrategy_NoReplica(t *testing.T) {
435435
args := test_helpers.ListenOnInstanceArgs{
436436
ServersNumber: serversNumber,
437437
ExpectedPorts: allPorts,
438-
ConnPool: connPool,
439-
Mode: connection_pool.ANY,
438+
ConnPool: connPool,
439+
Mode: connection_pool.ANY,
440440
}
441441

442442
err = test_helpers.ProcessListenOnInstance(args)
@@ -446,8 +446,8 @@ func TestRoundRobinStrategy_NoReplica(t *testing.T) {
446446
args = test_helpers.ListenOnInstanceArgs{
447447
ServersNumber: serversNumber,
448448
ExpectedPorts: allPorts,
449-
ConnPool: connPool,
450-
Mode: connection_pool.RW,
449+
ConnPool: connPool,
450+
Mode: connection_pool.RW,
451451
}
452452

453453
err = test_helpers.ProcessListenOnInstance(args)
@@ -457,8 +457,8 @@ func TestRoundRobinStrategy_NoReplica(t *testing.T) {
457457
args = test_helpers.ListenOnInstanceArgs{
458458
ServersNumber: serversNumber,
459459
ExpectedPorts: allPorts,
460-
ConnPool: connPool,
461-
Mode: connection_pool.PreferRW,
460+
ConnPool: connPool,
461+
Mode: connection_pool.PreferRW,
462462
}
463463

464464
err = test_helpers.ProcessListenOnInstance(args)
@@ -468,8 +468,8 @@ func TestRoundRobinStrategy_NoReplica(t *testing.T) {
468468
args = test_helpers.ListenOnInstanceArgs{
469469
ServersNumber: serversNumber,
470470
ExpectedPorts: allPorts,
471-
ConnPool: connPool,
472-
Mode: connection_pool.PreferRO,
471+
ConnPool: connPool,
472+
Mode: connection_pool.PreferRO,
473473
}
474474

475475
err = test_helpers.ProcessListenOnInstance(args)
@@ -506,8 +506,8 @@ func TestRoundRobinStrategy_NoMaster(t *testing.T) {
506506
args := test_helpers.ListenOnInstanceArgs{
507507
ServersNumber: serversNumber,
508508
ExpectedPorts: allPorts,
509-
ConnPool: connPool,
510-
Mode: connection_pool.ANY,
509+
ConnPool: connPool,
510+
Mode: connection_pool.ANY,
511511
}
512512

513513
err = test_helpers.ProcessListenOnInstance(args)
@@ -517,8 +517,8 @@ func TestRoundRobinStrategy_NoMaster(t *testing.T) {
517517
args = test_helpers.ListenOnInstanceArgs{
518518
ServersNumber: serversNumber,
519519
ExpectedPorts: allPorts,
520-
ConnPool: connPool,
521-
Mode: connection_pool.RO,
520+
ConnPool: connPool,
521+
Mode: connection_pool.RO,
522522
}
523523

524524
err = test_helpers.ProcessListenOnInstance(args)
@@ -528,8 +528,8 @@ func TestRoundRobinStrategy_NoMaster(t *testing.T) {
528528
args = test_helpers.ListenOnInstanceArgs{
529529
ServersNumber: serversNumber,
530530
ExpectedPorts: allPorts,
531-
ConnPool: connPool,
532-
Mode: connection_pool.PreferRW,
531+
ConnPool: connPool,
532+
Mode: connection_pool.PreferRW,
533533
}
534534

535535
err = test_helpers.ProcessListenOnInstance(args)
@@ -539,8 +539,8 @@ func TestRoundRobinStrategy_NoMaster(t *testing.T) {
539539
args = test_helpers.ListenOnInstanceArgs{
540540
ServersNumber: serversNumber,
541541
ExpectedPorts: allPorts,
542-
ConnPool: connPool,
543-
Mode: connection_pool.PreferRO,
542+
ConnPool: connPool,
543+
Mode: connection_pool.PreferRO,
544544
}
545545

546546
err = test_helpers.ProcessListenOnInstance(args)
@@ -584,8 +584,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
584584
args := test_helpers.ListenOnInstanceArgs{
585585
ServersNumber: serversNumber,
586586
ExpectedPorts: allPorts,
587-
ConnPool: connPool,
588-
Mode: connection_pool.ANY,
587+
ConnPool: connPool,
588+
Mode: connection_pool.ANY,
589589
}
590590

591591
err = test_helpers.ProcessListenOnInstance(args)
@@ -595,8 +595,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
595595
args = test_helpers.ListenOnInstanceArgs{
596596
ServersNumber: serversNumber,
597597
ExpectedPorts: masterPorts,
598-
ConnPool: connPool,
599-
Mode: connection_pool.RW,
598+
ConnPool: connPool,
599+
Mode: connection_pool.RW,
600600
}
601601

602602
err = test_helpers.ProcessListenOnInstance(args)
@@ -606,8 +606,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
606606
args = test_helpers.ListenOnInstanceArgs{
607607
ServersNumber: serversNumber,
608608
ExpectedPorts: replicaPorts,
609-
ConnPool: connPool,
610-
Mode: connection_pool.RO,
609+
ConnPool: connPool,
610+
Mode: connection_pool.RO,
611611
}
612612

613613
err = test_helpers.ProcessListenOnInstance(args)
@@ -617,8 +617,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
617617
args = test_helpers.ListenOnInstanceArgs{
618618
ServersNumber: serversNumber,
619619
ExpectedPorts: masterPorts,
620-
ConnPool: connPool,
621-
Mode: connection_pool.PreferRW,
620+
ConnPool: connPool,
621+
Mode: connection_pool.PreferRW,
622622
}
623623

624624
err = test_helpers.ProcessListenOnInstance(args)
@@ -628,8 +628,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
628628
args = test_helpers.ListenOnInstanceArgs{
629629
ServersNumber: serversNumber,
630630
ExpectedPorts: replicaPorts,
631-
ConnPool: connPool,
632-
Mode: connection_pool.PreferRO,
631+
ConnPool: connPool,
632+
Mode: connection_pool.PreferRO,
633633
}
634634

635635
err = test_helpers.ProcessListenOnInstance(args)
@@ -655,8 +655,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
655655
args = test_helpers.ListenOnInstanceArgs{
656656
ServersNumber: serversNumber,
657657
ExpectedPorts: allPorts,
658-
ConnPool: connPool,
659-
Mode: connection_pool.ANY,
658+
ConnPool: connPool,
659+
Mode: connection_pool.ANY,
660660
}
661661

662662
err = test_helpers.Retry(test_helpers.ProcessListenOnInstance, args, defaultCountRetry, defaultTimeoutRetry)
@@ -666,8 +666,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
666666
args = test_helpers.ListenOnInstanceArgs{
667667
ServersNumber: serversNumber,
668668
ExpectedPorts: masterPorts,
669-
ConnPool: connPool,
670-
Mode: connection_pool.RW,
669+
ConnPool: connPool,
670+
Mode: connection_pool.RW,
671671
}
672672

673673
err = test_helpers.Retry(test_helpers.ProcessListenOnInstance, args, defaultCountRetry, defaultTimeoutRetry)
@@ -677,8 +677,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
677677
args = test_helpers.ListenOnInstanceArgs{
678678
ServersNumber: serversNumber,
679679
ExpectedPorts: replicaPorts,
680-
ConnPool: connPool,
681-
Mode: connection_pool.RO,
680+
ConnPool: connPool,
681+
Mode: connection_pool.RO,
682682
}
683683

684684
err = test_helpers.Retry(test_helpers.ProcessListenOnInstance, args, defaultCountRetry, defaultTimeoutRetry)
@@ -688,8 +688,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
688688
args = test_helpers.ListenOnInstanceArgs{
689689
ServersNumber: serversNumber,
690690
ExpectedPorts: masterPorts,
691-
ConnPool: connPool,
692-
Mode: connection_pool.PreferRW,
691+
ConnPool: connPool,
692+
Mode: connection_pool.PreferRW,
693693
}
694694

695695
err = test_helpers.Retry(test_helpers.ProcessListenOnInstance, args, defaultCountRetry, defaultTimeoutRetry)
@@ -699,8 +699,8 @@ func TestUpdateInstancesRoles(t *testing.T) {
699699
args = test_helpers.ListenOnInstanceArgs{
700700
ServersNumber: serversNumber,
701701
ExpectedPorts: replicaPorts,
702-
ConnPool: connPool,
703-
Mode: connection_pool.PreferRO,
702+
ConnPool: connPool,
703+
Mode: connection_pool.PreferRO,
704704
}
705705

706706
err = test_helpers.Retry(test_helpers.ProcessListenOnInstance, args, defaultCountRetry, defaultTimeoutRetry)

connection_pool/const.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Mode parameter:
2929
| upsert | RW |
3030
| select | ANY |
3131
| get | ANY |
32-
*/
32+
*/
3333
const (
3434
ANY = iota
3535
RW

0 commit comments

Comments
 (0)