Skip to content

Commit

Permalink
Add more linters (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacome authored Jun 17, 2024
1 parent a0e213b commit 5ba85ee
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
18 changes: 15 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ linters-settings:
- name: error-strings
- name: errorf
- name: exported
- name: if-return
- name: increment-decrement
- name: indent-error-flow
- name: package-comments
Expand All @@ -29,33 +28,45 @@ linters-settings:
- name: var-declaration
- name: var-naming
govet:
check-shadowing: true
enable-all: true

linters:
enable:
- asasalint
- asciicheck
- bidichk
- dupword
- errcheck
- errname
- errorlint
- exportloopref
- fatcontext
- forcetypeassert
- gocheckcompilerdirectives
- godot
- gofmt
- gofumpt
- goimports
- gosec
- gosimple
- gosmopolitan
- govet
- ineffassign
- intrange
- makezero
- misspell
- nilerr
- noctx
- nolintlint
- perfsprint
- prealloc
- predeclared
- reassign
- revive
- staticcheck
- stylecheck
- tagalign
- tenv
- thelper
- tparallel
- typecheck
- unconvert
Expand All @@ -64,6 +75,7 @@ linters:
- usestdlibvars
- wastedassign
- whitespace
- wrapcheck
disable-all: true
issues:
max-issues-per-linter: 0
Expand Down
56 changes: 28 additions & 28 deletions client/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
defaultWeight = 1
)

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

// NginxClient lets you access NGINX Plus API.
Expand Down Expand Up @@ -151,10 +151,10 @@ type NginxInfo struct {
ParentProcessID uint64 `json:"ppid"`
}

// Caches is a map of cache stats by cache zone
// Caches is a map of cache stats by cache zone.
type Caches = map[string]HTTPCache

