Browse Source
This commit adds a peer notifier package which provides clients with a subscription to peer online and offline events.master
carla
5 years ago
4 changed files with 150 additions and 2 deletions
@ -0,0 +1,29 @@
|
||||
package peernotifier |
||||
|
||||
import ( |
||||
"github.com/btcsuite/btclog" |
||||
"github.com/lightningnetwork/lnd/build" |
||||
) |
||||
|
||||
// log is a logger that is initialized with no output filters. This
|
||||
// means the package will not perform any logging by default until the caller
|
||||
// requests it.
|
||||
var log btclog.Logger |
||||
|
||||
// The default amount of logging is none.
|
||||
func init() { |
||||
UseLogger(build.NewSubLogger("PRNF", nil)) |
||||
} |
||||
|
||||
// DisableLog disables all library log output. Logging output is disabled
|
||||
// by default until UseLogger is called.
|
||||
func DisableLog() { |
||||
UseLogger(btclog.Disabled) |
||||
} |
||||
|
||||
// UseLogger uses a specified Logger to output package logging info.
|
||||
// This should be used in preference to SetLogWriter if the caller is also
|
||||
// using btclog.
|
||||
func UseLogger(logger btclog.Logger) { |
||||
log = logger |
||||
} |
@ -0,0 +1,87 @@
|
||||
package peernotifier |
||||
|
||||
import ( |
||||
"sync" |
||||
|
||||
"github.com/lightningnetwork/lnd/subscribe" |
||||
) |
||||
|
||||
// PeerNotifier is a subsystem which observes peer offline and online events.
|
||||
// It takes subscriptions for its events, and whenever it observes a new event
|
||||
// it notifies its subscribers over the proper channel.
|
||||
type PeerNotifier struct { |
||||
started sync.Once |
||||
stopped sync.Once |
||||
|
||||
ntfnServer *subscribe.Server |
||||
} |
||||
|
||||
// PeerOnlineEvent represents a new event where a peer comes online.
|
||||
type PeerOnlineEvent struct { |
||||
// PubKey is the peer's compressed public key.
|
||||
PubKey [33]byte |
||||
} |
||||
|
||||
// PeerOfflineEvent represents a new event where a peer goes offline.
|
||||
type PeerOfflineEvent struct { |
||||
// PubKey is the peer's compressed public key.
|
||||
PubKey [33]byte |
||||
} |
||||
|
||||
// New creates a new peer notifier which notifies clients of peer online
|
||||
// and offline events.
|
||||
func New() *PeerNotifier { |
||||
return &PeerNotifier{ |
||||
ntfnServer: subscribe.NewServer(), |
||||
} |
||||
} |
||||
|
||||
// Start starts the PeerNotifier's subscription server.
|
||||
func (p *PeerNotifier) Start() error { |
||||
var err error |
||||
|
||||
p.started.Do(func() { |
||||
log.Info("PeerNotifier starting") |
||||
err = p.ntfnServer.Start() |
||||
}) |
||||
|
||||
return err |
||||
} |
||||
|
||||
// Stop signals the notifier for a graceful shutdown.
|
||||
func (p *PeerNotifier) Stop() { |
||||
p.stopped.Do(func() { |
||||
log.Info("Stopping PeerNotifier") |
||||
p.ntfnServer.Stop() |
||||
}) |
||||
} |
||||
|
||||
// SubscribePeerEvents returns a subscribe.Client that will receive updates
|
||||
// any time the Server is informed of a peer event.
|
||||
func (p *PeerNotifier) SubscribePeerEvents() (*subscribe.Client, error) { |
||||
return p.ntfnServer.Subscribe() |
||||
} |
||||
|
||||
// NotifyPeerOnline sends a peer online event to all clients subscribed to the
|
||||
// peer notifier.
|
||||
func (p *PeerNotifier) NotifyPeerOnline(pubKey [33]byte) { |
||||
event := PeerOnlineEvent{PubKey: pubKey} |
||||
|
||||
log.Debugf("PeerNotifier notifying peer: %x online", pubKey) |
||||
|
||||
if err := p.ntfnServer.SendUpdate(event); err != nil { |
||||
log.Warnf("Unable to send peer online update: %v", err) |
||||
} |
||||
} |
||||
|
||||
// NotifyPeerOffline sends a peer offline event to all the clients subscribed
|
||||
// to the peer notifier.
|
||||
func (p *PeerNotifier) NotifyPeerOffline(pubKey [33]byte) { |
||||
event := PeerOfflineEvent{PubKey: pubKey} |
||||
|
||||
log.Debugf("PeerNotifier notifying peer: %x offline", pubKey) |
||||
|
||||
if err := p.ntfnServer.SendUpdate(event); err != nil { |
||||
log.Warnf("Unable to send peer offline update: %v", err) |
||||
} |
||||
} |
Loading…
Reference in new issue