Select fabric carrier by endpoint
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -37,6 +38,7 @@ func (t *QUICFabricTransport) Connect(ctx context.Context, target FabricTranspor
|
||||
if target.Endpoint == "" {
|
||||
return nil, fmt.Errorf("quic fabric endpoint is required")
|
||||
}
|
||||
target.Endpoint = strings.TrimPrefix(strings.TrimSpace(target.Endpoint), "quic://")
|
||||
tlsConfig := target.TLSConfig
|
||||
if tlsConfig == nil {
|
||||
tlsConfig = &tls.Config{NextProtos: []string{fabricQUICNextProto}}
|
||||
@@ -76,7 +78,7 @@ func (t *QUICFabricTransport) Connect(ctx context.Context, target FabricTranspor
|
||||
maxPayload: maxPayload,
|
||||
timeout: target.Timeout,
|
||||
}
|
||||
go session.readLoop(ctx)
|
||||
go session.readLoop(context.Background())
|
||||
return session, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ func (m *FabricSessionPeerManager) Get(ctx context.Context, target FabricSession
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pump := session.StartPump(ctx, target.Pump)
|
||||
pump := session.StartPump(context.Background(), target.Pump)
|
||||
|
||||
m.mu.Lock()
|
||||
if existing := m.sessions[key]; existing != nil {
|
||||
|
||||
@@ -3,7 +3,9 @@ package mesh
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/example/remote-access-platform/agents/rap-node-agent/internal/fabricproto"
|
||||
@@ -25,6 +27,7 @@ type FabricTransport interface {
|
||||
type FabricTransportTarget struct {
|
||||
PeerID string
|
||||
Endpoint string
|
||||
Transport string
|
||||
Token string
|
||||
Header http.Header
|
||||
TLSConfig *tls.Config
|
||||
@@ -35,6 +38,29 @@ type FabricTransportTarget struct {
|
||||
ErrorBuffer int
|
||||
}
|
||||
|
||||
func FabricTransportForTarget(target FabricTransportTarget, websocket *WebSocketFabricTransport, quicTransport *QUICFabricTransport) (FabricTransport, FabricTransportTarget, error) {
|
||||
transportLabel := strings.ToLower(strings.TrimSpace(target.Transport))
|
||||
endpoint := strings.TrimSpace(target.Endpoint)
|
||||
if strings.HasPrefix(strings.ToLower(endpoint), "quic://") {
|
||||
transportLabel = "quic"
|
||||
target.Endpoint = strings.TrimPrefix(endpoint, "quic://")
|
||||
}
|
||||
switch transportLabel {
|
||||
case "quic", "direct_quic", "udp_quic", "quic_udp":
|
||||
if quicTransport == nil {
|
||||
quicTransport = NewQUICFabricTransport(nil)
|
||||
}
|
||||
return quicTransport, target, nil
|
||||
case "", "websocket", "ws", "wss", "direct_http", "direct_https", "direct_tcp_tls":
|
||||
if websocket == nil {
|
||||
websocket = NewWebSocketFabricTransport(nil)
|
||||
}
|
||||
return websocket, target, nil
|
||||
default:
|
||||
return nil, target, fmt.Errorf("unsupported fabric transport %q", target.Transport)
|
||||
}
|
||||
}
|
||||
|
||||
type WebSocketFabricTransport struct {
|
||||
Manager *FabricSessionPeerManager
|
||||
}
|
||||
|
||||
@@ -107,3 +107,33 @@ func TestWebSocketFabricTransportReopensClosedSession(t *testing.T) {
|
||||
t.Fatalf("opened = %d, want 2", opened)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFabricTransportForTargetSelectsQUICByScheme(t *testing.T) {
|
||||
transport, target, err := FabricTransportForTarget(FabricTransportTarget{
|
||||
Endpoint: "quic://127.0.0.1:4433",
|
||||
}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("select transport: %v", err)
|
||||
}
|
||||
if _, ok := transport.(*QUICFabricTransport); !ok {
|
||||
t.Fatalf("transport = %T, want QUIC", transport)
|
||||
}
|
||||
if target.Endpoint != "127.0.0.1:4433" {
|
||||
t.Fatalf("endpoint = %q", target.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFabricTransportForTargetSelectsWebSocketByDefault(t *testing.T) {
|
||||
transport, target, err := FabricTransportForTarget(FabricTransportTarget{
|
||||
Endpoint: "https://node.example",
|
||||
}, nil, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("select transport: %v", err)
|
||||
}
|
||||
if _, ok := transport.(*WebSocketFabricTransport); !ok {
|
||||
t.Fatalf("transport = %T, want websocket", transport)
|
||||
}
|
||||
if target.Endpoint != "https://node.example" {
|
||||
t.Fatalf("endpoint = %q", target.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user