Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ jobs:
name: Lint
strategy:
matrix:
go: [ '1.20' ]
go: [ '1.24' ]
fail-fast: true
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go }}
uses: actions/setup-go@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: false
cache: true

- name: Run GolangCI-Lint
uses: golangci/golangci-lint-action@v6
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v7
with:
version: v1.62.2
version: v2.5.0
args: --timeout=5m
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ jobs:
name: Test
strategy:
matrix:
go: [ '1.20' ]
go: [ '1.24' ]
os: [ ubuntu-latest ]
fail-fast: true
runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup Go ${{ matrix.go }}
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
cache: false
Expand Down
127 changes: 67 additions & 60 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,84 +1,91 @@
linters-settings:
gocyclo:
min-complexity: 15
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- diagnostic
- performance
- style
- experimental
disabled-checks:
- whyNoLint
- unnecessaryDefer
- paramTypeCombine
settings:
hugeParam:
sizeThreshold: 256
rangeValCopy:
sizeThreshold: 256
funlen:
lines: 100
statements: 50
revive:
rules:
- name: "unused-parameter"
disabled: true
version: "2"

formatters:
enable:
- goimports
settings:
goimports:
local-prefixes:
- github.com/acronis/go-dbkit/

linters:
disable-all: true
settings:
revive:
rules:
- name: exported
disabled: true
- name: package-comments
disabled: true
gocyclo:
min-complexity: 15
goconst:
min-len: 2
min-occurrences: 2
govet:
settings:
printf:
funcs:
- (github.com/acronis/go-appkit/log.FieldLogger).Infof
- (github.com/acronis/go-appkit/log.FieldLogger).Warnf
- (github.com/acronis/go-appkit/log.FieldLogger).Errorf
- (github.com/acronis/go-appkit/log.FieldLogger).Debugf
misspell:
locale: US
lll:
line-length: 140
gocritic:
enabled-tags:
- diagnostic
- performance
- style
- experimental
disabled-checks:
- paramTypeCombine
- rangeValCopy
settings:
hugeParam:
sizeThreshold: 256
funlen:
lines: 100
statements: 50

enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- funlen
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

issues:
# Don't use default excluding to be sure all exported things (method, functions, consts and so on) have comments.
exclude-use-default: false
exclude-rules:
- path: _test\.go
linters:
- dogsled
- dupl
- errcheck
- funlen
- goconst
- gocritic
- gocyclo
- gosec
- govet
- lll
- ineffassign
- noctx
- unused
exclusions:
rules:
- path: '(.+)_test\.go'
linters:
- dogsled
- dupl
- errcheck
- funlen
- goconst
- gocritic
- gocyclo
- gosec
- govet
- lll
- ineffassign
- noctx
- unparam
- unused
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
cfgKeyMySQLPort = "mysql.port"
cfgKeyMySQLDatabase = "mysql.database"
cfgKeyMySQLUser = "mysql.user"
cfgKeyMySQLPassword = "mysql.password" //nolint: gosec
cfgKeyMySQLPassword = "mysql.password" //nolint:gosec // Not a hardcoded password, just a config key
cfgKeyMySQLTxLevel = "mysql.txLevel"

