feat: make PortRegistry interface segregation

This commit is contained in:
2026-07-13 02:42:21 +07:00
parent 43d1ec0e6d
commit f1df3d26c2
3 changed files with 19 additions and 13 deletions
+15 -7
View File
@@ -10,8 +10,6 @@ import (
"tunnel_pls/internal/session/slug"
"tunnel_pls/internal/types"
portUtil "tunnel_pls/internal/port"
"golang.org/x/crypto/ssh"
)
@@ -25,6 +23,12 @@ type SessionRegistry interface {
Remove(key types.SessionKey)
}
type PortRegistry interface {
Unassigned() (uint16, bool)
Claim(port uint16) bool
SetStatus(port uint16, assigned bool) error
}
type lifecycle struct {
mu sync.Mutex
status types.SessionStatus
@@ -35,11 +39,11 @@ type lifecycle struct {
slug slug.Slug
startedAt time.Time
sessionRegistry SessionRegistry
portRegistry portUtil.Port
portRegistry PortRegistry
user string
}
func New(conn ssh.Conn, forwarder Forwarder, slugManager slug.Slug, port portUtil.Port, sessionRegistry SessionRegistry, user string) Lifecycle {
func New(conn ssh.Conn, forwarder Forwarder, slugManager slug.Slug, port PortRegistry, sessionRegistry SessionRegistry, user string) Lifecycle {
return &lifecycle{
status: types.SessionStatusINITIALIZING,
conn: conn,
@@ -56,7 +60,7 @@ func New(conn ssh.Conn, forwarder Forwarder, slugManager slug.Slug, port portUti
type Lifecycle interface {
Connection() ssh.Conn
Channel() ssh.Channel
PortRegistry() portUtil.Port
PortRegistry() PortRegistry
User() string
SetChannel(channel ssh.Channel) error
SetStatus(status types.SessionStatus)
@@ -65,7 +69,7 @@ type Lifecycle interface {
Close() error
}
func (l *lifecycle) PortRegistry() portUtil.Port {
func (l *lifecycle) PortRegistry() PortRegistry {
return l.portRegistry
}
@@ -148,8 +152,12 @@ func (l *lifecycle) closeConnection() error {
}
func (l *lifecycle) cleanupRegistry() {
slugStr := l.slug.String()
if slugStr == "" {
return
}
key := types.SessionKey{
Id: l.slug.String(),
Id: slugStr,
Type: l.forwarder.TunnelType(),
}
l.sessionRegistry.Remove(key)