Skip to content

Commit

Permalink
chore: fix from golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
robotomize committed May 18, 2021
1 parent f3fcbe9 commit 4b6b799
Show file tree
Hide file tree
Showing 42 changed files with 451 additions and 289 deletions.
74 changes: 74 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
run:
# default: '1m'
timeout: '5m'

# default: []
build-tags:
- 'all'

# default: true
skip-dirs-use-default: false

# default: ''
modules-download-mode: 'readonly'

# default: false
allow-parallel-runners: true

linters:
enable:
- 'asciicheck'
- 'bodyclose'
- 'deadcode'
- 'depguard'
- 'dogsled'
- 'errcheck'
- 'errorlint'
- 'exportloopref'
- 'gofmt'
- 'gofumpt'
- 'goheader'
- 'goimports'
- 'gomodguard'
- 'goprintffuncname'
- 'gosec'
- 'gosimple'
- 'govet'
- 'ineffassign'
- 'makezero'
- 'misspell'
- 'noctx'
- 'paralleltest'
- 'prealloc'
- 'predeclared'
- 'sqlclosecheck'
- 'staticcheck'
- 'structcheck'
- 'stylecheck'
- 'typecheck'
- 'unconvert'
- 'unused'
- 'varcheck'
- 'whitespace'

issues:
# default: []
exclude:
- '^S1023:' # staticcheck: redundant returns help with http handlers
- '^SA3000:' # staticcheck: not required in Go 11.4+
- '^G102:' # gosec: to bind to all ifaces
- '^G402:' # gosec: some services terminate at the load balancer
- '^G401:' # gosec: use crypto/sha1
- '^G505:' # gosec: use crypto/sha1
- '^G108:' # profiling is available
- '^Range statement' # paralleltest: false positives

# default: 50
max-issues-per-linter: 0

# default: 3
max-same-issues: 0

severity:
# default: ''
default-severity: error
18 changes: 10 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
APP?=sod
NAME?=sod
RELEASE?=0.0.0
APP?=sod-srv
NAME?=sod server
RELEASE?=$(shell git describe --tags --abbrev=0)
GOOS?=linux
GOARCH=amd64
GO111MODULE?=on
BUILD_TIME?=$(shell date -u '+%Y-%m-%d_%H:%M:%S')

unittest:
Expand All @@ -10,13 +12,13 @@ unittest:
test:
go test -v -cover -covermode=atomic ./...

test-cover:
go test -count=2 -race -timeout=10m ./... -coverprofile=coverage.out

.PHONY: build
build: clean
# build server
CGO_ENABLED=0 GOOS=${GOOS} go build ./cmd/sod \
-ldflags "-X main.version=${RELEASE} -X main.buildTime=${BUILD_TIME} -X main.name=${NAME}" \
-o build/${APP}
GOARCH=${GOARCH} GO111MODULE=${GO111MODULE} CGO_ENABLED=0 GOOS=${GOOS} go build -o ${APP} -trimpath -ldflags "-s -w -X main.version=${RELEASE} -X main.buildTime=${BUILD_TIME} -X main.projectName=${NAME}" ./cmd/sod-srv

.PHONY: clean
clean:
@rm -f build/${APP}
@rm -f ${APP}
27 changes: 20 additions & 7 deletions cmd/sod-srv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@ import (
"fmt"
"net/http"
_ "net/http/pprof"
"sod/internal/collect"
"sod/internal/config"
"sod/internal/logging"
"sod/internal/predict"
"sod/internal/server"
"sod/internal/setup"
"sod/internal/shutdown"
"os"
"time"

"github.com/go-sod/sod/internal/collect"
sod "github.com/go-sod/sod/internal/config"
"github.com/go-sod/sod/internal/logging"
"github.com/go-sod/sod/internal/predict"
"github.com/go-sod/sod/internal/server"
"github.com/go-sod/sod/internal/setup"
"github.com/go-sod/sod/internal/shutdown"
)

var (
version string
buildTime = time.Now().String()
projectName = "SOD server"
graffiti = " _____ ___________ \n/ ___|| _ | _ \\\n\\ `--. | | | | | | |\n `--. \\| | | | | | |\n/\\__/ /\\ \\_/ / |/ / \n\\____/ \\___/|___/ \n\n"
)

