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