2026-02-11 16:56:17: merge [main:afeadb1d9a241da30507f3683781558dbccfefc6];
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
This commit is contained in:
1 parent
aecc42c6d8
commit
d4091f8805
25 files changed
+1664
-92
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package assembly
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/txix-open/isp-kit/app"
|
||||
"github.com/txix-open/isp-kit/bootstrap"
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/http"
|
||||
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
)
|
||||
|
||||
type Assembly struct {
|
||||
boot *bootstrap.Bootstrap
|
||||
server *http.Server
|
||||
proxyMap map[ProxyKey]Proxy
|
||||
logger *log.Adapter
|
||||
}
|
||||
|
||||
func New(boot *bootstrap.Bootstrap) *Assembly {
|
||||
return &Assembly{
|
||||
boot: boot,
|
||||
server: http.NewServer(boot.App.Logger()),
|
||||
proxyMap: make(map[ProxyKey]Proxy, 0),
|
||||
logger: boot.App.Logger(),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Assembly) ReceiveRoutes(_ context.Context, routingCfg cluster.RoutingConfig) error {
|
||||
locator := NewLocator(a.logger)
|
||||
locatorCfg := locator.LocatorConfig(routingCfg, a.proxyMap)
|
||||
a.server.Upgrade(locatorCfg.Handler)
|
||||
|
||||
for key, oldProxy := range a.proxyMap {
|
||||
_, ok := locatorCfg.ProxyMap[key]
|
||||
if !ok {
|
||||
_ = oldProxy.Close()
|
||||
}
|
||||
}
|
||||
a.proxyMap = locatorCfg.ProxyMap
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Assembly) Runners() []app.Runner {
|
||||
eventHandler := cluster.NewEventHandler().
|
||||
RoutesReceiver(a)
|
||||
return []app.Runner{
|
||||
app.RunnerFunc(func(ctx context.Context) error {
|
||||
return a.server.ListenAndServe(a.boot.BindingAddress)
|
||||
}),
|
||||
app.RunnerFunc(func(ctx context.Context) error {
|
||||
return a.boot.ClusterCli.Run(ctx, eventHandler)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Assembly) Closers() []app.Closer {
|
||||
return []app.Closer{
|
||||
a.boot.ClusterCli,
|
||||
app.CloserFunc(func() error {
|
||||
return a.server.Shutdown(context.Background())
|
||||
}),
|
||||
app.CloserFunc(func() error {
|
||||
for _, proxy := range a.proxyMap {
|
||||
_ = proxy.Close()
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package assembly
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"isp-router-service/domain"
|
||||
"isp-router-service/middleware"
|
||||
"isp-router-service/service/proxy"
|
||||
"net"
|
||||
"net/http"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
http2 "github.com/txix-open/isp-kit/http"
|
||||
"github.com/txix-open/isp-kit/http/endpoint"
|
||||
"github.com/txix-open/isp-kit/http/router"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"github.com/txix-open/isp-kit/cluster"
|
||||
"github.com/txix-open/isp-kit/grpc/client"
|
||||
)
|
||||
|
||||
const (
|
||||
maxMessageSize = 64 * 1024 * 1024
|
||||
proxyTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type Proxy interface {
|
||||
Handle(ctx *domain.RequestContext) error
|
||||
PrepareEndpoint(endpoint string, proxyApiPrefix bool) string
|
||||
Close() error
|
||||
}
|
||||
|
||||
type Locator struct {
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
func NewLocator(logger log.Logger) Locator {
|
||||
return Locator{
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
type LocatorConfig struct {
|
||||
ProxyMap map[ProxyKey]Proxy
|
||||
Handler http.Handler
|
||||
}
|
||||
|
||||
type ProxyKey struct {
|
||||
Transport string
|
||||
Addresses string
|
||||
}
|
||||
|
||||
func (l Locator) LocatorConfig(config cluster.RoutingConfig, oldProxyMap map[ProxyKey]Proxy) LocatorConfig {
|
||||
routes := l.getRoutes(config)
|
||||
|
||||
groupMap := make(map[string][]domain.RouteKey)
|
||||
for route, cfg := range routes {
|
||||
addresses := append([]string{}, cfg.Addresses...)
|
||||
sort.Strings(addresses) // чтобы ключ был одинаковым для одинакового множества
|
||||
groupKey := strings.Join(addresses, ",")
|
||||
groupMap[groupKey] = append(groupMap[groupKey], route)
|
||||
}
|
||||
|
||||
mux := router.New()
|
||||
mux.InternalRouter().NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, fmt.Sprintf("unknown endpoint '%s'", r.URL.Path), http.StatusNotImplemented)
|
||||
})
|
||||
|
||||
middlewares := []http2.Middleware{
|
||||
middleware.ErrorHandler(l.logger),
|
||||
endpoint.RequestId(),
|
||||
}
|
||||
|
||||
proxyMap := make(map[ProxyKey]Proxy, len(groupMap))
|
||||
for groupKey, routeKeys := range groupMap {
|
||||
addresses := strings.Split(groupKey, ",")
|
||||
// ОК, т.к. в getRoutes собираем только с endpoint'ами
|
||||
transport := routes[routeKeys[0]].Transport
|
||||
proxyKey := ProxyKey{
|
||||
Transport: transport,
|
||||
Addresses: groupKey,
|
||||
}
|
||||
|
||||
var err error
|
||||
proxy, ok := oldProxyMap[proxyKey]
|
||||
if !ok {
|
||||
proxy, err = buildProxy(proxyKey.Transport, addresses)
|
||||
if err != nil {
|
||||
l.logger.Error(context.Background(), err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
proxyMap[proxyKey] = proxy
|
||||
|
||||
for _, routeKey := range routeKeys {
|
||||
l.registerEndpoint(mux, routeKey, proxy, middlewares)
|
||||
}
|
||||
}
|
||||
|
||||
l.logger.Info(
|
||||
context.Background(),
|
||||
"change routing table",
|
||||
log.Int("totalBackends", len(proxyMap)),
|
||||
log.Int("totalEndpoints", len(routes)),
|
||||
)
|
||||
return LocatorConfig{
|
||||
ProxyMap: proxyMap,
|
||||
Handler: mux,
|
||||
}
|
||||
}
|
||||
|
||||
func (l Locator) getRoutes(config cluster.RoutingConfig) map[domain.RouteKey]*domain.RouteConfig {
|
||||
routes := make(map[domain.RouteKey]*domain.RouteConfig, len(config))
|
||||
|
||||
for _, decl := range config {
|
||||
if decl.Address.IP == "" ||
|
||||
decl.Address.Port == "" ||
|
||||
len(decl.Endpoints) == 0 ||
|
||||
decl.Transport == cluster.EmptyTransport {
|
||||
continue
|
||||
}
|
||||
|
||||
addr := net.JoinHostPort(decl.Address.IP, decl.Address.Port)
|
||||
|
||||
for _, ep := range decl.Endpoints {
|
||||
if !strings.HasPrefix(ep.Path, "/") {
|
||||
ep.Path = "/" + ep.Path
|
||||
}
|
||||
|
||||
transport := decl.Transport
|
||||
if decl.Transport == "" {
|
||||
transport = cluster.GrpcTransport
|
||||
}
|
||||
|
||||
withApiPrefix := strings.HasPrefix(ep.Path, domain.ApiPrefix)
|
||||
cleanPath := strings.TrimPrefix(ep.Path, domain.ApiPrefix)
|
||||
|
||||
method := ep.HttpMethod
|
||||
if transport == cluster.GrpcTransport &&
|
||||
method == "" {
|
||||
method = http.MethodPost
|
||||
}
|
||||
|
||||
key := domain.RouteKey{
|
||||
Method: method,
|
||||
CleanPath: cleanPath,
|
||||
WithApiPrefix: withApiPrefix,
|
||||
}
|
||||
|
||||
route, ok := routes[key]
|
||||
if !ok {
|
||||
route = &domain.RouteConfig{
|
||||
Transport: transport,
|
||||
}
|
||||
routes[key] = route
|
||||
}
|
||||
|
||||
if route.Transport != transport {
|
||||
l.logger.Error(
|
||||
context.Background(),
|
||||
"transport conflict for route",
|
||||
log.String("method", key.Method),
|
||||
log.String("path", key.CleanPath),
|
||||
log.String("transport1", route.Transport),
|
||||
log.String("transport2", transport),
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
route.Addresses = append(route.Addresses, addr)
|
||||
}
|
||||
}
|
||||
return routes
|
||||
}
|
||||
|
||||
func (l Locator) registerEndpoint(
|
||||
mux *router.Router,
|
||||
routeKey domain.RouteKey,
|
||||
proxy Proxy,
|
||||
middlewares []http2.Middleware,
|
||||
) {
|
||||
handler := middleware.Entrypoint(maxMessageSize, routeKey.WithApiPrefix, proxy, l.logger)
|
||||
for i := range slices.Backward(middlewares) {
|
||||
handler = middlewares[i](handler)
|
||||
}
|
||||
|
||||
paths := []string{
|
||||
domain.ApiPrefix + routeKey.CleanPath, // стандартный маршрут c /api
|
||||
}
|
||||
if !routeKey.WithApiPrefix {
|
||||
paths = append(paths, routeKey.CleanPath) // для межмодульного взаимодействия
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
func() {
|
||||
defer func() {
|
||||
_ = recover()
|
||||
}()
|
||||
|
||||
mux.Handler(routeKey.Method, path,
|
||||
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), proxyTimeout)
|
||||
defer cancel()
|
||||
|
||||
err := handler(ctx, w, r)
|
||||
if err != nil {
|
||||
l.logger.Error(ctx, err)
|
||||
}
|
||||
}),
|
||||
)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// nolint:ireturn
|
||||
func buildProxy(
|
||||
transport string,
|
||||
addresses []string,
|
||||
) (Proxy, error) {
|
||||
switch transport {
|
||||
case cluster.HttpTransport:
|
||||
return proxy.NewHttp(addresses), nil
|
||||
|
||||
case cluster.GrpcTransport:
|
||||
cli, err := client.New(addresses, client.WithDialOptions(
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxMessageSize)),
|
||||
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(maxMessageSize)),
|
||||
))
|
||||
if err != nil {
|
||||
return nil, errors.WithMessage(err, "init grpc client")
|
||||
}
|
||||
return proxy.NewGrpc(cli), nil
|
||||
default:
|
||||
return nil, errors.Errorf("unknown transport '%s'", transport)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user