From 7e10fb52173ec8be95734cba519dc16e478f9a1b Mon Sep 17 00:00:00 2001 From: Maxim Merzhanov Date: Tue, 3 Mar 2020 17:59:51 +0300 Subject: [PATCH] optimize allocations --- main.go | 4 ++-- proxy/grpc/handlers/common.go | 18 ++++++++++-------- proxy/websocket/websocket.go | 20 ++++++++++++++------ service/veritification/runtime_verify.go | 20 ++++++++++++-------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index 46cbac1..cb79979 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,8 @@ package main import ( "context" + "os" + "github.com/integration-system/isp-lib/bootstrap" "github.com/integration-system/isp-lib/config" "github.com/integration-system/isp-lib/config/schema" @@ -20,12 +22,10 @@ import ( "isp-gate-service/server" "isp-gate-service/service" "isp-gate-service/service/matcher" - "os" ) var ( version = "0.1.0" - date = "undefined" ) func main() { diff --git a/proxy/grpc/handlers/common.go b/proxy/grpc/handlers/common.go index cdc1f0e..d31f5bc 100644 --- a/proxy/grpc/handlers/common.go +++ b/proxy/grpc/handlers/common.go @@ -1,6 +1,11 @@ package handlers import ( + "bytes" + "io" + "net/http" + "time" + "github.com/golang/protobuf/proto" structpb "github.com/golang/protobuf/ptypes/struct" "github.com/integration-system/isp-lib/backend" @@ -16,14 +21,10 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" - "io" "isp-gate-service/conf" "isp-gate-service/domain" "isp-gate-service/log_code" utils2 "isp-gate-service/utils" - "net/http" - "strings" - "time" ) const ( @@ -129,11 +130,12 @@ func openStream(headers *fasthttp.RequestHeader, method string, timeout time.Dur func makeMetadata(r *fasthttp.RequestHeader, method string) (metadata.MD, string) { //method = strings.TrimPrefix(method, "/api/") - md := metadata.Pairs(utils.ProxyMethodNameHeader, method) + md := make(metadata.MD, 5) + md[utils.ProxyMethodNameHeader] = []string{method} r.VisitAll(func(key, v []byte) { - lowerHeader := strings.ToLower(string(key)) - if len(v) > 0 && strings.HasPrefix(lowerHeader, "x-") { - md = metadata.Join(md, metadata.Pairs(lowerHeader, string(v))) + lowerHeader := bytes.ToLower(key) + if len(v) > 0 && bytes.HasPrefix(lowerHeader, []byte("x-")) { + md[string(lowerHeader)] = []string{string(v)} } }) return md, method diff --git a/proxy/websocket/websocket.go b/proxy/websocket/websocket.go index e6568a2..9922a0d 100644 --- a/proxy/websocket/websocket.go +++ b/proxy/websocket/websocket.go @@ -2,19 +2,21 @@ package websocket import ( "errors" + "io" + "net" + "net/http" + "net/http/cookiejar" + "net/url" + "sync" + "github.com/fasthttp/websocket" "github.com/integration-system/isp-lib/structure" log "github.com/integration-system/isp-log" "github.com/valyala/fasthttp" "google.golang.org/grpc/codes" - "io" "isp-gate-service/domain" "isp-gate-service/log_code" "isp-gate-service/utils" - "net" - "net/http" - "net/http/cookiejar" - "net/url" ) const ( @@ -22,6 +24,10 @@ const ( readBufSize = 4 << 10 ) +var pool = &sync.Pool{New: func() interface{} { + return make([]byte, readBufSize) +}} + // used to filter client request headers var forbiddenDuplicateHeaders = map[string]struct{}{ "Upgrade": {}, @@ -139,9 +145,11 @@ func (p *websocketProxy) Close() { } func (p *websocketProxy) proxyConn(from, to *websocket.Conn) error { + buf := pool.Get().([]byte) defer func() { _ = from.Close() _ = to.Close() + pool.Put(buf) }() for { msgType, reader, err := from.NextReader() @@ -152,7 +160,7 @@ func (p *websocketProxy) proxyConn(from, to *websocket.Conn) error { if err != nil { return err } - if _, err := io.Copy(writer, reader); err != nil { + if _, err := io.CopyBuffer(writer, reader, buf); err != nil { return err } if err := writer.Close(); err != nil { diff --git a/service/veritification/runtime_verify.go b/service/veritification/runtime_verify.go index f8847a2..9636cc2 100644 --- a/service/veritification/runtime_verify.go +++ b/service/veritification/runtime_verify.go @@ -1,7 +1,6 @@ package veritification import ( - "fmt" rd "github.com/go-redis/redis" "github.com/integration-system/isp-lib/config" "github.com/integration-system/isp-lib/redis" @@ -24,7 +23,7 @@ type runtimeVerify struct{} func (v *runtimeVerify) ApplicationToken(token string) (map[string]string, error) { instanceUuid := config.Get().(*conf.Configuration).InstanceUuid - key := fmt.Sprintf("%s|%s", token, instanceUuid) + key := makeDbKey(token, instanceUuid) if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error { if cmd := p.Select(int(redis.ApplicationTokenDb)); v.notEmptyError(cmd.Err()) { @@ -44,8 +43,9 @@ func (v *runtimeVerify) ApplicationToken(token string) (map[string]string, error } else if stringStringMapCmd, ok := resp[1].(*rd.StringStringMapCmd); !ok { return nil, nil } else { - identityMap := make(map[string]string) - for i, value := range stringStringMapCmd.Val() { + vals := stringStringMapCmd.Val() + identityMap := make(map[string]string, len(vals)) + for i, value := range vals { identityMap[headerKeyByRedisIdentity[i]] = value } return identityMap, nil @@ -54,10 +54,10 @@ func (v *runtimeVerify) ApplicationToken(token string) (map[string]string, error } func (v *runtimeVerify) Identity(t map[string]string, uri string) (map[string]string, error) { - secondDbKey := fmt.Sprintf("%s|%s", t[utils.ApplicationIdHeader], uri) - thirdDbKey := fmt.Sprintf("%s|%s", t[utils.UserTokenHeader], t[utils.DomainIdHeader]) - fourthDbKey := fmt.Sprintf("%s|%s", t[utils.UserIdHeader], uri) - fifthDbKey := fmt.Sprintf("%s|%s", t[utils.DeviceTokenHeader], t[utils.DomainIdHeader]) + secondDbKey := makeDbKey(t[utils.ApplicationIdHeader], uri) + thirdDbKey := makeDbKey(t[utils.UserTokenHeader], t[utils.DomainIdHeader]) + fourthDbKey := makeDbKey(t[utils.UserIdHeader], uri) + fifthDbKey := makeDbKey(t[utils.DeviceTokenHeader], t[utils.DomainIdHeader]) if resp, err := rdClient.Client.Get().Pipelined(func(p rd.Pipeliner) error { if _, err := p.Select(int(redis.ApplicationPermissionDb)).Result(); v.notEmptyError(err) { @@ -153,3 +153,7 @@ func (v *runtimeVerify) findStringCmd(cmders []rd.Cmder, arrayKey int) (string, return "", errors.New("not found cmd") } } + +func makeDbKey(key, val string) string { + return key + "|" + val +}