2019-03-26 09:06:32 +00:00
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-03-26 12:30:39 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-03-26 09:06:32 +00:00
|
|
|
"sync"
|
2019-04-01 12:46:59 +00:00
|
|
|
"time"
|
2019-03-26 09:06:32 +00:00
|
|
|
|
|
|
|
"github.com/edouardparis/lntop/app"
|
|
|
|
"github.com/edouardparis/lntop/events"
|
|
|
|
"github.com/edouardparis/lntop/logging"
|
2019-03-26 12:30:39 +00:00
|
|
|
"github.com/edouardparis/lntop/network"
|
|
|
|
"github.com/edouardparis/lntop/network/models"
|
2019-03-26 09:06:32 +00:00
|
|
|
)
|
|
|
|
|
2019-03-26 12:30:39 +00:00
|
|
|
type pubSub struct {
|
|
|
|
stop chan bool
|
|
|
|
logger logging.Logger
|
|
|
|
network *network.Network
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPubSub(logger logging.Logger, network *network.Network) *pubSub {
|
|
|
|
return &pubSub{
|
|
|
|
logger: logger.With(logging.String("logger", "pubsub")),
|
|
|
|
network: network,
|
|
|
|
wg: &sync.WaitGroup{},
|
|
|
|
stop: make(chan bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-01 12:46:59 +00:00
|
|
|
func (p *pubSub) ticker(ctx context.Context, sub chan *events.Event) {
|
|
|
|
p.wg.Add(1)
|
|
|
|
ticker := time.NewTicker(3 * time.Second)
|
|
|
|
go func() {
|
|
|
|
var old *models.Info
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-p.stop:
|
|
|
|
ticker.Stop()
|
|
|
|
p.wg.Done()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
info, err := p.network.Info(ctx)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.Error("SubscribeInvoice returned an error", logging.Error(err))
|
|
|
|
}
|
|
|
|
if old != nil {
|
|
|
|
if old.BlockHeight != info.BlockHeight {
|
|
|
|
sub <- events.New(events.BlockReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
if old.NumPeers != info.NumPeers {
|
|
|
|
sub <- events.New(events.PeerUpdated)
|
|
|
|
}
|
|
|
|
|
|
|
|
if old.NumPendingChannels < info.NumPendingChannels {
|
|
|
|
sub <- events.New(events.ChannelPending)
|
|
|
|
}
|
|
|
|
|
|
|
|
if old.NumActiveChannels < info.NumActiveChannels {
|
|
|
|
sub <- events.New(events.ChannelActive)
|
|
|
|
}
|
|
|
|
|
|
|
|
if old.NumInactiveChannels < info.NumInactiveChannels {
|
|
|
|
sub <- events.New(events.ChannelInactive)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
old = info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-03-26 12:30:39 +00:00
|
|
|
func (p *pubSub) invoices(ctx context.Context, sub chan *events.Event) {
|
2019-04-01 07:57:21 +00:00
|
|
|
p.wg.Add(3)
|
2019-03-26 12:30:39 +00:00
|
|
|
invoices := make(chan *models.Invoice)
|
2019-03-29 17:03:51 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-03-26 12:30:39 +00:00
|
|
|
|
2019-03-29 16:02:52 +00:00
|
|
|
go func() {
|
|
|
|
for invoice := range invoices {
|
|
|
|
p.logger.Debug("receive invoice", logging.Object("invoice", invoice))
|
|
|
|
if invoice.Settled {
|
|
|
|
sub <- events.New(events.InvoiceSettled)
|
|
|
|
} else {
|
|
|
|
sub <- events.New(events.InvoiceCreated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.wg.Done()
|
|
|
|
}()
|
|
|
|
|
2019-03-26 12:30:39 +00:00
|
|
|
go func() {
|
2019-03-29 17:03:51 +00:00
|
|
|
err := p.network.SubscribeInvoice(ctx, invoices)
|
|
|
|
if err != nil {
|
|
|
|
p.logger.Error("SubscribeInvoice returned an error", logging.Error(err))
|
2019-03-26 12:30:39 +00:00
|
|
|
}
|
2019-04-01 07:57:21 +00:00
|
|
|
p.wg.Done()
|
2019-03-26 12:30:39 +00:00
|
|
|
}()
|
2019-03-29 17:03:51 +00:00
|
|
|
|
2019-04-01 07:57:21 +00:00
|
|
|
go func() {
|
|
|
|
<-p.stop
|
|
|
|
cancel()
|
|
|
|
close(invoices)
|
|
|
|
p.wg.Done()
|
|
|
|
}()
|
2019-03-26 12:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pubSub) wait() {
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt)
|
|
|
|
p.wg.Add(1)
|
|
|
|
go func() {
|
2019-03-29 16:02:52 +00:00
|
|
|
sig := <-c
|
|
|
|
p.logger.Debug("Received signal, gracefully stopping", logging.String("sig", sig.String()))
|
|
|
|
p.stop <- true
|
|
|
|
close(p.stop)
|
|
|
|
p.wg.Done()
|
2019-03-26 12:30:39 +00:00
|
|
|
}()
|
|
|
|
p.wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-03-29 16:30:48 +00:00
|
|
|
func Run(ctx context.Context, app *app.App, sub chan *events.Event) {
|
2019-03-26 12:30:39 +00:00
|
|
|
pubSub := newPubSub(app.Logger, app.Network)
|
|
|
|
pubSub.logger.Debug("Starting...")
|
2019-03-26 09:06:32 +00:00
|
|
|
|
2019-03-26 12:30:39 +00:00
|
|
|
pubSub.invoices(ctx, sub)
|
2019-04-01 12:46:59 +00:00
|
|
|
pubSub.ticker(ctx, sub)
|
2019-03-26 12:30:39 +00:00
|
|
|
pubSub.wait()
|
2019-03-26 09:06:32 +00:00
|
|
|
}
|