-
Notifications
You must be signed in to change notification settings - Fork 9
/
listener.go
37 lines (31 loc) · 861 Bytes
/
listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package lightning
import (
"time"
)
var InvoiceListeningTimeout = time.Minute * 150
// ListenForInvoices starts a goroutine that will repeatedly call waitanyinvoice.
// Each payment received will be fed into the client.PaymentHandler function.
// You can change that function in the meantime.
// Or you can set it to nil if you want to stop listening for invoices.
func (ln *Client) ListenForInvoices() {
go func() {
for {
if ln.PaymentHandler == nil {
return
}
res, err := ln.CallWithCustomTimeout(InvoiceListeningTimeout,
"waitanyinvoice", ln.LastInvoiceIndex)
if err != nil {
if _, ok := err.(ErrorTimeout); ok {
time.Sleep(time.Minute)
} else {
time.Sleep(5 * time.Second)
}
continue
}
index := res.Get("pay_index").Int()
ln.LastInvoiceIndex = int(index)
ln.PaymentHandler(res)
}
}()
}