Skip to content

Commit

Permalink
fixes: add quit chan, use enum, set map type
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazic committed Jan 25, 2024
1 parent d0e3e38 commit d52b3b7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
24 changes: 18 additions & 6 deletions collectors/channels_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const closedChannelsKey = "closedChannels"

// ChannelsCollector is a collector that keeps track of channel information.
type ChannelsCollector struct {
channelBalanceDesc *prometheus.Desc
Expand Down Expand Up @@ -54,9 +56,12 @@ type ChannelsCollector struct {
// This channel should be buffered so that it does not block sends.
errChan chan<- error

// quit is a channel that we use to signal for graceful shutdown
quit chan struct{}

// cache is a map storing results from a ticker to reduce grpc server
// load on lnd
cache map[string]interface{}
cache map[string][]lndclient.ClosedChannel
cacheMutex sync.RWMutex
}

Expand Down Expand Up @@ -183,7 +188,9 @@ func NewChannelsCollector(lnd lndclient.LightningClient, errChan chan<- error,

lnd: lnd,
primaryNode: cfg.PrimaryNode,
cache: make(map[string][]lndclient.ClosedChannel),
errChan: errChan,
quit: make(chan struct{}),
}

// Perform an initial refresh for the cache
Expand All @@ -193,8 +200,13 @@ func NewChannelsCollector(lnd lndclient.LightningClient, errChan chan<- error,
go func() {
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()
for range ticker.C {
collector.refreshClosedChannelsCache()
for {
select {
case <-ticker.C:
collector.refreshClosedChannelsCache()
case <-collector.quit:
return
}
}
}()

Expand All @@ -208,7 +220,7 @@ func (c *ChannelsCollector) refreshClosedChannelsCache() {
return
}
c.cacheMutex.Lock()
c.cache["closedChannels"] = data
c.cache[closedChannelsKey] = data
c.cacheMutex.Unlock()
}

Expand Down Expand Up @@ -485,11 +497,11 @@ func (c *ChannelsCollector) Collect(ch chan<- prometheus.Metric) {

// Get the list of closed channels.
c.cacheMutex.RLock()
closedChannelsResp, ok := c.cache["closedChannels"].([]*lndclient.ClosedChannel)
closedChannelsResp, ok := c.cache[closedChannelsKey]
if !ok {
// Cache is not initialized
Logger.Warn("ClosedChannels not in cache")
closedChannelsResp = []*lndclient.ClosedChannel{}
closedChannelsResp = []lndclient.ClosedChannel{}
}
c.cacheMutex.RUnlock()
closeCounts := make(map[string]int)
Expand Down
18 changes: 15 additions & 3 deletions collectors/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/btcsuite/btcd/btcutil"
"github.com/lightninglabs/lndclient"
Expand Down Expand Up @@ -94,11 +96,21 @@ func NewPrometheusExporter(cfg *PrometheusConfig, lnd *lndclient.LndServices,

htlcMonitor := newHtlcMonitor(lnd.Router, errChan)

// Setup signalling so SIGTERM || SIGINT will stop the cache goroutine
// from ChannelsCollector
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
chanCollector := NewChannelsCollector(
lnd.Client, errChan, monitoringCfg,
)
go func() {
<-sigs
close(chanCollector.quit)
}()

collectors := []prometheus.Collector{
NewChainCollector(lnd.Client, errChan),
NewChannelsCollector(
lnd.Client, errChan, monitoringCfg,
),
chanCollector,
NewWalletCollector(lnd, errChan),
NewPeerCollector(lnd.Client, errChan),
NewInfoCollector(lnd.Client, errChan),
Expand Down
Binary file added lndmon
Binary file not shown.

0 comments on commit d52b3b7

Please sign in to comment.