refactor(bondserver): tenant-scoped ключ Registry
Заменяет map[uint64]*serverConn на map[connKey]*serverConn, где connKey =
{auth.TenantID, uint64}. Предотвращает коллизии ConnID между tenant в будущих
multi-tenant развёртываниях. Все текущие вызовы передают auth.Anonymous, поэтому
поведение не изменено.
HandleStreamAfterMagic получает параметр tenant auth.TenantID; tcpfwdserver
передаёт auth.Anonymous, сохраняя single-tenant семантику. serverConn получает
поле tenantID под будущий per-tenant роутинг.
This commit is contained in:
1 parent
65aa55b211
commit
52271d0367
3 files changed
+36
-17
No files matched your search
@@ -11,6 +11,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/samosvalishe/btp/internal/auth"
|
||||||
"github.com/samosvalishe/btp/internal/logx"
|
"github.com/samosvalishe/btp/internal/logx"
|
||||||
"github.com/samosvalishe/btp/internal/wire/bondframe"
|
"github.com/samosvalishe/btp/internal/wire/bondframe"
|
||||||
"github.com/xtaci/smux"
|
"github.com/xtaci/smux"
|
||||||
@@ -37,30 +38,42 @@ func (d *Deps) log() logx.Logger {
|
|||||||
return d.Log
|
return d.Log
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registry deduplicates concurrent lanes that share a ConnID into a single
|
// connKey is the composite de-duplication key for the Registry map.
|
||||||
// backend TCP connection.
|
// Incorporating TenantID ensures ConnIDs from different tenants never collide,
|
||||||
|
// which is required for multi-tenant deployments. In single-tenant (non-auth)
|
||||||
|
// mode every key has Tenant == auth.Anonymous.
|
||||||
|
type connKey struct {
|
||||||
|
Tenant auth.TenantID
|
||||||
|
ConnID uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Registry deduplicates concurrent lanes that share a (TenantID, ConnID) pair
|
||||||
|
// into a single backend TCP connection.
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
deps Deps
|
deps Deps
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
conns map[uint64]*serverConn
|
conns map[connKey]*serverConn
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRegistry creates an empty Registry.
|
// NewRegistry creates an empty Registry.
|
||||||
func NewRegistry(deps Deps) *Registry {
|
func NewRegistry(deps Deps) *Registry {
|
||||||
return &Registry{deps: deps, conns: make(map[uint64]*serverConn)}
|
return &Registry{deps: deps, conns: make(map[connKey]*serverConn)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleStreamAfterMagic accepts a smux stream whose first 4 magic bytes have
|
// HandleStreamAfterMagic accepts a smux stream whose first 4 magic bytes have
|
||||||
// already been consumed (server-side multiplex pre-peek), reads the Hello,
|
// already been consumed (server-side multiplex pre-peek), reads the Hello,
|
||||||
// attaches as a lane, and blocks until the underlying bond connection is done.
|
// attaches as a lane, and blocks until the underlying bond connection is done.
|
||||||
func (r *Registry) HandleStreamAfterMagic(ctx context.Context, stream *smux.Stream, connectAddr string, magic [4]byte) {
|
//
|
||||||
r.handleStream(ctx, stream, connectAddr, func(rd io.Reader) (bondframe.Hello, error) {
|
// tenant scopes the de-duplication key. Pass [auth.Anonymous] in single-tenant
|
||||||
|
// deployments; real Authenticator-derived values in multi-tenant mode.
|
||||||
|
func (r *Registry) HandleStreamAfterMagic(ctx context.Context, tenant auth.TenantID, stream *smux.Stream, connectAddr string, magic [4]byte) {
|
||||||
|
r.handleStream(ctx, tenant, stream, connectAddr, func(rd io.Reader) (bondframe.Hello, error) {
|
||||||
return bondframe.ReadHelloAfterMagic(rd, magic)
|
return bondframe.ReadHelloAfterMagic(rd, magic)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) handleStream(ctx context.Context, stream *smux.Stream, connectAddr string, readHello func(io.Reader) (bondframe.Hello, error)) {
|
func (r *Registry) handleStream(ctx context.Context, tenant auth.TenantID, stream *smux.Stream, connectAddr string, readHello func(io.Reader) (bondframe.Hello, error)) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := stream.Close(); err != nil && err != smux.ErrGoAway {
|
if err := stream.Close(); err != nil && err != smux.ErrGoAway {
|
||||||
r.deps.log().Errorf("bondserver: close smux stream: %v", err)
|
r.deps.log().Errorf("bondserver: close smux stream: %v", err)
|
||||||
@@ -73,7 +86,7 @@ func (r *Registry) handleStream(ctx context.Context, stream *smux.Stream, connec
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conn := r.get(ctx, hello.ConnID, connectAddr)
|
conn := r.get(ctx, tenant, hello.ConnID, connectAddr)
|
||||||
conn.addLane(&serverLane{
|
conn.addLane(&serverLane{
|
||||||
index: hello.LaneIndex,
|
index: hello.LaneIndex,
|
||||||
stream: stream,
|
stream: stream,
|
||||||
@@ -85,15 +98,17 @@ func (r *Registry) handleStream(ctx context.Context, stream *smux.Stream, connec
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) get(ctx context.Context, id uint64, connectAddr string) *serverConn {
|
func (r *Registry) get(ctx context.Context, tenant auth.TenantID, id uint64, connectAddr string) *serverConn {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
if c := r.conns[id]; c != nil {
|
key := connKey{Tenant: tenant, ConnID: id}
|
||||||
|
if c := r.conns[key]; c != nil {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
connCtx, cancel := context.WithCancel(ctx)
|
connCtx, cancel := context.WithCancel(ctx)
|
||||||
c := &serverConn{
|
c := &serverConn{
|
||||||
deps: &r.deps,
|
deps: &r.deps,
|
||||||
|
tenantID: tenant,
|
||||||
id: id,
|
id: id,
|
||||||
connectAddr: connectAddr,
|
connectAddr: connectAddr,
|
||||||
ctx: connCtx,
|
ctx: connCtx,
|
||||||
@@ -102,12 +117,12 @@ func (r *Registry) get(ctx context.Context, id uint64, connectAddr string) *serv
|
|||||||
ready: make(chan struct{}, 1),
|
ready: make(chan struct{}, 1),
|
||||||
recvCh: make(chan bondframe.Frame, 1024),
|
recvCh: make(chan bondframe.Frame, 1024),
|
||||||
}
|
}
|
||||||
r.conns[id] = c
|
r.conns[key] = c
|
||||||
go func() {
|
go func() {
|
||||||
<-c.done
|
<-c.done
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
if r.conns[id] == c {
|
if r.conns[key] == c {
|
||||||
delete(r.conns, id)
|
delete(r.conns, key)
|
||||||
}
|
}
|
||||||
r.mu.Unlock()
|
r.mu.Unlock()
|
||||||
}()
|
}()
|
||||||
@@ -122,6 +137,7 @@ type serverLane struct {
|
|||||||
|
|
||||||
type serverConn struct {
|
type serverConn struct {
|
||||||
deps *Deps
|
deps *Deps
|
||||||
|
tenantID auth.TenantID
|
||||||
id uint64
|
id uint64
|
||||||
connectAddr string
|
connectAddr string
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/samosvalishe/btp/internal/auth"
|
||||||
"github.com/samosvalishe/btp/internal/wire/bondframe"
|
"github.com/samosvalishe/btp/internal/wire/bondframe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -251,18 +252,19 @@ func TestRegistryGetDedup(t *testing.T) {
|
|||||||
r := NewRegistry(Deps{})
|
r := NewRegistry(Deps{})
|
||||||
ctx := t.Context()
|
ctx := t.Context()
|
||||||
|
|
||||||
c1 := r.get(ctx, 7, "127.0.0.1:1")
|
c1 := r.get(ctx, auth.Anonymous, 7, "127.0.0.1:1")
|
||||||
c2 := r.get(ctx, 7, "127.0.0.1:1")
|
c2 := r.get(ctx, auth.Anonymous, 7, "127.0.0.1:1")
|
||||||
if c1 != c2 {
|
if c1 != c2 {
|
||||||
t.Fatal("expected same conn for same id")
|
t.Fatal("expected same conn for same id")
|
||||||
}
|
}
|
||||||
|
|
||||||
close(c1.done)
|
close(c1.done)
|
||||||
// give the cleanup goroutine a chance to run
|
// give the cleanup goroutine a chance to run
|
||||||
|
key := connKey{Tenant: auth.Anonymous, ConnID: 7}
|
||||||
deadline := time.Now().Add(time.Second)
|
deadline := time.Now().Add(time.Second)
|
||||||
for time.Now().Before(deadline) {
|
for time.Now().Before(deadline) {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
_, ok := r.conns[7]
|
_, ok := r.conns[key]
|
||||||
r.mu.Unlock()
|
r.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/samosvalishe/btp/internal/auth"
|
||||||
"github.com/samosvalishe/btp/internal/logx"
|
"github.com/samosvalishe/btp/internal/logx"
|
||||||
"github.com/samosvalishe/btp/internal/netconn"
|
"github.com/samosvalishe/btp/internal/netconn"
|
||||||
"github.com/samosvalishe/btp/internal/proxy/bondserver"
|
"github.com/samosvalishe/btp/internal/proxy/bondserver"
|
||||||
@@ -90,7 +91,7 @@ func handleStream(ctx context.Context, logger logx.Logger, registry *bondserver.
|
|||||||
}
|
}
|
||||||
if string(prefix[:]) == bondframe.Magic {
|
if string(prefix[:]) == bondframe.Magic {
|
||||||
logger.Debugf("auto-detected bond smux stream")
|
logger.Debugf("auto-detected bond smux stream")
|
||||||
registry.HandleStreamAfterMagic(ctx, s, connectAddr, prefix)
|
registry.HandleStreamAfterMagic(ctx, auth.Anonymous, s, connectAddr, prefix)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user