Introduce fabric transport abstraction
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
package mesh
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/example/remote-access-platform/agents/rap-node-agent/internal/fabricproto"
|
||||
)
|
||||
|
||||
type FabricTransportSession interface {
|
||||
Send(context.Context, fabricproto.Frame) error
|
||||
Frames() <-chan fabricproto.Frame
|
||||
Errors() <-chan error
|
||||
Close() error
|
||||
Closed() bool
|
||||
}
|
||||
|
||||
type FabricTransport interface {
|
||||
Connect(context.Context, FabricTransportTarget) (FabricTransportSession, error)
|
||||
Close() error
|
||||
}
|
||||
|
||||
type FabricTransportTarget struct {
|
||||
PeerID string
|
||||
Endpoint string
|
||||
Token string
|
||||
Header http.Header
|
||||
Timeout time.Duration
|
||||
MaxPayload int
|
||||
OutboundBuffer int
|
||||
InboundBuffer int
|
||||
ErrorBuffer int
|
||||
}
|
||||
|
||||
type WebSocketFabricTransport struct {
|
||||
Manager *FabricSessionPeerManager
|
||||
}
|
||||
|
||||
func NewWebSocketFabricTransport(manager *FabricSessionPeerManager) *WebSocketFabricTransport {
|
||||
if manager == nil {
|
||||
manager = NewFabricSessionPeerManager()
|
||||
}
|
||||
return &WebSocketFabricTransport{Manager: manager}
|
||||
}
|
||||
|
||||
func (t *WebSocketFabricTransport) Connect(ctx context.Context, target FabricTransportTarget) (FabricTransportSession, error) {
|
||||
manager := t.Manager
|
||||
if manager == nil {
|
||||
manager = NewFabricSessionPeerManager()
|
||||
t.Manager = manager
|
||||
}
|
||||
return manager.Get(ctx, FabricSessionPeerTarget{
|
||||
PeerID: target.PeerID,
|
||||
BaseURL: target.Endpoint,
|
||||
Options: FabricSessionDialOptions{
|
||||
Token: target.Token,
|
||||
Header: target.Header,
|
||||
Timeout: target.Timeout,
|
||||
MaxPayload: target.MaxPayload,
|
||||
},
|
||||
Pump: FabricSessionPumpOptions{
|
||||
OutboundBuffer: target.OutboundBuffer,
|
||||
InboundBuffer: target.InboundBuffer,
|
||||
ErrorBuffer: target.ErrorBuffer,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (t *WebSocketFabricTransport) Close() error {
|
||||
if t == nil || t.Manager == nil {
|
||||
return nil
|
||||
}
|
||||
return t.Manager.Close()
|
||||
}
|
||||
|
||||
func (t *WebSocketFabricTransport) Snapshot() FabricSessionPeerManagerSnapshot {
|
||||
if t == nil || t.Manager == nil {
|
||||
return FabricSessionPeerManagerSnapshot{SchemaVersion: "rap.fabric_session_peer_manager.v1"}
|
||||
}
|
||||
return t.Manager.Snapshot()
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package mesh
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/example/remote-access-platform/agents/rap-node-agent/internal/fabricproto"
|
||||
)
|
||||
|
||||
func TestWebSocketFabricTransportConnectsAndReusesSession(t *testing.T) {
|
||||
var opened int
|
||||
server := httptest.NewServer(Server{
|
||||
Local: PeerIdentity{ClusterID: "cluster-1", NodeID: "node-a"},
|
||||
FabricSessionEnabled: true,
|
||||
FabricSessionLogger: func(entry FabricSessionEventLogEntry) {
|
||||
if entry.Event == "fabric_session_websocket_opened" {
|
||||
opened++
|
||||
}
|
||||
},
|
||||
}.Handler())
|
||||
defer server.Close()
|
||||
|
||||
transport := NewWebSocketFabricTransport(nil)
|
||||
defer transport.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
target := FabricTransportTarget{
|
||||
PeerID: "node-a",
|
||||
Endpoint: server.URL,
|
||||
Token: "rap_fsn_transport",
|
||||
Timeout: time.Second,
|
||||
OutboundBuffer: 4,
|
||||
InboundBuffer: 4,
|
||||
ErrorBuffer: 4,
|
||||
}
|
||||
|
||||
first, err := transport.Connect(ctx, target)
|
||||
if err != nil {
|
||||
t.Fatalf("first connect: %v", err)
|
||||
}
|
||||
second, err := transport.Connect(ctx, target)
|
||||
if err != nil {
|
||||
t.Fatalf("second connect: %v", err)
|
||||
}
|
||||
if first != second {
|
||||
t.Fatal("transport did not reuse session")
|
||||
}
|
||||
if opened != 1 {
|
||||
t.Fatalf("opened = %d, want 1", opened)
|
||||
}
|
||||
if err := first.Send(ctx, fabricproto.Frame{Type: fabricproto.FramePing, Sequence: 1, Payload: []byte("transport")}); err != nil {
|
||||
t.Fatalf("send ping: %v", err)
|
||||
}
|
||||
select {
|
||||
case frame := <-first.Frames():
|
||||
if frame.Type != fabricproto.FramePong || frame.Sequence != 1 || string(frame.Payload) != "transport" {
|
||||
t.Fatalf("frame = %+v", frame)
|
||||
}
|
||||
case err := <-first.Errors():
|
||||
t.Fatalf("session error: %v", err)
|
||||
case <-ctx.Done():
|
||||
t.Fatal(ctx.Err())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSocketFabricTransportReopensClosedSession(t *testing.T) {
|
||||
var opened int
|
||||
server := httptest.NewServer(Server{
|
||||
Local: PeerIdentity{ClusterID: "cluster-1", NodeID: "node-a"},
|
||||
FabricSessionEnabled: true,
|
||||
FabricSessionLogger: func(entry FabricSessionEventLogEntry) {
|
||||
if entry.Event == "fabric_session_websocket_opened" {
|
||||
opened++
|
||||
}
|
||||
},
|
||||
}.Handler())
|
||||
defer server.Close()
|
||||
|
||||
transport := NewWebSocketFabricTransport(nil)
|
||||
defer transport.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
target := FabricTransportTarget{
|
||||
PeerID: "node-a",
|
||||
Endpoint: server.URL,
|
||||
Token: "rap_fsn_transport_reopen",
|
||||
Timeout: time.Second,
|
||||
}
|
||||
|
||||
first, err := transport.Connect(ctx, target)
|
||||
if err != nil {
|
||||
t.Fatalf("first connect: %v", err)
|
||||
}
|
||||
if err := first.Close(); err != nil {
|
||||
t.Fatalf("close first session: %v", err)
|
||||
}
|
||||
second, err := transport.Connect(ctx, target)
|
||||
if err != nil {
|
||||
t.Fatalf("second connect: %v", err)
|
||||
}
|
||||
if first == second {
|
||||
t.Fatal("transport reused closed session")
|
||||
}
|
||||
if opened != 2 {
|
||||
t.Fatalf("opened = %d, want 2", opened)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user