Skip to content

Commit

Permalink
app: improve graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pymq committed Oct 24, 2024
1 parent 4b809e5 commit 47c2c04
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
14 changes: 8 additions & 6 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ func (a *Application) Close() {
a.logger.Errorf("closing api server: %v", err)
}
}

if a.Tunnel != nil {
a.Tunnel.Close()
}
if a.SOCKS5 != nil {
a.SOCKS5.Close()
}

if a.P2p != nil {
err := a.P2p.Close()
if err != nil {
Expand All @@ -235,12 +243,6 @@ func (a *Application) Close() {
if a.Dns != nil {
a.Dns.Close()
}
if a.Tunnel != nil {
a.Tunnel.Close()
}
if a.SOCKS5 != nil {
a.SOCKS5.Close()
}
if a.vpnDevice != nil {
err := a.vpnDevice.Close()
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions vpn/vpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Device struct {
localIP net.IP
outboundCh chan *Packet

closeCh chan struct{}
packetsPool sync.Pool
logger *log.ZapEventLogger
}
Expand Down Expand Up @@ -62,7 +63,8 @@ func NewDevice(existingTun tun.Device, interfaceName string, localIP net.IP, ipM
New: func() interface{} {
return new(Packet)
}},
logger: log.Logger("awl/vpn"),
logger: log.Logger("awl/vpn"),
closeCh: make(chan struct{}),
}
go dev.tunEventsReader()
go dev.tunPacketsReader()
Expand Down Expand Up @@ -106,6 +108,7 @@ func (d *Device) OutboundChan() <-chan *Packet {
}

func (d *Device) Close() error {
close(d.closeCh)
return d.tun.Close()
}

Expand Down Expand Up @@ -173,7 +176,12 @@ func (d *Device) tunPacketsReader() {
continue
}

d.outboundCh <- data
select {
case <-d.closeCh:
return
case d.outboundCh <- data:
// ok
}
packets[i] = nil
}

Expand Down

0 comments on commit 47c2c04

Please sign in to comment.