Fixing formatting

This commit is contained in:
Pavel Shevaev 2022-11-15 17:53:34 +03:00
parent 5146eb7c3b
commit 564cd5cbbe
1 changed files with 32 additions and 34 deletions

View File

@ -44,42 +44,42 @@ type RPCResult struct {
} }
type RPCServer struct { type RPCServer struct {
isDev bool isDev bool
versionHeader string versionHeader string
gzipLenThreshold int gzipLenThreshold int
logger logr.Logger logger logr.Logger
newHandler func(ctx *RPCCtx) IRPCHandler newHandler func(ctx *RPCCtx) IRPCHandler
createRPC func(code uint32)(meta.IRPC, error) createRPC func(code uint32) (meta.IRPC, error)
} }
type IRPCHandler interface { type IRPCHandler interface {
BeforeExecute(p meta.IRPC) error BeforeExecute(p meta.IRPC) error
AfterExecute(p meta.IRPC, err error) error AfterExecute(p meta.IRPC, err error) error
Close() Close()
} }
type RPCCtx struct { type RPCCtx struct {
Ctx context.Context Ctx context.Context
Log logr.Logger Log logr.Logger
ClientVersion string ClientVersion string
ClientIP net.IP ClientIP net.IP
ClientPlatform int ClientPlatform int
IsDev bool IsDev bool
} }
func NewRPCServer(logger logr.Logger, func NewRPCServer(logger logr.Logger,
isDev bool, isDev bool,
versionHeader string, versionHeader string,
gzipLenThreshold int, gzipLenThreshold int,
newHandler func(ctx *RPCCtx) IRPCHandler, newHandler func(ctx *RPCCtx) IRPCHandler,
createRPC func(code uint32)(meta.IRPC, error)) *RPCServer { createRPC func(code uint32) (meta.IRPC, error)) *RPCServer {
return &RPCServer{ return &RPCServer{
isDev: isDev, isDev: isDev,
versionHeader: versionHeader, versionHeader: versionHeader,
gzipLenThreshold : gzipLenThreshold, gzipLenThreshold: gzipLenThreshold,
logger: logger.WithName("[rpc]"), logger: logger.WithName("[rpc]"),
newHandler: newHandler, newHandler: newHandler,
createRPC: createRPC} createRPC: createRPC}
} }
func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -98,7 +98,7 @@ func (server *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
logger := server.logger logger := server.logger
if server.isDev { if server.isDev {
reqId := uuid.New() reqId := uuid.New()
logger = logger.WithValues("uid", reqId.String()) logger = logger.WithValues("uid", reqId.String())
@ -127,7 +127,6 @@ func GetIPStr(r *http.Request) string {
return ra return ra
} }
func (server *RPCServer) writeResponse(canGzip bool, w http.ResponseWriter, payload []byte) { func (server *RPCServer) writeResponse(canGzip bool, w http.ResponseWriter, payload []byte) {
if len(payload) == 0 { if len(payload) == 0 {
server.logger.Error(nil, "Nothing was written to response!") server.logger.Error(nil, "Nothing was written to response!")
@ -165,12 +164,12 @@ func (w *respWriterWithLen) Write(b []byte) (n int, err error) {
return return
} }
func (server *RPCServer) serveRequest(context context.Context, func (server *RPCServer) serveRequest(context context.Context,
logger logr.Logger, logger logr.Logger,
request []byte, request []byte,
clientVersion string, clientVersion string,
clientPlatform int, clientPlatform int,
clientIP net.IP) (result RPCResult) { clientIP net.IP) (result RPCResult) {
result = RPCResult{} result = RPCResult{}
@ -188,8 +187,8 @@ func (server *RPCServer) serveRequest(context context.Context,
} }
}() }()
h := server.newHandler(&RPCCtx{context, logger, clientVersion, clientIP, clientPlatform, server.isDev}) h := server.newHandler(&RPCCtx{context, logger, clientVersion, clientIP, clientPlatform, server.isDev})
defer h.Close() defer h.Close()
p, err := server.executeRPC(h, request) p, err := server.executeRPC(h, request)
@ -206,12 +205,12 @@ func (server *RPCServer) executeRPC(h IRPCHandler, request []byte) (meta.IRPC, e
} }
if err := h.BeforeExecute(p); err != nil { if err := h.BeforeExecute(p); err != nil {
err = h.AfterExecute(p, err) err = h.AfterExecute(p, err)
//NOTE: returning constructed packet so that it can be inspected //NOTE: returning constructed packet so that it can be inspected
return p, err return p, err
} }
err = p.Execute(h) err = p.Execute(h)
err = h.AfterExecute(p, err) err = h.AfterExecute(p, err)
if err != nil { if err != nil {
return p, err return p, err
@ -289,4 +288,3 @@ func parseVersionHeader(header string) (string, int) {
return cltVersion, cltPlatform return cltVersion, cltPlatform
} }