MSP-8995 fix review comments
This commit is contained in:
1 parent
5fef05516b
commit
458f94cfd6
12 files changed
+28
-89
No files matched your search
@@ -65,6 +65,16 @@ linters:
|
||||
- unqueryvet
|
||||
- prealloc
|
||||
path: ".*_test\\.go$"
|
||||
- linters:
|
||||
- wrapcheck
|
||||
path: middleware #отсутствует необходимость оборачивать ошибки
|
||||
- linters:
|
||||
- gochecknoglobals
|
||||
- mnd
|
||||
path: proxy
|
||||
- linters:
|
||||
- wrapcheck
|
||||
path: httperrors #отсутствует необходимость оборачивать ошибки
|
||||
- linters:
|
||||
- goconst
|
||||
path: "tests/" # компонентные тесты: фикстуры и моки не выносим в константы
|
||||
|
||||
@@ -8,9 +8,6 @@ type AuthenticateAppResponse struct {
|
||||
|
||||
type AppAuthData struct {
|
||||
AppName string
|
||||
SystemId int
|
||||
DomainId int
|
||||
ServiceId int
|
||||
ApplicationId int
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,5 @@ package entity
|
||||
|
||||
type AppAuthData struct {
|
||||
AppName string
|
||||
SystemId int
|
||||
DomainId int
|
||||
ServiceId int
|
||||
ApplicationId int
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package httperrors
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/txix-open/isp-kit/errors"
|
||||
"github.com/txix-open/isp-kit/json"
|
||||
)
|
||||
|
||||
@@ -34,11 +33,7 @@ func (e *HttpError) WriteError(w http.ResponseWriter) error {
|
||||
"errorMessage": e.userMessage,
|
||||
"details": e.details,
|
||||
}
|
||||
err := json.NewEncoder(w).Encode(data)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "encode HTTP error response")
|
||||
}
|
||||
return nil
|
||||
return json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
func (e *HttpError) WithDetails(details ...any) {
|
||||
|
||||
@@ -35,11 +35,7 @@ func (w *writerWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
||||
if !ok {
|
||||
return nil, nil, errors.New("writerWrapper: upstream writer doesn't implement Hijack")
|
||||
}
|
||||
conn, readWriter, err := upstream.Hijack()
|
||||
if err != nil {
|
||||
return conn, readWriter, errors.WithMessage(err, "writerWrapper: hijack upstream writer")
|
||||
}
|
||||
return conn, readWriter, nil
|
||||
return upstream.Hijack()
|
||||
}
|
||||
|
||||
func (w *writerWrapper) StatusCode() int {
|
||||
@@ -140,10 +136,7 @@ func Logger( // nolint:gocognit,funlen
|
||||
logger.Debug(ctx.Context(), "log request", fields...)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "logger: handle request")
|
||||
}
|
||||
return nil
|
||||
return err
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
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"
|
||||
)
|
||||
|
||||
@@ -22,7 +20,7 @@ func Metrics(storage *http_metrics.ServerStorage) Middleware {
|
||||
start := time.Now()
|
||||
err := next.Handle(ctx)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "metrics: handle request")
|
||||
return err
|
||||
}
|
||||
|
||||
storage.ObserveDuration(r.Method, pathSchema, time.Since(start))
|
||||
|
||||
@@ -32,7 +32,6 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gochecknoglobals
|
||||
var (
|
||||
codeMap = map[int]codes.Code{
|
||||
http.StatusOK: codes.OK,
|
||||
@@ -165,9 +164,6 @@ func (p Grpc) writeMetadata(ctx *request.Context) metadata.MD {
|
||||
|
||||
appAuthData, err := ctx.GetAuthData()
|
||||
if err == nil {
|
||||
md[grpc.SystemIdHeader] = []string{strconv.Itoa(appAuthData.SystemId)}
|
||||
md[grpc.DomainIdHeader] = []string{strconv.Itoa(appAuthData.DomainId)}
|
||||
md[grpc.ServiceIdHeader] = []string{strconv.Itoa(appAuthData.ServiceId)}
|
||||
md[grpc.ApplicationIdHeader] = []string{strconv.Itoa(appAuthData.ApplicationId)}
|
||||
encodedAppName := base64.StdEncoding.EncodeToString([]byte(appAuthData.AppName))
|
||||
md[grpc.ApplicationNameHeader] = []string{encodedAppName}
|
||||
|
||||
+10
-24
@@ -20,30 +20,21 @@ import (
|
||||
"github.com/txix-open/isp-kit/requestid"
|
||||
)
|
||||
|
||||
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{
|
||||
var (
|
||||
httpTransport = &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DialContext: defaultTransportDialContext(&net.Dialer{
|
||||
Timeout: httpDialTimeout,
|
||||
KeepAlive: httpDialKeepAlive,
|
||||
Timeout: 3 * time.Second,
|
||||
KeepAlive: 30 * time.Second,
|
||||
}),
|
||||
ForceAttemptHTTP2: true,
|
||||
MaxIdleConns: httpMaxIdleConns,
|
||||
MaxIdleConnsPerHost: httpMaxIdleConnsPerHost,
|
||||
IdleConnTimeout: httpIdleConnTimeout,
|
||||
TLSHandshakeTimeout: httpTLSHandshakeTimeout,
|
||||
MaxIdleConns: 512,
|
||||
MaxIdleConnsPerHost: 32,
|
||||
IdleConnTimeout: 60 * time.Second,
|
||||
TLSHandshakeTimeout: 5 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
type HttpHostManager interface {
|
||||
Next() (string, error)
|
||||
@@ -51,7 +42,6 @@ type HttpHostManager interface {
|
||||
|
||||
type Http struct {
|
||||
hostManager HttpHostManager
|
||||
transport http.RoundTripper
|
||||
skipAuth bool
|
||||
timeout time.Duration
|
||||
}
|
||||
@@ -59,7 +49,6 @@ type Http struct {
|
||||
func NewHttp(hostManager HttpHostManager, skipAuth bool, timeout time.Duration) Http {
|
||||
return Http{
|
||||
hostManager: hostManager,
|
||||
transport: newHttpTransport(),
|
||||
skipAuth: skipAuth,
|
||||
timeout: timeout,
|
||||
}
|
||||
@@ -82,7 +71,7 @@ func (p Http) Handle(ctx *request.Context) error {
|
||||
setHttpHeaders(ctx, request.Header, p.skipAuth)
|
||||
|
||||
reverseProxy := httputil.NewSingleHostReverseProxy(target)
|
||||
reverseProxy.Transport = p.transport
|
||||
reverseProxy.Transport = httpTransport
|
||||
var resultError error
|
||||
reverseProxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
|
||||
resultError = httperrors.New(
|
||||
@@ -121,9 +110,6 @@ func setHttpHeaders(ctx *request.Context, header http.Header, skipAuth bool) {
|
||||
|
||||
appAuthData, err := ctx.GetAuthData()
|
||||
if err == nil {
|
||||
header.Set(grpc.SystemIdHeader, strconv.Itoa(appAuthData.SystemId))
|
||||
header.Set(grpc.DomainIdHeader, strconv.Itoa(appAuthData.DomainId))
|
||||
header.Set(grpc.ServiceIdHeader, strconv.Itoa(appAuthData.ServiceId))
|
||||
header.Set(grpc.ApplicationIdHeader, strconv.Itoa(appAuthData.ApplicationId))
|
||||
encodedAppName := base64.StdEncoding.EncodeToString([]byte(appAuthData.AppName))
|
||||
header.Set(grpc.ApplicationNameHeader, encodedAppName) //nolint:canonicalheader
|
||||
|
||||
+3
-9
@@ -13,12 +13,6 @@ import (
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
const (
|
||||
wsHandshakeTimeout = 5 * time.Second
|
||||
wsReadBufferSize = 1024
|
||||
wsWriteBufferSize = 1024
|
||||
)
|
||||
|
||||
type Ws struct {
|
||||
hostManager HttpHostManager
|
||||
skipAuth bool
|
||||
@@ -52,9 +46,9 @@ func (ws Ws) Handle(ctx *request.Context) error {
|
||||
setHttpHeaders(ctx, out, ws.skipAuth)
|
||||
}
|
||||
proxy.Upgrader = &websocket.Upgrader{
|
||||
HandshakeTimeout: wsHandshakeTimeout,
|
||||
ReadBufferSize: wsReadBufferSize,
|
||||
WriteBufferSize: wsWriteBufferSize,
|
||||
HandshakeTimeout: 5 * time.Second,
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
WriteBufferPool: nil,
|
||||
Subprotocols: nil,
|
||||
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
||||
|
||||
@@ -74,9 +74,6 @@ func (Authentication) convertAuthData(authData *entity.AppAuthData) *domain.AppA
|
||||
|
||||
return &domain.AppAuthData{
|
||||
AppName: authData.AppName,
|
||||
SystemId: authData.SystemId,
|
||||
DomainId: authData.DomainId,
|
||||
ServiceId: authData.ServiceId,
|
||||
ApplicationId: authData.ApplicationId,
|
||||
}
|
||||
}
|
||||
@@ -60,18 +60,9 @@ func (s *HappyPathTestSuite) TestGrpcProxy() {
|
||||
requestId := requestid.Next()
|
||||
targetService, targetCli := grpct.NewMock(test)
|
||||
targetService.Mock("endpoint", func(ctx context.Context, authData grpc.AuthData, req request) response {
|
||||
systemId, err := authData.SystemId()
|
||||
require.NoError(err)
|
||||
domainId, err := authData.DomainId()
|
||||
require.NoError(err)
|
||||
serviceId, err := authData.ServiceId()
|
||||
require.NoError(err)
|
||||
applicationId, err := authData.ApplicationId()
|
||||
require.NoError(err)
|
||||
require.EqualValues(requestId, requestid.FromContext(ctx))
|
||||
require.EqualValues(1, systemId)
|
||||
require.EqualValues(2, domainId)
|
||||
require.EqualValues(3, serviceId)
|
||||
require.EqualValues(4, applicationId)
|
||||
|
||||
adminId, err := grpc.StringFromMd("x-admin-id", metadata.MD(authData))
|
||||
@@ -598,9 +589,6 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, *
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AuthData: &entity.AppAuthData{
|
||||
SystemId: 1,
|
||||
DomainId: 2,
|
||||
ServiceId: 3,
|
||||
ApplicationId: 4,
|
||||
AppName: "test",
|
||||
},
|
||||
@@ -640,18 +628,9 @@ func TestHappyPathTestSuite(t *testing.T) {
|
||||
}
|
||||
|
||||
func assertHeaders(require *require.Assertions, requestId string, ctx context.Context, headers http.Header) {
|
||||
systemId, err := strconv.Atoi(headers.Get("x-system-identity"))
|
||||
require.NoError(err)
|
||||
domainId, err := strconv.Atoi(headers.Get("x-domain-identity"))
|
||||
require.NoError(err)
|
||||
serviceId, err := strconv.Atoi(headers.Get("x-service-identity"))
|
||||
require.NoError(err)
|
||||
applicationId, err := strconv.Atoi(headers.Get("x-application-identity"))
|
||||
require.NoError(err)
|
||||
require.EqualValues(requestId, requestid.FromContext(ctx))
|
||||
require.EqualValues(1, systemId)
|
||||
require.EqualValues(2, domainId)
|
||||
require.EqualValues(3, serviceId)
|
||||
require.EqualValues(4, applicationId)
|
||||
expectedAppName := base64.StdEncoding.EncodeToString([]byte("test"))
|
||||
require.EqualValues(expectedAppName, headers.Get("x-application-name"))
|
||||
|
||||
@@ -66,9 +66,6 @@ func main() {
|
||||
Authenticated: true,
|
||||
ErrorReason: "",
|
||||
AuthData: &entity.AppAuthData{
|
||||
SystemId: 1,
|
||||
DomainId: 1,
|
||||
ServiceId: 1,
|
||||
ApplicationId: 1,
|
||||
},
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user