Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions internal/lnbits/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package lnbits

import (
"context"
"net/http"
"sync"
"time"

"github.com/imroc/req"
log "github.com/sirupsen/logrus"
)

const (
// requestTimeout bounds every LNbits call except payments, so users get a
// fast answer instead of a frozen bot when LNbits stops responding.
requestTimeout = 15 * time.Second
// paymentTimeout is long on purpose: a payment can take a while to settle
// and aborting it early would leave its outcome unknown.
paymentTimeout = 24 * time.Hour
// probeInterval is how often the background check pings LNbits.
probeInterval = 15 * time.Second
probeTimeout = 10 * time.Second
)

// Health tracks whether LNbits is reachable. It is updated by every request
// the client makes and by a background probe, so the bot can show a
// maintenance message instead of wrong data (like a 0 balance) when LNbits
// is down.
type Health struct {
mu sync.RWMutex
down bool
downSince time.Time
lastErr string
// onChange is called on every switch between up and down, see
// Client.OnHealthChange.
onChange func(down bool, reason string, since time.Time)
}

// Available reports whether LNbits is currently reachable. A nil Health is
// always available.
func (h *Health) Available() bool {
if h == nil {
return true
}
h.mu.RLock()
defer h.mu.RUnlock()
return !h.down
}

func (h *Health) markDown(reason string) {
if h == nil {
return
}
h.mu.Lock()
defer h.mu.Unlock()
h.lastErr = reason
if h.down {
return
}
h.down = true
h.downSince = time.Now()
log.Errorf("[lnbits] LNbits is unreachable, entering maintenance mode: %s", reason)
if h.onChange != nil {
h.onChange(true, reason, h.downSince)
}
}

func (h *Health) markUp() {
if h == nil {
return
}
h.mu.Lock()
defer h.mu.Unlock()
if !h.down {
return
}
log.Warnf("[lnbits] LNbits is reachable again after %s, leaving maintenance mode", time.Since(h.downSince).Round(time.Second))
h.down = false
h.lastErr = ""
if h.onChange != nil {
h.onChange(false, "", h.downSince)
}
}

// isOutageStatus reports whether an HTTP status means LNbits itself is down,
// as opposed to rejecting one request. 502/503/504 come from the reverse proxy
// in front of LNbits when LNbits does not answer.
func isOutageStatus(code int) bool {
return code == http.StatusBadGateway ||
code == http.StatusServiceUnavailable ||
code == http.StatusGatewayTimeout
}

// healthTransport records the outcome of every LNbits request in Health.
// Connection errors, timeouts and gateway errors mark LNbits as down; any
// other response means LNbits answered, so it is up.
type healthTransport struct {
next http.RoundTripper
health *Health
}

func (t *healthTransport) RoundTrip(r *http.Request) (*http.Response, error) {
resp, err := t.next.RoundTrip(r)
switch {
case err != nil:
t.health.markDown(err.Error())
case isOutageStatus(resp.StatusCode):
t.health.markDown(resp.Status)
default:
t.health.markUp()
}
return resp, err
}

// newHTTP returns a req instance whose requests are tracked by health.
func newHTTP(health *Health, timeout time.Duration) *req.Req {
r := req.New()
r.SetClient(&http.Client{
Transport: &healthTransport{next: http.DefaultTransport.(*http.Transport).Clone(), health: health},
Timeout: timeout,
})
return r
}

// Available reports whether LNbits is currently reachable.
func (c *Client) Available() bool {
return c.health.Available()
}

// OnHealthChange registers fn to be called each time LNbits goes down
// (down=true, with the error) or comes back up (down=false). since is when
// the outage started. fn runs under the health lock, so calls arrive in
// order, but it must return quickly and must not call back into the client.
func (c *Client) OnHealthChange(fn func(down bool, reason string, since time.Time)) {
if c.health == nil {
return
}
c.health.mu.Lock()
defer c.health.mu.Unlock()
c.health.onChange = fn
}

// StartHealthCheck pings LNbits in the background until ctx is done, so the
// bot notices an outage, and the recovery, even while no user is active.
func (c *Client) StartHealthCheck(ctx context.Context) {
probe := newHTTP(c.health, probeTimeout)
go func() {
t := time.NewTicker(probeInterval)
defer t.Stop()
for {
// The transport records the result; the response itself is unused.
// Any non-gateway answer, even a 404, proves LNbits is up.
if resp, err := probe.Get(c.url + "/api/v1/health"); err == nil {
resp.Response().Body.Close()
}
select {
case <-ctx.Done():
return
case <-t.C:
}
}
}()
}

