Skip to content

Commit 5ba85ee

Browse files
authored
Add more linters (#290)
1 parent a0e213b commit 5ba85ee

File tree

4 files changed

+58
-35
lines changed

4 files changed

+58
-35
lines changed

.golangci.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ linters-settings:
1414
- name: error-strings
1515
- name: errorf
1616
- name: exported
17-
- name: if-return
1817
- name: increment-decrement
1918
- name: indent-error-flow
2019
- name: package-comments
@@ -29,33 +28,45 @@ linters-settings:
2928
- name: var-declaration
3029
- name: var-naming
3130
govet:
32-
check-shadowing: true
3331
enable-all: true
34-
3532
linters:
3633
enable:
34+
- asasalint
3735
- asciicheck
3836
- bidichk
3937
- dupword
4038
- errcheck
39+
- errname
4140
- errorlint
41+
- exportloopref
42+
- fatcontext
43+
- forcetypeassert
44+
- gocheckcompilerdirectives
45+
- godot
4246
- gofmt
4347
- gofumpt
4448
- goimports
4549
- gosec
4650
- gosimple
51+
- gosmopolitan
4752
- govet
4853
- ineffassign
54+
- intrange
4955
- makezero
5056
- misspell
5157
- nilerr
5258
- noctx
59+
- nolintlint
5360
- perfsprint
61+
- prealloc
5462
- predeclared
5563
- reassign
5664
- revive
5765
- staticcheck
66+
- stylecheck
5867
- tagalign
68+
- tenv
69+
- thelper
5970
- tparallel
6071
- typecheck
6172
- unconvert
@@ -64,6 +75,7 @@ linters:
6475
- usestdlibvars
6576
- wastedassign
6677
- whitespace
78+
- wrapcheck
6779
disable-all: true
6880
issues:
6981
max-issues-per-linter: 0

client/nginx.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
defaultWeight = 1
3838
)
3939

40-
// ErrUnsupportedVer means that client's API version is not supported by NGINX plus API
40+
// ErrUnsupportedVer means that client's API version is not supported by NGINX plus API.
4141
var ErrUnsupportedVer = errors.New("API version of the client is not supported by running NGINX Plus")
4242

4343
// NginxClient lets you access NGINX Plus API.
@@ -151,10 +151,10 @@ type NginxInfo struct {
151151
ParentProcessID uint64 `json:"ppid"`
152152
}
153153

154-
// Caches is a map of cache stats by cache zone
154+
// Caches is a map of cache stats by cache zone.
155155
type Caches = map[string]HTTPCache
156156