func main() {
_, _ = fmt.Fprint(os.Stdout, graffiti)
_, _ = fmt.Fprintf(os.Stdout, "%s: %s, %s\n", projectName, buildTime, version)

ctx, done := shutdown.New()
logger := logging.FromContext(ctx)
if err := run(ctx, done); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module sod
module github.com/go-sod/sod

go 1.16

require (
github.com/davecgh/go-xdr v0.0.0-20161123171359-e6a2ba005892
github.com/google/uuid v1.1.2
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.4.0
github.com/stretchr/testify v1.4.0 // indirect
github.com/valyala/fastrand v1.0.0
github.com/vektra/mockery/v2 v2.3.0 // indirect
go.etcd.io/bbolt v1.3.5
Expand Down
39 changes: 20 additions & 19 deletions internal/alert/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"io/ioutil"
"net/http"
"net/url"
alertDb "sod/internal/alert/database"
"sod/internal/alert/model"
"sod/internal/database"
"sod/internal/httputil"
"sod/internal/logging"
metricModel "sod/internal/metric/model"
"sod/pkg/container/rworker"
"sync"
"time"

alertDb "github.com/go-sod/sod/internal/alert/database"
"github.com/go-sod/sod/internal/alert/model"
"github.com/go-sod/sod/internal/database"
"github.com/go-sod/sod/internal/httputil"
"github.com/go-sod/sod/internal/logging"
metricModel "github.com/go-sod/sod/internal/metric/model"
"github.com/go-sod/sod/pkg/container/rworker"
)

type ProvideFn = func(chan<- error) (Manager, error)
Expand Down Expand Up @@ -66,7 +67,7 @@ type request struct {

func New(db *database.DB, shutdownCh chan<- error, opts ...Option) (*manager, error) {
m := &manager{
alertDb: alertDb.New(db),
alertDB: alertDb.New(db),
shutdownCh: shutdownCh,
targets: Targets{},
alerts: map[string][]metricModel.Metric{},
Expand All @@ -78,7 +79,7 @@ func New(db *database.DB, shutdownCh chan<- error, opts ...Option) (*manager, er
if _, ok := m.clients[target.EntityID]; !ok {
client, err := httputil.NewClientFromConfig(target.HTTPConfig, true)
if err != nil {
return nil, fmt.Errorf("unable crate client for entity %s: %v", target.EntityID, err)
return nil, fmt.Errorf("unable crate client for entity %s: %w", target.EntityID, err)
}
m.clients[target.EntityID] = client
}
Expand All @@ -99,7 +100,7 @@ type Manager interface {
type manager struct {
mtx sync.RWMutex
opts Options
alertDb *alertDb.DB
alertDB *alertDb.DB
shutdownCh chan<- error
targets Targets
clients map[string]*http.Client
Expand All @@ -110,9 +111,9 @@ type manager struct {
func (m *manager) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
m.cancel = cancel
go m.notifier(ctx, m.alertDb.Store, m.alertDb.Delete)
if err := m.bulkLoad(ctx, m.alertDb.Delete, m.alertDb.FindAll); err != nil {
return fmt.Errorf("can not start alert manager: %v", err)
go m.notifier(ctx, m.alertDB.Store, m.alertDB.Delete)
if err := m.bulkLoad(ctx, m.alertDB.Delete, m.alertDB.FindAll); err != nil {
return fmt.Errorf("can not start alert manager: %w", err)
}
return nil
}
Expand Down Expand Up @@ -145,7 +146,7 @@ func (m *manager) bulkLoad(ctx context.Context, deleteFn deleteFn, fetchFn fetch
for i := range alerts {
m.Notify(alerts[i].Metrics...)
if err := deleteFn(context.Background(), alerts[i]); err != nil {
return fmt.Errorf("unable delete alert on bulkLoad: %v", err)
return fmt.Errorf("unable delete alert on bulkLoad: %w", err)
}
}
return nil
Expand All @@ -159,7 +160,7 @@ func (m *manager) shutdown(fn storeFn) error {
for entityID, metrics := range m.alerts {
alert := model.NewAlert(entityID, metrics)
if err := fn(context.Background(), alert); err != nil {
return fmt.Errorf("alert shutdown: unable store alert: %v", err)
return fmt.Errorf("alert shutdown: unable store alert: %w", err)
}
}
return nil
Expand Down Expand Up @@ -195,7 +196,7 @@ func (m *manager) notifier(ctx context.Context, storeFn storeFn, deleteFn delete
rworker.Job(&wg, func() error {
alertModel := model.NewAlert(metrics[0].EntityID, metrics)
if err := storeFn(context.Background(), alertModel); err != nil {
return fmt.Errorf("unable store alert: %v", err)
return fmt.Errorf("unable store alert: %w", err)
}
if err := m.do(context.Background(), target, func() request {
outliers := make([]data, len(metrics))
Expand All @@ -212,10 +213,10 @@ func (m *manager) notifier(ctx context.Context, storeFn storeFn, deleteFn delete
Data: outliers,
}
}); err != nil {
return fmt.Errorf("alert do request error: %v", err)
return fmt.Errorf("alert do request error: %w", err)
}
if err := deleteFn(context.Background(), alertModel); err != nil {
return fmt.Errorf("unable store alert: %v", err)
return fmt.Errorf("unable store alert: %w", err)
}
m.mtx.Lock()
m.alerts[target.EntityID] = m.alerts[target.EntityID][:0]
Expand All @@ -241,7 +242,7 @@ func (m *manager) do(ctx context.Context, target Target, fn makeRequestFn) error
}

b := make([]byte, len(body))
link, err := url.Parse(target.Url)
link, err := url.Parse(target.URL)
if err != nil {
return fmt.Errorf("url parsing error: %w", err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/alert/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package alert

import (
"encoding/json"
"sod/internal/httputil"
"time"

"github.com/go-sod/sod/internal/httputil"
)

type Config struct {
Expand All @@ -25,7 +26,7 @@ func (ts *Targets) Decode(value string) error {
}

type Target struct {
Url string `json:"url"`
URL string `json:"url"`
EntityID string `json:"entityId"`
HTTPConfig httputil.HTTPClientConfig `json:"httpConfig"`
}
15 changes: 8 additions & 7 deletions internal/alert/database/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"encoding/json"
"fmt"

"github.com/go-sod/sod/internal/alert/model"
"github.com/go-sod/sod/internal/database"
bolt "go.etcd.io/bbolt"
"sod/internal/alert/model"
"sod/internal/database"
)

const (
Expand Down Expand Up @@ -69,7 +70,7 @@ func (db *DB) Store(_ context.Context, alert model.Alert) error {
}
return nil
}); err != nil {
return fmt.Errorf("update transaction error: %v", err)
return fmt.Errorf("update transaction error: %w", err)
}
return nil
}
Expand All @@ -84,7 +85,7 @@ func (db *DB) Delete(_ context.Context, alert model.Alert) error {

return b.Delete([]byte(alert.ID.String()))
}); err != nil {
return fmt.Errorf("update transaction error: %v", err)
return fmt.Errorf("update transaction error: %w", err)
}
return nil
}
Expand All @@ -96,7 +97,7 @@ func (db *DB) FindAll(_ context.Context, filter FilterFn) ([]model.Alert, error)
)
tx, err := db.sDB.DB.Begin(true)
if err != nil {
return nil, fmt.Errorf("starting transaction: %v", err)
return nil, fmt.Errorf("starting transaction: %w", err)
}

defer tx.Rollback() //nolint:errcheck
Expand All @@ -121,7 +122,7 @@ func (db *DB) FindAll(_ context.Context, filter FilterFn) ([]model.Alert, error)
for k, v := c.First(); k != nil; k, v = c.Next() {
var m model.Alert
if err := json.Unmarshal(v, &m); err != nil {
return nil, fmt.Errorf("metricCollector unmarshal error, %q", err)
return nil, fmt.Errorf("metricCollector unmarshal error, %w", err)
}
metrics = append(metrics, m)
}
Expand All @@ -138,7 +139,7 @@ func (db *DB) FindAll(_ context.Context, filter FilterFn) ([]model.Alert, error)
}
}
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("committing transaction: %v", err)
return nil, fmt.Errorf("committing transaction: %w", err)
}
return filtered, nil
}
5 changes: 3 additions & 2 deletions internal/alert/model/alert_model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package model

import (
"github.com/google/uuid"
"sod/internal/metric/model"
"time"

"github.com/go-sod/sod/internal/metric/model"
"github.com/google/uuid"
)

func NewAlert(entityID string, metrics []model.Metric) Alert {
Expand Down
11 changes: 6 additions & 5 deletions internal/collect/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"encoding/json"
"fmt"
"net/http"
"sod/internal/dispatcher"
"sod/internal/geom"
"sod/internal/httputil"
"sod/internal/logging"
"sod/internal/metric/model"
"sort"
"time"

"github.com/go-sod/sod/internal/dispatcher"
"github.com/go-sod/sod/internal/geom"
"github.com/go-sod/sod/internal/httputil"
"github.com/go-sod/sod/internal/logging"
"github.com/go-sod/sod/internal/metric/model"
)

const maxBodyBytes = 64 * 1024 * 1024
Expand Down
Loading

0 comments on commit 4b6b799

Please sign in to comment.