// httpClient returns the tracked client for normal calls. A Client built
// without NewClient (e.g. in tests) falls back to an untracked one.
func (c Client) httpClient() *req.Req {
if c.http == nil {
return newHTTP(nil, requestTimeout)
}
return c.http
}

// payClient returns the tracked client for payments, see httpClient.
func (c Client) payClient() *req.Req {
if c.payHTTP == nil {
return newHTTP(nil, paymentTimeout)
}
return c.payHTTP
}
104 changes: 104 additions & 0 deletions internal/lnbits/health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package lnbits

import (
"context"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
)

func TestHealthTracksRequests(t *testing.T) {
var status int32
atomic.StoreInt32(&status, http.StatusOK)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(int(atomic.LoadInt32(&status)))
}))
defer srv.Close()

c := NewClient("key", "token", srv.URL)
w := Wallet{Inkey: "k"}

steps := []struct {
name string
status int
up bool
}{
{"ok response", http.StatusOK, true},
{"gateway error means down", http.StatusBadGateway, false},
{"plain 404 means up again", http.StatusNotFound, true},
{"503 means down", http.StatusServiceUnavailable, false},
{"request error is not an outage", http.StatusBadRequest, true},
{"504 means down", http.StatusGatewayTimeout, false},
{"500 is a request error, not an outage", http.StatusInternalServerError, true},
}
for _, s := range steps {
atomic.StoreInt32(&status, int32(s.status))
c.Info(w)
if got := c.Available(); got != s.up {
t.Fatalf("%s: Available() = %v, want %v", s.name, got, s.up)
}
}
}

func TestHealthMarksDownWhenUnreachable(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
url := srv.URL
srv.Close()

c := NewClient("key", "token", url)
if _, err := c.Info(Wallet{Inkey: "k"}); err == nil {
t.Fatal("expected an error from a closed server")
}
if c.Available() {
t.Fatal("Available() = true after connection error, want false")
}
}

func TestHealthCheckRecovers(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/health" {
t.Errorf("probe hit %s, want /api/v1/health", r.URL.Path)
}
}))
defer srv.Close()

c := NewClient("key", "token", srv.URL)
c.health.markDown("test")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c.StartHealthCheck(ctx)

deadline := time.Now().Add(5 * time.Second)
for !c.Available() {
if time.Now().After(deadline) {
t.Fatal("health check did not mark LNbits as up")
}
time.Sleep(10 * time.Millisecond)
}
}

func TestZeroClientIsAvailable(t *testing.T) {
if !(&Client{}).Available() {
t.Fatal("a Client without health tracking should report available")
}
}

func TestHealthChangeCallback(t *testing.T) {
c := NewClient("key", "token", "http://unused")
var events []bool
c.OnHealthChange(func(down bool, reason string, since time.Time) {
events = append(events, down)
})

c.health.markDown("a")
c.health.markDown("b") // already down: no new event
c.health.markUp()
c.health.markUp() // already up: no new event

if len(events) != 2 || events[0] != true || events[1] != false {
t.Fatalf("events = %v, want [true false]", events)
}
}
29 changes: 15 additions & 14 deletions internal/lnbits/lnbits.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lnbits

