From 7343edf756b17d819496feb1f2128d269b89c6cb Mon Sep 17 00:00:00 2001 From: michael1011 Date: Thu, 29 Mar 2018 13:06:28 +0200 Subject: [PATCH] added ticker to remove expired invoices periodically --- config.go | 2 +- frontend/lightningTip.css | 2 +- frontend/lightningTip.js | 18 ++++++++++++++---- lightningtip.go | 30 +++++++++++++++++++++++++++++- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 8b49ec0..9638a60 100644 --- a/config.go +++ b/config.go @@ -33,7 +33,7 @@ type config struct { RESTHost string `long:"resthost" Description:"Host for the rest interface of LightningTip"` - TipExpiry int64 `long:"tipexpiry" Description:"Expiry time in seconds"` + TipExpiry int64 `long:"tipexpiry" Description:"Invoice expiry time in seconds"` LND *backends.LND `group:"LND" namespace:"lnd"` } diff --git a/frontend/lightningTip.css b/frontend/lightningTip.css index b12cef2..f6a0ee5 100644 --- a/frontend/lightningTip.css +++ b/frontend/lightningTip.css @@ -115,7 +115,7 @@ float: right; } -#lightningTipThankYou { +#lightningTipFinished { margin-bottom: 0.2em; display: block; diff --git a/frontend/lightningTip.js b/frontend/lightningTip.js index 06c0a7c..8da814d 100644 --- a/frontend/lightningTip.js +++ b/frontend/lightningTip.js @@ -1,4 +1,5 @@ -var requestUrl = window.location.protocol + "//" + window.location.hostname + ":8081/"; +//var requestUrl = window.location.protocol + "//" + window.location.hostname + ":8081/"; +var requestUrl = "http://localhost:8081/"; // To prohibit multiple requests at the same time var running = false; @@ -109,7 +110,7 @@ function getInvoice() { } function listenInvoiceSettled(hash) { - if (EventSource === undefined) { + if (EventSource !== undefined) { var eventSrc = new EventSource(requestUrl + "eventsource"); eventSrc.onmessage = function (event) { @@ -154,19 +155,21 @@ function showThankYouScreen() { var wrapper = document.getElementById("lightningTip"); wrapper.innerHTML = "

"; - wrapper.innerHTML += "Thank you for your tip!"; + wrapper.innerHTML += "Thank you for your tip!"; } function starTimer(duration, element) { showTimer(duration, element); var interval = setInterval(function () { - if (duration > 0) { + if (duration > 1) { duration--; showTimer(duration, element); } else { + showExpired(); + clearInterval(interval); } @@ -191,6 +194,13 @@ function showTimer(duration, element) { } +function showExpired() { + var wrapper = document.getElementById("lightningTip"); + + wrapper.innerHTML = "

"; + wrapper.innerHTML += "Your invoice expired!"; +} + function addLeadingZeros(value) { return ("0" + value).slice(-2); } diff --git a/lightningtip.go b/lightningtip.go index 3eaba4b..3bbc3f7 100644 --- a/lightningtip.go +++ b/lightningtip.go @@ -75,6 +75,35 @@ func main() { // Alternative for browsers which don't support EventSource (Internet Explorer and Edge) http.HandleFunc("/invoicesettled", invoiceSettledHandler) + log.Debug("Starting ticker to clear expired invoices") + + // A bit longer than the expiry time to make sure the invoice doesn't show as settled if it isn't (affects just invoiceSettledHandler) + duration := time.Duration(cfg.TipExpiry + 10) + ticker := time.Tick(duration * time.Second) + + go func() { + for { + select { + case <-ticker: + now := time.Now() + + for index := len(pendingInvoices) - 1; index >= 0; index-- { + invoice := pendingInvoices[index] + + if now.Sub(invoice.Expiry) > 0 { + log.Debug("Invoice expired: " + invoice.Invoice) + + pendingInvoices = append(pendingInvoices[:index], pendingInvoices[index+1:]...) + } + + } + + } + + } + + }() + log.Info("Subscribing to invoices") go func() { @@ -194,7 +223,6 @@ func getInvoiceHandler(writer http.ResponseWriter, request *http.Request) { log.Info(logMessage) - // TODO: check every minute or so if expired pendingInvoices = append(pendingInvoices, PendingInvoice{ Invoice: invoice, Hash: hash,