added ticker to remove expired invoices periodically
This commit is contained in:
parent
e525828939
commit
7343edf756
|
@ -33,7 +33,7 @@ type config struct {
|
||||||
|
|
||||||
RESTHost string `long:"resthost" Description:"Host for the rest interface of LightningTip"`
|
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"`
|
LND *backends.LND `group:"LND" namespace:"lnd"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,7 +115,7 @@
|
||||||
float: right;
|
float: right;
|
||||||
}
|
}
|
||||||
|
|
||||||
#lightningTipThankYou {
|
#lightningTipFinished {
|
||||||
margin-bottom: 0.2em;
|
margin-bottom: 0.2em;
|
||||||
|
|
||||||
display: block;
|
display: block;
|
||||||
|
|
|
@ -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
|
// To prohibit multiple requests at the same time
|
||||||
var running = false;
|
var running = false;
|
||||||
|
@ -109,7 +110,7 @@ function getInvoice() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function listenInvoiceSettled(hash) {
|
function listenInvoiceSettled(hash) {
|
||||||
if (EventSource === undefined) {
|
if (EventSource !== undefined) {
|
||||||
var eventSrc = new EventSource(requestUrl + "eventsource");
|
var eventSrc = new EventSource(requestUrl + "eventsource");
|
||||||
|
|
||||||
eventSrc.onmessage = function (event) {
|
eventSrc.onmessage = function (event) {
|
||||||
|
@ -154,19 +155,21 @@ function showThankYouScreen() {
|
||||||
var wrapper = document.getElementById("lightningTip");
|
var wrapper = document.getElementById("lightningTip");
|
||||||
|
|
||||||
wrapper.innerHTML = "<p id=\"lightningTipLogo\">⚡</p>";
|
wrapper.innerHTML = "<p id=\"lightningTipLogo\">⚡</p>";
|
||||||
wrapper.innerHTML += "<a id='lightningTipThankYou'>Thank you for your tip!</a>";
|
wrapper.innerHTML += "<a id='lightningTipFinished'>Thank you for your tip!</a>";
|
||||||
}
|
}
|
||||||
|
|
||||||
function starTimer(duration, element) {
|
function starTimer(duration, element) {
|
||||||
showTimer(duration, element);
|
showTimer(duration, element);
|
||||||
|
|
||||||
var interval = setInterval(function () {
|
var interval = setInterval(function () {
|
||||||
if (duration > 0) {
|
if (duration > 1) {
|
||||||
duration--;
|
duration--;
|
||||||
|
|
||||||
showTimer(duration, element);
|
showTimer(duration, element);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
showExpired();
|
||||||
|
|
||||||
clearInterval(interval);
|
clearInterval(interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,6 +194,13 @@ function showTimer(duration, element) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function showExpired() {
|
||||||
|
var wrapper = document.getElementById("lightningTip");
|
||||||
|
|
||||||
|
wrapper.innerHTML = "<p id=\"lightningTipLogo\">⚡</p>";
|
||||||
|
wrapper.innerHTML += "<a id='lightningTipFinished'>Your invoice expired!</a>";
|
||||||
|
}
|
||||||
|
|
||||||
function addLeadingZeros(value) {
|
function addLeadingZeros(value) {
|
||||||
return ("0" + value).slice(-2);
|
return ("0" + value).slice(-2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,6 +75,35 @@ func main() {
|
||||||
// Alternative for browsers which don't support EventSource (Internet Explorer and Edge)
|
// Alternative for browsers which don't support EventSource (Internet Explorer and Edge)
|
||||||
http.HandleFunc("/invoicesettled", invoiceSettledHandler)
|
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")
|
log.Info("Subscribing to invoices")
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -194,7 +223,6 @@ func getInvoiceHandler(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
|
||||||
log.Info(logMessage)
|
log.Info(logMessage)
|
||||||
|
|
||||||
// TODO: check every minute or so if expired
|
|
||||||
pendingInvoices = append(pendingInvoices, PendingInvoice{
|
pendingInvoices = append(pendingInvoices, PendingInvoice{
|
||||||
Invoice: invoice,
|
Invoice: invoice,
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
|
|
Loading…
Reference in New Issue