// HTTPCache represents a zone's HTTP Cache
// HTTPCache represents a zone's HTTP Cache.
type HTTPCache struct {
Size uint64
MaxSize uint64 `json:"max_size"`
Expand Down Expand Up @@ -204,7 +204,7 @@ type Pages struct {
Free uint64
}

// Slots is a map of slots by slot size
// Slots is a map of slots by slot size.
type Slots map[string]Slot

// Slot represents slot related stats.
Expand Down Expand Up @@ -241,7 +241,7 @@ type VerifyFailures struct {
Other uint64 `json:"other"`
}

// ServerZones is map of server zone stats by zone name
// ServerZones is map of server zone stats by zone name.
type ServerZones map[string]ServerZone

// ServerZone represents server zone related stats.
Expand Down Expand Up @@ -269,19 +269,19 @@ type StreamServerZone struct {
SSL SSL
}

// StreamZoneSync represents the sync information per each shared memory zone and the sync information per node in a cluster
// StreamZoneSync represents the sync information per each shared memory zone and the sync information per node in a cluster.
type StreamZoneSync struct {
Zones map[string]SyncZone
Status StreamZoneSyncStatus
}

// SyncZone represents the synchronization status of a shared memory zone
// SyncZone represents the synchronization status of a shared memory zone.
type SyncZone struct {
RecordsPending uint64 `json:"records_pending"`
RecordsTotal uint64 `json:"records_total"`
}

// StreamZoneSyncStatus represents the status of a shared memory zone
// StreamZoneSyncStatus represents the status of a shared memory zone.
type StreamZoneSyncStatus struct {
BytesIn uint64 `json:"bytes_in"`
MsgsIn uint64 `json:"msgs_in"`
Expand All @@ -301,7 +301,7 @@ type Responses struct {
Total uint64
}

// HTTPCodes represents HTTP response codes
// HTTPCodes represents HTTP response codes.
type HTTPCodes struct {
HTTPContinue uint64 `json:"100,omitempty"`
HTTPSwitchingProtocols uint64 `json:"101,omitempty"`
Expand Down Expand Up @@ -442,13 +442,13 @@ type HealthChecks struct {
LastPassed bool `json:"last_passed"`
}

// LocationZones represents location_zones related stats
// LocationZones represents location_zones related stats.
type LocationZones map[string]LocationZone

// Resolvers represents resolvers related stats
// Resolvers represents resolvers related stats.
type Resolvers map[string]Resolver

// LocationZone represents location_zones related stats
// LocationZone represents location_zones related stats.
type LocationZone struct {
Requests int64
Responses Responses
Expand All @@ -457,20 +457,20 @@ type LocationZone struct {
Sent int64
}

// Resolver represents resolvers related stats
// Resolver represents resolvers related stats.
type Resolver struct {
Requests ResolverRequests `json:"requests"`
Responses ResolverResponses `json:"responses"`
}

// ResolverRequests represents resolver requests
// ResolverRequests represents resolver requests.
type ResolverRequests struct {
Name int64
Srv int64
Addr int64
}

// ResolverResponses represents resolver responses
// ResolverResponses represents resolver responses.
type ResolverResponses struct {
Noerror int64
Formerr int64
Expand All @@ -482,12 +482,12 @@ type ResolverResponses struct {
Unknown int64
}

// Processes represents processes related stats
// Processes represents processes related stats.
type Processes struct {
Respawned int64
}

// HTTPLimitRequest represents HTTP Requests Rate Limiting
// HTTPLimitRequest represents HTTP Requests Rate Limiting.
type HTTPLimitRequest struct {
Passed uint64
Delayed uint64
Expand All @@ -496,31 +496,31 @@ type HTTPLimitRequest struct {
RejectedDryRun uint64 `json:"rejected_dry_run"`
}

// HTTPLimitRequests represents limit requests related stats
// HTTPLimitRequests represents limit requests related stats.
type HTTPLimitRequests map[string]HTTPLimitRequest

// LimitConnection represents Connections Limiting
// LimitConnection represents Connections Limiting.
type LimitConnection struct {
Passed uint64
Rejected uint64
RejectedDryRun uint64 `json:"rejected_dry_run"`
}

// HTTPLimitConnections represents limit connections related stats
// HTTPLimitConnections represents limit connections related stats.
type HTTPLimitConnections map[string]LimitConnection

// StreamLimitConnections represents limit connections related stats
// StreamLimitConnections represents limit connections related stats.
type StreamLimitConnections map[string]LimitConnection

// Workers represents worker connections related stats
// Workers represents worker connections related stats.
type Workers struct {
ID int
ProcessID uint64 `json:"pid"`
HTTP WorkersHTTP `json:"http"`
Connections Connections
}

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

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

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

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

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

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

// GetCaches returns Cache stats
// GetCaches returns Cache stats.
func (client *NginxClient) GetCaches() (*Caches, error) {
var caches Caches
err := client.get("http/caches", &caches)
Expand Down
10 changes: 10 additions & 0 deletions client/nginx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func TestDetermineUpdates(t *testing.T) {
t.Parallel()
maxConns := 1
tests := []struct {
updated []UpstreamServer
Expand Down Expand Up @@ -162,6 +163,7 @@ func TestDetermineUpdates(t *testing.T) {
}

func TestStreamDetermineUpdates(t *testing.T) {
t.Parallel()
maxConns := 1
tests := []struct {
updated []StreamUpstreamServer
Expand Down Expand Up @@ -318,6 +320,7 @@ func TestStreamDetermineUpdates(t *testing.T) {
}

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

func TestHaveSameParameters(t *testing.T) {
t.Parallel()
tests := []struct {
server UpstreamServer
serverNGX UpstreamServer
Expand Down Expand Up @@ -449,6 +453,7 @@ func TestHaveSameParameters(t *testing.T) {
}

func TestHaveSameParametersForStream(t *testing.T) {
t.Parallel()
tests := []struct {
server StreamUpstreamServer
serverNGX StreamUpstreamServer
Expand Down Expand Up @@ -523,6 +528,7 @@ func TestHaveSameParametersForStream(t *testing.T) {
}

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

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

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

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

func TestGetStats_SSL(t *testing.T) {
t.Parallel()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/" {
_, err := w.Write([]byte(`[4, 5, 6, 7, 8, 9]`))
Expand Down
9 changes: 5 additions & 4 deletions tests/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,11 +1177,12 @@ func TestStreamZoneSync(t *testing.T) {
}

func compareUpstreamServers(x []client.UpstreamServer, y []client.UpstreamServer) bool {
var xServers []string
xServers := make([]string, 0, len(x))
for _, us := range x {
xServers = append(xServers, us.Server)
}
var yServers []string

yServers := make([]string, 0, len(y))
for _, us := range y {
yServers = append(yServers, us.Server)
}
Expand All @@ -1190,11 +1191,11 @@ func compareUpstreamServers(x []client.UpstreamServer, y []client.UpstreamServer
}

func compareStreamUpstreamServers(x []client.StreamUpstreamServer, y []client.StreamUpstreamServer) bool {
var xServers []string
xServers := make([]string, 0, len(x))
for _, us := range x {
xServers = append(xServers, us.Server)
}
var yServers []string
yServers := make([]string, 0, len(y))
for _, us := range y {
yServers = append(yServers, us.Server)
}
Expand Down

0 comments on commit 5ba85ee

Please sign in to comment.