157-
// HTTPCache represents a zone's HTTP Cache
157+
// HTTPCache represents a zone's HTTP Cache.
158158
type HTTPCache struct {
159159
Size uint64
160160
MaxSize uint64 `json:"max_size"`
@@ -204,7 +204,7 @@ type Pages struct {
204204
Free uint64
205205
}
206206

207-
// Slots is a map of slots by slot size
207+
// Slots is a map of slots by slot size.
208208
type Slots map[string]Slot
209209

210210
// Slot represents slot related stats.
@@ -241,7 +241,7 @@ type VerifyFailures struct {
241241
Other uint64 `json:"other"`
242242
}
243243

244-
// ServerZones is map of server zone stats by zone name
244+
// ServerZones is map of server zone stats by zone name.
245245
type ServerZones map[string]ServerZone
246246

247247
// ServerZone represents server zone related stats.
@@ -269,19 +269,19 @@ type StreamServerZone struct {
269269
SSL SSL
270270
}
271271

272-
// StreamZoneSync represents the sync information per each shared memory zone and the sync information per node in a cluster
272+
// StreamZoneSync represents the sync information per each shared memory zone and the sync information per node in a cluster.
273273
type StreamZoneSync struct {
274274
Zones map[string]SyncZone
275275
Status StreamZoneSyncStatus
276276
}
277277

278-
// SyncZone represents the synchronization status of a shared memory zone
278+
// SyncZone represents the synchronization status of a shared memory zone.
279279
type SyncZone struct {
280280
RecordsPending uint64 `json:"records_pending"`
281281
RecordsTotal uint64 `json:"records_total"`
282282
}
283283

284-
// StreamZoneSyncStatus represents the status of a shared memory zone
284+
// StreamZoneSyncStatus represents the status of a shared memory zone.
285285
type StreamZoneSyncStatus struct {
286286
BytesIn uint64 `json:"bytes_in"`
287287
MsgsIn uint64 `json:"msgs_in"`
@@ -301,7 +301,7 @@ type Responses struct {
301301
Total uint64
302302
}
303303

304-
// HTTPCodes represents HTTP response codes
304+
// HTTPCodes represents HTTP response codes.
305305
type HTTPCodes struct {
306306
HTTPContinue uint64 `json:"100,omitempty"`
307307
HTTPSwitchingProtocols uint64 `json:"101,omitempty"`
@@ -442,13 +442,13 @@ type HealthChecks struct {
442442
LastPassed bool `json:"last_passed"`
443443
}
444444

445-
// LocationZones represents location_zones related stats
445+
// LocationZones represents location_zones related stats.
446446
type LocationZones map[string]LocationZone
447447

448-
// Resolvers represents resolvers related stats
448+
// Resolvers represents resolvers related stats.
449449
type Resolvers map[string]Resolver
450450

451-
// LocationZone represents location_zones related stats
451+
// LocationZone represents location_zones related stats.
452452
type LocationZone struct {
453453
Requests int64
454454
Responses Responses
@@ -457,20 +457,20 @@ type LocationZone struct {
457457
Sent int64
458458
}
459459

460-
// Resolver represents resolvers related stats
460+
// Resolver represents resolvers related stats.
461461
type Resolver struct {
462462
Requests ResolverRequests `json:"requests"`
463463
Responses ResolverResponses `json:"responses"`
464464
}
465465

466-
// ResolverRequests represents resolver requests
466+
// ResolverRequests represents resolver requests.
467467
type ResolverRequests struct {
468468
Name int64
469469
Srv int64
470470
Addr int64
471471
}
472472

473-
// ResolverResponses represents resolver responses
473+
// ResolverResponses represents resolver responses.
474474
type ResolverResponses struct {
475475
Noerror int64
476476
Formerr int64
@@ -482,12 +482,12 @@ type ResolverResponses struct {
482482
Unknown int64
483483
}
484484

485-
// Processes represents processes related stats
485+
// Processes represents processes related stats.
486486
type Processes struct {
487487
Respawned int64
488488
}
489489

490-
// HTTPLimitRequest represents HTTP Requests Rate Limiting
490+
// HTTPLimitRequest represents HTTP Requests Rate Limiting.
491491
type HTTPLimitRequest struct {
492492
Passed uint64
493493
Delayed uint64
@@ -496,31 +496,31 @@ type HTTPLimitRequest struct {
496496
RejectedDryRun uint64 `json:"rejected_dry_run"`
497497
}
498498

499-
// HTTPLimitRequests represents limit requests related stats
499+
// HTTPLimitRequests represents limit requests related stats.
500500
type HTTPLimitRequests map[string]HTTPLimitRequest
501501

502-
// LimitConnection represents Connections Limiting
502+
// LimitConnection represents Connections Limiting.
503503
type LimitConnection struct {
504504
Passed uint64
505505
Rejected uint64
506506
RejectedDryRun uint64 `json:"rejected_dry_run"`
507507
}
508508

509-
// HTTPLimitConnections represents limit connections related stats
509+
// HTTPLimitConnections represents limit connections related stats.
510510
type HTTPLimitConnections map[string]LimitConnection
511511

512-
// StreamLimitConnections represents limit connections related stats
512+
// StreamLimitConnections represents limit connections related stats.
513513
type StreamLimitConnections map[string]LimitConnection
514514

515-
// Workers represents worker connections related stats
515+
// Workers represents worker connections related stats.
516516
type Workers struct {
517517
ID int
518518
ProcessID uint64 `json:"pid"`
519519
HTTP WorkersHTTP `json:"http"`
520520
Connections Connections
521521
}
522522

523-
// WorkersHTTP represents HTTP worker connections
523+
// WorkersHTTP represents HTTP worker connections.
524524
type WorkersHTTP struct {
525525
HTTPRequests HTTPRequests `json:"requests"`
526526
}
@@ -725,7 +725,7 @@ func (client *NginxClient) UpdateHTTPServers(upstream string, servers []Upstream
725725
}
726726

727727
// We assume port 80 if no port is set for servers.
728-
var formattedServers []UpstreamServer
728+
formattedServers := make([]UpstreamServer, 0, len(servers))
729729
for _, server := range servers {
730730
server.Server = addPortToServer(server.Server)
731731
formattedServers = append(formattedServers, server)
@@ -757,7 +757,7 @@ func (client *NginxClient) UpdateHTTPServers(upstream string, servers []Upstream
757757
return toAdd, toDelete, toUpdate, nil
758758
}
759759

760-
// haveSameParameters checks if a given server has the same parameters as a server already present in NGINX. Order matters
760+
// haveSameParameters checks if a given server has the same parameters as a server already present in NGINX. Order matters.
761761
func haveSameParameters(newServer UpstreamServer, serverNGX UpstreamServer) bool {
762762
newServer.ID = serverNGX.ID
763763

@@ -1036,7 +1036,7 @@ func (client *NginxClient) UpdateStreamServers(upstream string, servers []Stream
10361036
return nil, nil, nil, fmt.Errorf("failed to update stream servers of %v upstream: %w", upstream, err)
10371037
}
10381038

1039-
var formattedServers []StreamUpstreamServer
1039+
formattedServers := make([]StreamUpstreamServer, 0, len(servers))
10401040
for _, server := range servers {
10411041
server.Server = addPortToServer(server.Server)
10421042
formattedServers = append(formattedServers, server)
@@ -1083,7 +1083,7 @@ func (client *NginxClient) getIDOfStreamServer(upstream string, name string) (in
10831083
return -1, nil
10841084
}
10851085

1086-
// haveSameParametersForStream checks if a given server has the same parameters as a server already present in NGINX. Order matters
1086+
// haveSameParametersForStream checks if a given server has the same parameters as a server already present in NGINX. Order matters.
10871087
func haveSameParametersForStream(newServer StreamUpstreamServer, serverNGX StreamUpstreamServer) bool {
10881088
newServer.ID = serverNGX.ID
10891089
if serverNGX.MaxConns != nil && newServer.MaxConns == nil {
@@ -1330,7 +1330,7 @@ func (client *NginxClient) GetNginxInfo() (*NginxInfo, error) {
13301330
return &info, nil
13311331
}
13321332

1333-
// GetCaches returns Cache stats
1333+
// GetCaches returns Cache stats.
13341334
func (client *NginxClient) GetCaches() (*Caches, error) {
13351335
var caches Caches
13361336
err := client.get("http/caches", &caches)

client/nginx_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
func TestDetermineUpdates(t *testing.T) {
12+
t.Parallel()
1213
maxConns := 1
1314
tests := []struct {
1415
updated []UpstreamServer
@@ -162,6 +163,7 @@ func TestDetermineUpdates(t *testing.T) {
162163
}
163164

164165
func TestStreamDetermineUpdates(t *testing.T) {
166+
t.Parallel()
165167
maxConns := 1
166168
tests := []struct {
167169
updated []StreamUpstreamServer
@@ -318,6 +320,7 @@ func TestStreamDetermineUpdates(t *testing.T) {
318320
}
319321

320322
func TestAddPortToServer(t *testing.T) {
323+
t.Parallel()
321324
// More info about addresses http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server
322325
tests := []struct {
323326
address string
@@ -370,6 +373,7 @@ func TestAddPortToServer(t *testing.T) {
370373
}
371374

372375
func TestHaveSameParameters(t *testing.T) {
376+
t.Parallel()
373377
tests := []struct {
374378
server UpstreamServer
375379
serverNGX UpstreamServer
@@ -449,6 +453,7 @@ func TestHaveSameParameters(t *testing.T) {
449453
}
450454

451455
func TestHaveSameParametersForStream(t *testing.T) {
456+
t.Parallel()
452457
tests := []struct {
453458
server StreamUpstreamServer
454459
serverNGX StreamUpstreamServer
@@ -523,6 +528,7 @@ func TestHaveSameParametersForStream(t *testing.T) {
523528
}
524529

525530
func TestClientWithCheckAPI(t *testing.T) {
531+
t.Parallel()
526532
// Create a test server that returns supported API versions
527533
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
528534
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
@@ -552,6 +558,7 @@ func TestClientWithCheckAPI(t *testing.T) {
552558
}
553559

554560
func TestClientWithAPIVersion(t *testing.T) {
561+
t.Parallel()
555562
// Test creating a new client with a supported API version on the client
556563
client, err := NewNginxClient("http://api-url", WithAPIVersion(8))
557564
if err != nil {
@@ -572,6 +579,7 @@ func TestClientWithAPIVersion(t *testing.T) {
572579
}
573580

574581
func TestClientWithHTTPClient(t *testing.T) {
582+
t.Parallel()
575583
// Test creating a new client passing a custom HTTP client
576584
client, err := NewNginxClient("http://api-url", WithHTTPClient(&http.Client{}))
577585
if err != nil {
@@ -592,6 +600,7 @@ func TestClientWithHTTPClient(t *testing.T) {
592600
}
593601

594602
func TestGetStats_NoStreamEndpoint(t *testing.T) {
603+
t.Parallel()
595604
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
596605
if r.RequestURI == "/" {
597606
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
@@ -643,6 +652,7 @@ func TestGetStats_NoStreamEndpoint(t *testing.T) {
643652
}
644653

645654
func TestGetStats_SSL(t *testing.T) {
655+
t.Parallel()
646656
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
647657
if r.RequestURI == "/" {
648658
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))

tests/client_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,11 +1177,12 @@ func TestStreamZoneSync(t *testing.T) {
11771177
}
11781178

11791179
func compareUpstreamServers(x []client.UpstreamServer, y []client.UpstreamServer) bool {
1180-
var xServers []string
1180+
xServers := make([]string, 0, len(x))
11811181
for _, us := range x {
11821182
xServers = append(xServers, us.Server)
11831183
}
1184-
var yServers []string
1184+
1185+
yServers := make([]string, 0, len(y))
11851186
for _, us := range y {
11861187
yServers = append(yServers, us.Server)
11871188
}
@@ -1190,11 +1191,11 @@ func compareUpstreamServers(x []client.UpstreamServer, y []client.UpstreamServer
11901191
}
11911192

11921193
func compareStreamUpstreamServers(x []client.StreamUpstreamServer, y []client.StreamUpstreamServer) bool {
1193-
var xServers []string
1194+
xServers := make([]string, 0, len(x))
11941195
for _, us := range x {
11951196
xServers = append(xServers, us.Server)
11961197
}
1197-
var yServers []string
1198+
yServers := make([]string, 0, len(y))
11981199
for _, us := range y {
11991200
yServers = append(yServers, us.Server)
12001201
}

0 commit comments

Comments
 (0)