cfgKeySQLitePath = "sqlite3.path"
Expand All @@ -38,7 +38,7 @@ const (
cfgKeyPostgresPort = "postgres.port"
cfgKeyPostgresDatabase = "postgres.database"
cfgKeyPostgresUser = "postgres.user"
cfgKeyPostgresPassword = "postgres.password" //nolint: gosec
cfgKeyPostgresPassword = "postgres.password" //nolint:gosec // Not a hardcoded password, just a config key
cfgKeyPostgresTxLevel = "postgres.txLevel"
cfgKeyPostgresSSLMode = "postgres.sslMode"
cfgKeyPostgresSearchPath = "postgres.searchPath"
Expand All @@ -47,7 +47,7 @@ const (
cfgKeyMSSQLPort = "mssql.port"
cfgKeyMSSQLDatabase = "mssql.database"
cfgKeyMSSQLUser = "mssql.user"
cfgKeyMSSQLPassword = "mssql.password" //nolint: gosec
cfgKeyMSSQLPassword = "mssql.password" //nolint:gosec // Not a hardcoded password, just a config key
cfgKeyMSSQLTxLevel = "mssql.txLevel"
cfgKeyMSSQLAdditionalParams = "mssql.additionalParameters"
)
Expand Down Expand Up @@ -291,7 +291,7 @@ func (c *Config) setDialectSpecificConfig(dp config.DataProvider) error {
return err
}

// nolint: dupl
//nolint:dupl // Similar config setters for different database dialects
func (c *Config) setMySQLConfig(dp config.DataProvider) error {
var err error

Expand All @@ -317,7 +317,7 @@ func (c *Config) setMySQLConfig(dp config.DataProvider) error {
return nil
}

// nolint: dupl
//nolint:dupl // Similar config setters for different database dialects
func (c *Config) setMSSQLConfig(dp config.DataProvider) error {
var err error

Expand Down Expand Up @@ -350,7 +350,7 @@ func (c *Config) setMSSQLConfig(dp config.DataProvider) error {
return nil
}

// nolint: dupl
//nolint:dupl // Similar config setters for different database dialects
func (c *Config) setPostgresConfig(dp config.DataProvider, dialect Dialect) error {
var err error

Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"

"github.com/acronis/go-appkit/config"
"github.com/mitchellh/mapstructure"
"github.com/go-viper/mapstructure/v2"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
Expand Down
6 changes: 3 additions & 3 deletions dbrutil/dbrutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (s *TxSession) BeginTx(ctx context.Context) (*dbr.Tx, error) {
// DoInTx begins a new transaction, calls passed function and do commit or rollback
// depending on whether the function returns an error or not.
func (s *TxSession) DoInTx(ctx context.Context, fn func(runner dbr.SessionRunner) error) error {
if s.Connection.Dialect == dialect.SQLite3 {
if s.Dialect == dialect.SQLite3 {
// race of ctx cancel with transaction begin leads to 'cannot start a transaction within a transaction'
// https://github.com/mattn/go-sqlite3/pull/765
ctx = context.TODO()
Expand Down Expand Up @@ -210,9 +210,9 @@ func ParseAnnotationInQuery(query, prefix string, modifier func(string) string)
}
if annotation != "" {
if buf.Len() != 0 {
buf.WriteString("|") // nolint: gosec
buf.WriteString("|") //nolint:gosec // bytes.Buffer.WriteString never returns an error
}
buf.WriteString(annotation) // nolint: gosec
buf.WriteString(annotation) //nolint:gosec // bytes.Buffer.WriteString never returns an error
}
}
left = right + 2
Expand Down
5 changes: 3 additions & 2 deletions dbrutil/examples/dbr-instrumentation-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
if err != nil {
stdlog.Fatal(err)
}
defer conn.Close()
defer func() { _ = conn.Close() }()

txRunner := dbrutil.NewTxRunner(conn, &sql.TxOptions{Isolation: sql.LevelReadCommitted}, nil)

Expand All @@ -56,7 +56,8 @@ func main() {
}

// The following log message will be printed:
// {"level":"warn","time":"2025-02-14T16:29:55.429257+02:00","msg":"slow SQL query","pid":14030,"annotation":"query:long_operation","duration_ms":1007}
// {"level":"warn","time":"2025-02-14T16:29:55.429257+02:00","msg":"slow SQL query",
// "pid":14030,"annotation":"query:long_operation","duration_ms":1007}

// Prometheus metrics will be collected:
// db_query_duration_seconds_bucket{query="query:long_operation",le="2.5"} 1
Expand Down
11 changes: 9 additions & 2 deletions dbrutil/examples/dbr-instrumentation-2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func main() {
if err != nil {
stdlog.Fatal(err)
}
defer conn.Close()
defer func() { _ = conn.Close() }()

// Construct the middleware that will put the transaction runner into the request context.
var txRunnerOpts dbrutil.TxRunnerMiddlewareOpts
Expand Down Expand Up @@ -77,7 +77,14 @@ func main() {
mux := http.NewServeMux()
mux.Handle("/long-operation", h)
mux.Handle("/metrics", promhttp.Handler())
if srvErr := http.ListenAndServe(":8080", mux); srvErr != nil && errors.Is(srvErr, http.ErrServerClosed) {
srv := &http.Server{
Addr: ":8080",
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
}
if srvErr := srv.ListenAndServe(); srvErr != nil && errors.Is(srvErr, http.ErrServerClosed) {
stdlog.Fatalf("failed to start server: %v", srvErr)
}
}
Expand Down
4 changes: 2 additions & 2 deletions distrlock/db_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ type SQLExecutor interface {

const createTableMigrationID = "distrlock_00001_create_table"

//nolint:lll
//nolint:lll // SQL queries are more readable on single lines
const (
postgresCreateTableQuery = `CREATE TABLE IF NOT EXISTS "%s" (lock_key varchar(40) PRIMARY KEY, token uuid, expire_at timestamp);`
postgresDropTableQuery = `DROP TABLE IF EXISTS "%s";`
Expand All @@ -395,7 +395,7 @@ func postgresMakeInterval(interval time.Duration) string {
return strconv.FormatInt(interval.Microseconds(), 10) + " microseconds"
}

//nolint:lll
//nolint:lll // SQL queries are more readable on single lines
const (
mySQLCreateTableQuery = "CREATE TABLE IF NOT EXISTS `%s` (lock_key VARCHAR(40) PRIMARY KEY, token VARCHAR(36), expire_at BIGINT);"
mySQLDropTableQuery = "DROP TABLE IF EXISTS `%s`;"
Expand Down
1 change: 1 addition & 0 deletions distrlock/db_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ func assertRollbackWithCtxTimeoutError(t *gotesting.T, dialect dbkit.Dialect, tx
case dbkit.DialectMySQL:
ok = assert.True(t, errors.Is(rollbackErr, sql.ErrTxDone) ||
errors.Is(rollbackErr, mysql.ErrInvalidConn) ||
errors.Is(rollbackErr, context.DeadlineExceeded) ||
rollbackErr == nil, // Rollback sometimes can return nil error in case of mysql driver .
)
case dbkit.DialectPostgres:
Expand Down
Loading