feat: add graceful shutdown support to Transport with done channel and stopOnce synchronization
Some checks failed
Bearer / scan (push) Successful in 47s
Go Build Multi-Platform / build (amd64, freebsd) (push) Successful in 46s
Go Build Multi-Platform / build (amd64, windows) (push) Successful in 44s
Go Build Multi-Platform / build (arm, linux) (push) Successful in 42s
Go Build Multi-Platform / build (wasm, js) (push) Successful in 54s
Go Build Multi-Platform / build (arm64, linux) (push) Successful in 58s
Go Build Multi-Platform / build (arm64, windows) (push) Successful in 56s
TinyGo Build / tinygo-build (tinygo-build, tinygo-default, reticulum-go-tinygo, ) (pull_request) Failing after 52s
TinyGo Build / tinygo-build (tinygo-wasm, tinygo-wasm, reticulum-go.wasm, wasm) (pull_request) Failing after 58s
Go Test Multi-Platform / Test (ubuntu-latest, arm64) (push) Successful in 1m6s
Go Revive Lint / lint (push) Successful in 58s
Run Gosec / tests (push) Successful in 1m54s
Go Test Multi-Platform / Test (ubuntu-latest, amd64) (push) Successful in 2m27s
Go Build Multi-Platform / build (amd64, linux) (push) Successful in 9m27s
Go Build Multi-Platform / build (amd64, darwin) (push) Successful in 9m29s
Go Build Multi-Platform / build (arm, freebsd) (push) Successful in 9m29s
Go Build Multi-Platform / build (arm, windows) (push) Successful in 9m27s
Go Build Multi-Platform / build (arm64, freebsd) (push) Successful in 9m27s
Go Build Multi-Platform / build (arm64, darwin) (push) Successful in 9m29s
Go Build Multi-Platform / Create Release (push) Has been skipped

This commit is contained in:
2025-12-30 21:15:16 -06:00
parent 6888eccc62
commit eec73d2d93

View File

@@ -125,6 +125,8 @@ type Transport struct {
heldAnnounces map[string]*PathAnnounceEntry heldAnnounces map[string]*PathAnnounceEntry
transportIdentity *identity.Identity transportIdentity *identity.Identity
pathRequestDest interface{} pathRequestDest interface{}
done chan struct{}
stopOnce sync.Once
} }
type DiscoveryPathRequest struct { type DiscoveryPathRequest struct {
@@ -176,6 +178,7 @@ func NewTransport(cfg *common.ReticulumConfig) *Transport {
discoveryPRTags: make(map[string]bool), discoveryPRTags: make(map[string]bool),
announceTable: make(map[string]*PathAnnounceEntry), announceTable: make(map[string]*PathAnnounceEntry),
heldAnnounces: make(map[string]*PathAnnounceEntry), heldAnnounces: make(map[string]*PathAnnounceEntry),
done: make(chan struct{}),
} }
// TODO: Path table persistence // TODO: Path table persistence
@@ -194,11 +197,16 @@ func (t *Transport) startMaintenanceJobs() {
ticker := time.NewTicker(common.FIVE * time.Second) ticker := time.NewTicker(common.FIVE * time.Second)
defer ticker.Stop() defer ticker.Stop()
for range ticker.C { for {
t.cleanupExpiredPaths() select {
t.cleanupExpiredDiscoveryRequests() case <-ticker.C:
t.cleanupExpiredAnnounces() t.cleanupExpiredPaths()
t.cleanupExpiredReceipts() t.cleanupExpiredDiscoveryRequests()
t.cleanupExpiredAnnounces()
t.cleanupExpiredReceipts()
case <-t.done:
return
}
} }
} }
@@ -308,14 +316,12 @@ func (t *Transport) CreateIncomingLink(dest interface{}, networkIface common.Net
return nil return nil
} }
// Add GetTransportInstance function
func GetTransportInstance() *Transport { func GetTransportInstance() *Transport {
transportMutex.Lock() transportMutex.Lock()
defer transportMutex.Unlock() defer transportMutex.Unlock()
return transportInstance return transportInstance
} }
// Update the interface methods
func (t *Transport) RegisterInterface(name string, iface common.NetworkInterface) error { func (t *Transport) RegisterInterface(name string, iface common.NetworkInterface) error {
t.mutex.Lock() t.mutex.Lock()
defer t.mutex.Unlock() defer t.mutex.Unlock()
@@ -340,8 +346,11 @@ func (t *Transport) GetInterface(name string) (common.NetworkInterface, error) {
return iface, nil return iface, nil
} }
// Update the Close method
func (t *Transport) Close() error { func (t *Transport) Close() error {
t.stopOnce.Do(func() {
close(t.done)
})
t.mutex.Lock() t.mutex.Lock()
defer t.mutex.Unlock() defer t.mutex.Unlock()
@@ -607,7 +616,6 @@ func (t *Transport) RequestPath(destinationHash []byte, onInterface string, tag
return nil return nil
} }
// updatePathUnlocked updates path without acquiring mutex (caller must hold lock)
func (t *Transport) updatePathUnlocked(destinationHash []byte, nextHop []byte, interfaceName string, hops uint8) { func (t *Transport) updatePathUnlocked(destinationHash []byte, nextHop []byte, interfaceName string, hops uint8) {
// Direct access to interfaces map since caller holds the lock // Direct access to interfaces map since caller holds the lock
iface, exists := t.interfaces[interfaceName] iface, exists := t.interfaces[interfaceName]
@@ -745,7 +753,6 @@ func (p *LinkPacket) send() error {
header = append(header, 0x02) // Link packet type header = append(header, 0x02) // Link packet type
header = append(header, p.Destination...) header = append(header, p.Destination...)
// Add timestamp
ts := make([]byte, 8) ts := make([]byte, 8)
binary.BigEndian.PutUint64(ts, uint64(p.Timestamp.Unix())) // #nosec G115 binary.BigEndian.PutUint64(ts, uint64(p.Timestamp.Unix())) // #nosec G115
header = append(header, ts...) header = append(header, ts...)