MSP-625 reimplement proxy

This commit is contained in:
DeniskaRediska authored and d1slike committed 2022-07-27 10:44:07 +03:00
1 parent ba2aeaf556
commit f2928ddcf7
8 files changed
+140 -389

No files matched your search

-121
View File
@@ -1,121 +0,0 @@
package proxy
import (
"isp-gate-service/conf"
"sort"
"strings"
"github.com/integration-system/isp-lib/v2/structure"
"github.com/pkg/errors"
"github.com/valyala/fasthttp"
"isp-gate-service/domain"
"isp-gate-service/proxy/grpc"
"isp-gate-service/proxy/health_check"
"isp-gate-service/proxy/http"
"isp-gate-service/proxy/websocket"
)
var (
store = make([]storeItem, 0)
)
const (
httpProtocol = "http"
websocketProtocol = "websocket"
grpcProtocol = "grpc"
healthCheckProtocol = "healthсheck"
)
type (
Proxy interface {
ProxyRequest(ctx *fasthttp.RequestCtx, path string) domain.ProxyResponse
Consumer([]structure.AddressConfiguration) bool
SkipAuth() bool
SkipExistCheck() bool
Close()
}
storeItem struct {
pathPrefix string
proxy Proxy
}
)
func InitProxies(locations []conf.Location) (map[string]func([]structure.AddressConfiguration) bool, error) {
locationsByTargetModule := conf.GetLocationsByTargetModule(locations)
requiredModules := make(map[string]func([]structure.AddressConfiguration) bool)
for targetModule, locations := range locationsByTargetModule {
consumerStorage := make([]func([]structure.AddressConfiguration) bool, len(locations))
for i, location := range locations {
if location.PathPrefix[0] != '/' {
return nil, errors.Errorf("path must begin with '/' in path '%s'", location.PathPrefix)
}
p, err := makeProxy(location.Protocol, location.SkipAuth, location.SkipExistCheck)
if err != nil {
return nil, err
}
store = append(store, storeItem{
pathPrefix: location.PathPrefix,
proxy: p,
})
consumerStorage[i] = p.Consumer
}
requiredModules[targetModule] = aggregateConsumers(consumerStorage...)
}
sort.Slice(store, func(i, j int) bool {
return store[i].pathPrefix > store[j].pathPrefix
})
return requiredModules, nil
}
func makeProxy(protocol string, skipAuth, skipExistCheck bool) (Proxy, error) {
var proxy Proxy
switch protocol {
case httpProtocol:
proxy = http.NewProxy(skipAuth, skipExistCheck)
case grpcProtocol:
proxy = grpc.NewProxy(skipAuth, skipExistCheck)
case healthCheckProtocol:
proxy = health_check.NewProxy(skipAuth, skipExistCheck)
case websocketProtocol:
proxy = websocket.NewProxy(skipAuth, skipExistCheck)
default:
return nil, errors.Errorf("unknown protocol '%s'", protocol)
}
return proxy, nil
}
func Find(path string) (Proxy, string) {
for _, item := range store {
if strings.HasPrefix(path, item.pathPrefix) {
return item.proxy, getPathWithoutPrefix(path, item.pathPrefix)
}
}
return nil, ""
}
func Close() {
for _, p := range store {
p.proxy.Close()
}
}
func getPathWithoutPrefix(path, prefix string) string {
s := strings.TrimPrefix(path, prefix)
if len(s) > 0 && s[0] == '/' {
return s[1:]
}
return s
}
func aggregateConsumers(consumers ...func([]structure.AddressConfiguration) bool) func([]structure.AddressConfiguration) bool {
return func(list []structure.AddressConfiguration) bool {
for _, consumer := range consumers {
consumer(list)
}
return true
}
}
-34
View File
@@ -1,34 +0,0 @@
package proxy
import (
"testing"
)
func Test_getPathWithoutPrefix(t *testing.T) {
type args struct {
path string
prefix string
}
tests := []struct {
name string
args args
want string
}{
{name: "#1", args: args{
path: "/api/some/11/22",
prefix: "/api/some",
}, want: "11/22"},
{name: "#2", args: args{
path: "/api/service/group/method",
prefix: "/api",
}, want: "service/group/method"},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
if got := getPathWithoutPrefix(tt.args.path, tt.args.prefix); got != tt.want {
t.Errorf("getPathWithoutPrefix() = %v, want %v", got, tt.want)
}
})
}
}
-28
View File
@@ -1,28 +0,0 @@
package websocket
import (
"github.com/integration-system/isp-lib/v2/structure"
"sync"
)
type RoundRobinAddrs struct {
mu *sync.Mutex
addrs []structure.AddressConfiguration
index int
}
func (u *RoundRobinAddrs) Get() structure.AddressConfiguration {
u.mu.Lock()
u.index = (u.index + 1) % len(u.addrs)
addr := u.addrs[u.index]
u.mu.Unlock()
return addr
}
func NewRoundRobinAddrs(addrs []structure.AddressConfiguration) *RoundRobinAddrs {
return &RoundRobinAddrs{
addrs: addrs,
index: -1,
mu: new(sync.Mutex),
}
}