@cryptotaxi247 / kubo / commits / 191ac62c1

making the daemon shutdown quicker

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jul 14, 2015 at 14:04 UTC 191ac62c12fab7d996de0a06b185a9fc5572e6ee
3 files changed +13 -7
exchange/bitswap/wantmanager.go
+2
@@ -140,6 +140,8 @@ func (mq *msgQueue) runQueue(ctx context.Context) {
140 mq.doWork(ctx)
141 case <-mq.done:
142 return
143 + case <-ctx.Done():
144 + return
145 }
146 }
147 }
p2p/host/basic/natmgr.go
+10 -6
@@ -75,18 +75,22 @@ func (nmgr *natManager) discoverNAT() {
75 // to avoid leaking resources in a non-obvious way. the only case
76 // this affects is when the daemon is being started up and _immediately_
77 // asked to close. other services are also starting up, so ok to wait.
78 - nat := inat.DiscoverNAT()
79 - if nat == nil { // no nat, or failed to get it.
80 - return
81 - }
78 + discoverdone := make(chan struct{})
79 + var nat *inat.NAT
80 + go func() {
81 + defer close(discoverdone)
82 + nat = inat.DiscoverNAT()
83 + }()
84
85 // by this point -- after finding the NAT -- we may have already
86 // be closing. if so, just exit.
87 select {
88 case <-worker.Closing():
87 - nat.Close()
89 return
89 - default:
90 + case <-discoverdone:
91 + if nat == nil { // no nat, or failed to get it.
92 + return
93 + }
94 }
95
96 // wire up the nat to close when nmgr closes.
p2p/net/swarm/swarm.go
+1 -1
@@ -187,7 +187,7 @@ func (s *Swarm) NewStreamWithPeer(p peer.ID) (*Stream, error) {
187 // if we have no connections, try connecting.
188 if len(s.ConnectionsToPeer(p)) == 0 {
189 log.Debug("Swarm: NewStreamWithPeer no connections. Attempting to connect...")
190 - if _, err := s.Dial(context.Background(), p); err != nil {
190 + if _, err := s.Dial(s.Context(), p); err != nil {
191 return nil, err
192 }
193 }