MSP-8995 up golangci-lint config and fix lint issues
This commit is contained in:
1 parent
80d9286498
commit
5191f45076
9 files changed
+62
-31
No files matched your search
+7
-10
@@ -15,13 +15,17 @@ linters:
|
||||
- nolintlint # Goland все равно форматирует с отступом
|
||||
- perfsprint # мнимая производительность в угоду читаемости
|
||||
- tagalign # не всегда имеет смысл
|
||||
- testpackage
|
||||
- varnamelen # очень индивидуально (db)
|
||||
- wsl # расставление пробелов очень индивидуально
|
||||
- wsl_v5 # расставление пробелов очень индивидуально
|
||||
- godoclint # ругается на // nolint, не удобно
|
||||
- prealloc # слишком аггресивный, в большинстве случаев не требуется
|
||||
- gomodguard # deprecated
|
||||
- goconst # слишком агрессивный
|
||||
settings:
|
||||
exhaustruct_v5:
|
||||
allow-empty: true
|
||||
explicit-mode: true
|
||||
cyclop:
|
||||
max-complexity: 15 # по умолчанию 10 мало
|
||||
funlen:
|
||||
@@ -62,15 +66,8 @@ linters:
|
||||
- prealloc
|
||||
path: ".*_test\\.go$"
|
||||
- linters:
|
||||
- wrapcheck
|
||||
path: middleware #отсутствует необходимость оборачивать ошибки
|
||||
- linters:
|
||||
- gochecknoglobals
|
||||
- mnd
|
||||
path: proxy
|
||||
- linters:
|
||||
- wrapcheck
|
||||
path: httperrors #отсутствует необходимость оборачивать ошибки
|
||||
- goconst
|
||||
path: "tests/" # компонентные тесты: фикстуры и моки не выносим в константы
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
|
||||
@@ -3,6 +3,7 @@ package httperrors
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
)
|
||||
|
||||
@@ -33,7 +34,11 @@ func (e *HttpError) WriteError(w http.ResponseWriter) error {
|
||||
"errorMessage": e.userMessage,
|
||||
"details": e.details,
|
||||
}
|
||||
return json.NewEncoder(w).Encode(data)
|
||||
err := json.NewEncoder(w).Encode(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "encode HTTP error response")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *HttpError) WithDetails(details ...any) {
|
||||
|
||||
@@ -35,7 +35,11 @@ func (w *writerWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if !ok {
|
||||
return nil, nil, errors.New("writerWrapper: upstream writer doesn't implement Hijack")
|
||||
}
|
||||
return upstream.Hijack()
|
||||
conn, readWriter, err := upstream.Hijack()
|
||||
if err != nil {
|
||||
return conn, readWriter, errors.WithMessage(err, "writerWrapper: hijack upstream writer")
|
||||
}
|
||||
return conn, readWriter, nil
|
||||
}
|
||||
|
||||
func (w *writerWrapper) StatusCode() int {
|
||||
@@ -136,7 +140,10 @@ func Logger( // nolint:gocognit,funlen
|
||||
logger.Debug(ctx.Context(), "log request", fields...)
|
||||
}
|
||||
|
||||
return err
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "logger: handle request")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"isp-gate-service/request"
|
||||
"time"
|
||||
|
||||
"isp-gate-service/request"
|
||||
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/metrics/http_metrics"
|
||||
)
|
||||
|
||||
@@ -20,7 +22,7 @@ func Metrics(storage *http_metrics.ServerStorage) Middleware {
|
||||
start := time.Now()
|
||||
err := next.Handle(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
return errors.WithMessage(err, "metrics: handle request")
|
||||
}
|
||||
|
||||
storage.ObserveDuration(r.Method, pathSchema, time.Since(start))
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
@@ -18,8 +20,8 @@ type Middleware func(next Handler) Handler
|
||||
|
||||
// nolint:ireturn
|
||||
func Chain(root Handler, middlewares ...Middleware) Handler {
|
||||
for i := len(middlewares) - 1; i >= 0; i-- {
|
||||
root = middlewares[i](root)
|
||||
for _, middleware := range slices.Backward(middlewares) {
|
||||
root = middleware(root)
|
||||
}
|
||||
return root
|
||||
}
|
||||
@@ -32,6 +32,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
codeMap = map[int]codes.Code{
|
||||
http.StatusOK: codes.OK,
|
||||
|
||||
+21
-10
@@ -20,21 +20,30 @@ import (
|
||||
"github.com/txix-open/isp-kit/requestid"
|
||||
)
|
||||
|
||||
var (
|
||||
httpTransport = &http.Transport{
|
||||
const (
|
||||
httpDialTimeout = 3 * time.Second
|
||||
httpDialKeepAlive = 30 * time.Second
|
||||
httpMaxIdleConns = 512
|
||||
httpMaxIdleConnsPerHost = 32
|
||||
httpIdleConnTimeout = 60 * time.Second
|
||||
httpTLSHandshakeTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
func newHttpTransport() *http.Transport {
|
||||
return &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: defaultTransportDialContext(&net.Dialer{
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
Timeout: httpDialTimeout,
|
||||
KeepAlive: httpDialKeepAlive,
|
||||
}),
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: 512,
|
||||
MaxIdleConnsPerHost: 32,
|
||||
IdleConnTimeout: 60 * time.Second,
|
||||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
MaxIdleConns: httpMaxIdleConns,
|
||||
MaxIdleConnsPerHost: httpMaxIdleConnsPerHost,
|
||||
IdleConnTimeout: httpIdleConnTimeout,
|
||||
TLSHandshakeTimeout: httpTLSHandshakeTimeout,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
type HttpHostManager interface {
|
||||
Next() (string, error)
|
||||
@@ -42,6 +51,7 @@ type HttpHostManager interface {
|
||||
|
||||
type Http struct {
|
||||
hostManager HttpHostManager
|
||||
transport http.RoundTripper
|
||||
skipAuth bool
|
||||
timeout time.Duration
|
||||
}
|
||||
@@ -49,6 +59,7 @@ type Http struct {
|
||||
func NewHttp(hostManager HttpHostManager, skipAuth bool, timeout time.Duration) Http {
|
||||
return Http{
|
||||
hostManager: hostManager,
|
||||
transport: newHttpTransport(),
|
||||
skipAuth: skipAuth,
|
||||
timeout: timeout,
|
||||
}
|
||||
@@ -71,7 +82,7 @@ func (p Http) Handle(ctx *request.Context) error {
|
||||
setHttpHeaders(ctx, request.Header, p.skipAuth)
|
||||
|
||||
reverseProxy := httputil.NewSingleHostReverseProxy(target)
|
||||
reverseProxy.Transport = httpTransport
|
||||
reverseProxy.Transport = p.transport
|
||||
var resultError error
|
||||
reverseProxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
|
||||
resultError = httperrors.New(
|
||||
|
||||
+9
-3
@@ -13,6 +13,12 @@ import (
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
const (
|
||||
wsHandshakeTimeout = 5 * time.Second
|
||||
wsReadBufferSize = 1024
|
||||
wsWriteBufferSize = 1024
|
||||
)
|
||||
|
||||
type Ws struct {
|
||||
hostManager HttpHostManager
|
||||
skipAuth bool
|
||||
@@ -46,9 +52,9 @@ func (ws Ws) Handle(ctx *request.Context) error {
|
||||
setHttpHeaders(ctx, out, ws.skipAuth)
|
||||
}
|
||||
proxy.Upgrader = &websocket.Upgrader{
|
||||
HandshakeTimeout: 5 * time.Second,
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
HandshakeTimeout: wsHandshakeTimeout,
|
||||
ReadBufferSize: wsReadBufferSize,
|
||||
WriteBufferSize: wsWriteBufferSize,
|
||||
WriteBufferPool: nil,
|
||||
Subprotocols: nil,
|
||||
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// nolint:canonicalheader
|
||||
package tests
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
Reference in new issue
Block a user