mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-22 07:04:56 +01:00
handle close messages
This commit is contained in:
@@ -35,7 +35,6 @@ func (controller *InvoiceStreamController) StreamInvoices(c echo.Context) error
|
|||||||
invoiceChan := make(chan models.Invoice)
|
invoiceChan := make(chan models.Invoice)
|
||||||
reqId := c.Response().Header().Get(echo.HeaderXRequestID)
|
reqId := c.Response().Header().Get(echo.HeaderXRequestID)
|
||||||
controller.svc.InvoicePubSub.Subscribe(reqId, userId, invoiceChan)
|
controller.svc.InvoicePubSub.Subscribe(reqId, userId, invoiceChan)
|
||||||
ctx := c.Request().Context()
|
|
||||||
upgrader := websocket.Upgrader{}
|
upgrader := websocket.Upgrader{}
|
||||||
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
@@ -44,6 +43,19 @@ func (controller *InvoiceStreamController) StreamInvoices(c echo.Context) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer ws.Close()
|
defer ws.Close()
|
||||||
|
|
||||||
|
//start listening for close messages
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
defer close(done)
|
||||||
|
for {
|
||||||
|
_, _, err := ws.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
//start with keepalive message
|
//start with keepalive message
|
||||||
err = ws.WriteJSON(&InvoiceEventWrapper{Type: "keepalive"})
|
err = ws.WriteJSON(&InvoiceEventWrapper{Type: "keepalive"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -53,7 +65,7 @@ func (controller *InvoiceStreamController) StreamInvoices(c echo.Context) error
|
|||||||
SocketLoop:
|
SocketLoop:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-done:
|
||||||
break SocketLoop
|
break SocketLoop
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
err := ws.WriteJSON(&InvoiceEventWrapper{Type: "keepalive"})
|
err := ws.WriteJSON(&InvoiceEventWrapper{Type: "keepalive"})
|
||||||
@@ -81,6 +93,5 @@ SocketLoop:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
controller.svc.InvoicePubSub.Unsubscribe(reqId, userId)
|
return controller.svc.InvoicePubSub.Unsubscribe(reqId, userId)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,10 +26,12 @@ func (ps *Pubsub) Subscribe(id string, topic int64, ch chan models.Invoice) {
|
|||||||
ps.subs[topic][id] = ch
|
ps.subs[topic][id] = ch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *Pubsub) Unsubscribe(id string, topic int64) {
|
func (ps *Pubsub) Unsubscribe(id string, topic int64) error {
|
||||||
ps.mu.Lock()
|
ps.mu.Lock()
|
||||||
defer ps.mu.Unlock()
|
defer ps.mu.Unlock()
|
||||||
|
close(ps.subs[topic][id])
|
||||||
delete(ps.subs[topic], id)
|
delete(ps.subs[topic], id)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *Pubsub) Publish(topic int64, msg models.Invoice) {
|
func (ps *Pubsub) Publish(topic int64, msg models.Invoice) {
|
||||||
@@ -40,11 +42,3 @@ func (ps *Pubsub) Publish(topic int64, msg models.Invoice) {
|
|||||||
ch <- msg
|
ch <- msg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ps *Pubsub) CloseAll() {
|
|
||||||
for _, subs := range ps.subs {
|
|
||||||
for _, ch := range subs {
|
|
||||||
close(ch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -202,8 +202,6 @@ func main() {
|
|||||||
if err := e.Shutdown(ctx); err != nil {
|
if err := e.Shutdown(ctx); err != nil {
|
||||||
e.Logger.Fatal(err)
|
e.Logger.Fatal(err)
|
||||||
}
|
}
|
||||||
//close all channels
|
|
||||||
svc.InvoicePubSub.CloseAll()
|
|
||||||
if echoPrometheus != nil {
|
if echoPrometheus != nil {
|
||||||
if err := echoPrometheus.Shutdown(ctx); err != nil {
|
if err := echoPrometheus.Shutdown(ctx); err != nil {
|
||||||
e.Logger.Fatal(err)
|
e.Logger.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user