optimize allocations

This commit is contained in:
Maxim Merzhanov committed 2020-03-03 17:59:51 +03:00
1 parent 6d67d3a331
commit 7e10fb5217
4 files changed
+38 -24

No files matched your search

+2 -2
View File
@@ -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() {
+10 -8
View File
@@ -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
+14 -6
View File
@@ -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 {
+12 -8
View File
@@ -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
}