From d33b7c142dd120d0441b7167db885ab15d457584 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Wed, 1 May 2019 22:16:23 -0700 Subject: [PATCH] fix: remove notifier 1. It was unused. 2. It was broken. Attempting to register a notification listener would have registered the notifier itself. 3. We need to switch to some consistent event/notification system anyways so we're going to replace this code no matter what. (found by @pornin) --- go.mod | 1 - go.sum | 2 -- nat.go | 13 ------------- notifier.go | 47 ----------------------------------------------- 4 files changed, 63 deletions(-) delete mode 100644 notifier.go diff --git a/go.mod b/go.mod index e43298b..b60a8f2 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,4 @@ require ( github.com/jbenet/go-cienv v0.0.0-20150120210510-1bb1476777ec // indirect github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8 github.com/libp2p/go-nat v0.0.3 - github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f ) diff --git a/go.sum b/go.sum index 39da03d..ced98d8 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,6 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0 github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc h1:9lDbC6Rz4bwmou+oE6Dt4Cb2BGMur5eR/GYptkKUVHo= github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc/go.mod h1:bopw91TMyo8J3tvftk8xmU2kPmlrt4nScJQZU2hE5EM= -github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f h1:M/lL30eFZTKnomXY6huvM6G0+gVquFNf6mxghaWlFUg= -github.com/whyrusleeping/go-notifier v0.0.0-20170827234753-097c5d47330f/go.mod h1:cZNvX9cFybI01GriPRMXDtczuvUhgbcYr9iCGaNlRv8= golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo= golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/nat.go b/nat.go index 178f7d0..0466272 100644 --- a/nat.go +++ b/nat.go @@ -74,8 +74,6 @@ type NAT struct { mappingmu sync.RWMutex // guards mappings mappings map[*mapping]struct{} - - Notifier } func newNAT(realNAT nat.NAT) *NAT { @@ -186,10 +184,6 @@ func (nat *NAT) establishMapping(m *mapping) { m.setExternalPort(0) // clear mapping // TODO: log.Event log.Warningf("failed to establish port mapping: %s", err) - nat.Notifier.notifyAll(func(n Notifiee) { - n.MappingFailed(nat, m, oldport, err) - }) - // we do not close if the mapping failed, // because it may work again next time. return @@ -199,12 +193,5 @@ func (nat *NAT) establishMapping(m *mapping) { log.Debugf("NAT Mapping: %s --> %s (%s)", m.ExternalPort(), m.InternalPort(), m.Protocol()) if oldport != 0 && newport != oldport { log.Debugf("failed to renew same port mapping: ch %d -> %d", oldport, newport) - nat.Notifier.notifyAll(func(n Notifiee) { - n.MappingChanged(nat, m, oldport, newport) - }) } - - nat.Notifier.notifyAll(func(n Notifiee) { - n.MappingSuccess(nat, m) - }) } diff --git a/notifier.go b/notifier.go deleted file mode 100644 index 10fb6ac..0000000 --- a/notifier.go +++ /dev/null @@ -1,47 +0,0 @@ -package nat - -import ( - notifier "github.com/whyrusleeping/go-notifier" -) - -// Notifier is an object that assists NAT in notifying listeners. -// It is implemented using thirdparty/notifier -type Notifier struct { - n notifier.Notifier -} - -func (n *Notifier) notifyAll(notify func(n Notifiee)) { - n.n.NotifyAll(func(n notifier.Notifiee) { - notify(n.(Notifiee)) - }) -} - -// Notify signs up notifiee to listen to NAT events. -func (n *Notifier) Notify(notifiee Notifiee) { - n.n.Notify(n) -} - -// StopNotify stops signaling events to notifiee. -func (n *Notifier) StopNotify(notifiee Notifiee) { - n.n.StopNotify(notifiee) -} - -// Notifiee is an interface objects must implement to listen to NAT events. -type Notifiee interface { - - // Called every time a successful mapping happens - // Warning: the port mapping may have changed. If that is the - // case, both MappingSuccess and MappingChanged are called. - MappingSuccess(nat *NAT, m Mapping) - - // Called when mapping a port succeeds, but the mapping is - // with a different port than an earlier success. - MappingChanged(nat *NAT, m Mapping, oldport, newport int) - - // Called when a port mapping fails. NAT will continue attempting after - // the next period. To stop trying, use: mapping.Close(). After this failure, - // mapping.ExternalPort() will be zero, and nat.ExternalAddrs() will not - // return the address for this mapping. With luck, the next attempt will - // succeed, without the client needing to do anything. - MappingFailed(nat *NAT, m Mapping, oldport int, err error) -}