import (
"fmt"
"time"

"github.com/imroc/req"
)
Expand All @@ -24,8 +23,12 @@ func parseLNbitsError(resp *req.Resp) Error {
// NewClient returns a new lnbits api client. Pass your API key, the ACL token
// used for account management, and the url here.
func NewClient(key, adminToken, url string) *Client {
health := &Health{}
return &Client{
url: url,
url: url,
health: health,
http: newHTTP(health, requestTimeout),
payHTTP: newHTTP(health, paymentTimeout),
// info: this header holds the ADMIN key for the entire API
// it can be used to create wallets for example
// if you want to check the balance of a user, use w.Inkey
Expand All @@ -45,7 +48,7 @@ func NewClient(key, adminToken, url string) *Client {

// GetUser returns user information
func (c *Client) GetUser(userId string) (user User, err error) {
resp, err := req.Get(c.url+"/users/api/v1/user/"+userId, c.adminHeader, nil)
resp, err := c.httpClient().Get(c.url+"/users/api/v1/user/"+userId, c.adminHeader, nil)
if err != nil {
return
}
Expand All @@ -71,7 +74,7 @@ func (c *Client) GetUser(userId string) (user User, err error) {
func (c *Client) CreateUserWithInitialWallet(userName, walletName, adminId string, email string) (wal User, err error) {
// email is deliberately not sent: the bot passes a telegram handle here,
// which lnbits 1.x rejects via is_valid_email_address.
resp, err := req.Post(c.url+"/users/api/v1/user", c.adminHeader, req.BodyJSON(struct {
resp, err := c.httpClient().Post(c.url+"/users/api/v1/user", c.adminHeader, req.BodyJSON(struct {
UserName string `json:"username"`
}{userName}))
if err != nil {
Expand Down Expand Up @@ -107,7 +110,7 @@ func (c *Client) CreateUserWithInitialWallet(userName, walletName, adminId strin
// renameWallet sets a wallet's name. It authenticates with the wallet's own
// admin key rather than the ACL token, so it stays outside that token's scope.
func (c *Client) renameWallet(w Wallet, name string) error {
resp, err := req.Patch(c.url+"/api/v1/wallet", req.Header{
resp, err := c.httpClient().Patch(c.url+"/api/v1/wallet", req.Header{
"Content-Type": "application/json",
"Accept": "application/json",
"X-Api-Key": w.Adminkey,
Expand All @@ -127,7 +130,7 @@ func (c *Client) renameWallet(w Wallet, name string) error {
//
// adminId is unused, see CreateUserWithInitialWallet.
func (c *Client) CreateWallet(userId, walletName, adminId string) (wal Wallet, err error) {
resp, err := req.Post(c.url+"/users/api/v1/user/"+userId+"/wallet", c.adminHeader,
resp, err := c.httpClient().Post(c.url+"/users/api/v1/user/"+userId+"/wallet", c.adminHeader,
req.BodyJSON(struct {
Name string `json:"name"`
}{walletName}))
Expand All @@ -151,7 +154,7 @@ func (w Wallet) Invoice(params InvoiceParams, c *Client) (lntx Invoice, err erro
"Accept": "application/json",
"X-Api-Key": w.Inkey,
}
resp, err := req.Post(c.url+"/api/v1/payments", invoiceHeader, req.BodyJSON(&params))
resp, err := c.httpClient().Post(c.url+"/api/v1/payments", invoiceHeader, req.BodyJSON(&params))
if err != nil {
return
}
Expand All @@ -173,7 +176,7 @@ func (c Client) Info(w Wallet) (wtx Wallet, err error) {
"Accept": "application/json",
"X-Api-Key": w.Inkey,
}
resp, err := req.Get(c.url+"/api/v1/wallet", invoiceHeader, nil)
resp, err := c.httpClient().Get(c.url+"/api/v1/wallet", invoiceHeader, nil)
if err != nil {
return
}
Expand Down Expand Up @@ -201,7 +204,7 @@ func (c Client) PaymentsWithOptions(w Wallet, limit, offset int) (wtx Payments,
"X-Api-Key": w.Inkey,
}
url := fmt.Sprintf("%s/api/v1/payments?limit=%d&offset=%d", c.url, limit, offset)
resp, err := req.Get(url, invoiceHeader, nil)
resp, err := c.httpClient().Get(url, invoiceHeader, nil)
if err != nil {
return
}
Expand All @@ -223,7 +226,7 @@ func (c Client) Payment(w Wallet, payment_hash string) (payment LNbitsPayment, e
"Accept": "application/json",
"X-Api-Key": w.Inkey,
}
resp, err := req.Get(c.url+fmt.Sprintf("/api/v1/payments/%s", payment_hash), invoiceHeader, nil)
resp, err := c.httpClient().Get(c.url+fmt.Sprintf("/api/v1/payments/%s", payment_hash), invoiceHeader, nil)
if err != nil {
return
}
Expand All @@ -239,7 +242,7 @@ func (c Client) Payment(w Wallet, payment_hash string) (payment LNbitsPayment, e

// Wallets returns all wallets belonging to an user
func (c Client) Wallets(w User) (wtx []Wallet, err error) {
resp, err := req.Get(c.url+"/users/api/v1/user/"+w.ID+"/wallet", c.adminHeader, nil)
resp, err := c.httpClient().Get(c.url+"/users/api/v1/user/"+w.ID+"/wallet", c.adminHeader, nil)
if err != nil {
return
}
Expand All @@ -261,9 +264,7 @@ func (w Wallet) Pay(params PaymentParams, c *Client) (wtx Invoice, err error) {
"Accept": "application/json",
"X-Api-Key": w.Adminkey,
}
r := req.New()
r.SetTimeout(time.Hour * 24)
resp, err := r.Post(c.url+"/api/v1/payments", adminHeader, req.BodyJSON(&params))
resp, err := c.payClient().Post(c.url+"/api/v1/payments", adminHeader, req.BodyJSON(&params))
if err != nil {
return
}
Expand Down
Loading
Loading