Skip to content

Commit 53bff9f

Browse files
author
Tíghearnán Carroll
authored
Fix: Re-establish socket connections on startup (#123)
* reestablish socket connection on start up * remove 32 bit
1 parent e0694a6 commit 53bff9f

File tree

15 files changed

+113
-19
lines changed

15 files changed

+113
-19
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ cover.html
1818
*.swp
1919
run
2020
.vscode
21+
22+
bin/

.goreleaser.yml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ archives:
2020
darwin: Darwin
2121
linux: Linux
2222
windows: Windows
23-
386: i386
2423
amd64: x86_64
2524
checksum:
2625
name_template: 'checksums.txt'

cmd/internal/internal.go

+35
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package internal
33
import (
44
"context"
55
"fmt"
6+
"net/url"
7+
"time"
68

79
"github.com/gorilla/websocket"
810
"github.com/labstack/echo/v4"
911
"github.com/labstack/echo/v4/middleware"
12+
"github.com/libsv/payd"
1013
"github.com/libsv/payd/config"
1114
paydSQL "github.com/libsv/payd/data/sqlite"
1215
"github.com/libsv/payd/docs"
@@ -16,6 +19,7 @@ import (
1619
paydMiddleware "github.com/libsv/payd/transports/http/middleware"
1720
tsoc "github.com/libsv/payd/transports/sockets"
1821
socMiddleware "github.com/libsv/payd/transports/sockets/middleware"
22+
"github.com/pkg/errors"
1923
"github.com/spf13/viper"
2024
echoSwagger "github.com/swaggo/echo-swagger"
2125
"github.com/theflyingcodr/sockets/client"
@@ -125,6 +129,37 @@ func ResumeActiveChannels(deps *SocketDeps) error {
125129
return nil
126130
}
127131

132+
// ResumeSocketConnections resume socket connections with the P4 host.
133+
func ResumeSocketConnections(deps *SocketDeps, cfg *config.P4) error {
134+
u, err := url.Parse(cfg.ServerHost)
135+
if err != nil {
136+
return errors.Wrap(err, "failed to parse p4 host")
137+
}
138+
139+
// No need to re-establish socket conn when running over http
140+
if u.Scheme != "ws" && u.Scheme != "wss" {
141+
return nil
142+
}
143+
144+
ctx := context.Background()
145+
invoices, err := deps.InvoiceService.Invoices(ctx)
146+
if err != nil {
147+
return errors.Wrap(err, "failed to retrieve invoices")
148+
}
149+
150+
for _, invoice := range invoices {
151+
if time.Now().UTC().Unix() <= invoice.ExpiresAt.Time.UTC().Unix() && invoice.State == payd.StateInvoicePending {
152+
if err := deps.ConnectService.Connect(ctx, payd.ConnectArgs{
153+
InvoiceID: invoice.ID,
154+
}); err != nil {
155+
return errors.Wrapf(err, "failed to connect invoice %s", invoice.ID)
156+
}
157+
}
158+
}
159+
160+
return nil
161+
}
162+
128163
func wsHandler(svr *server.SocketServer) echo.HandlerFunc {
129164
upgrader := websocket.Upgrader{}
130165
return func(c echo.Context) error {

cmd/server/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func main() {
101101
if err := internal.ResumeActiveChannels(deps); err != nil {
102102
log.Fatal(err, "failed to resume active peer channels")
103103
}
104+
if err := internal.ResumeSocketConnections(deps, cfg.P4); err != nil {
105+
log.Error(err, "failed to reconnect invoices with p4")
106+
}
104107

105108
if cfg.Deployment.IsDev() {
106109
internal.PrintDev(e)

data/sockets/connect.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ func NewConnect(cfg *config.P4, cli *client.Client) *connect {
2222

2323
// Connect will join payd with a socket server and kick off the payment process.
2424
func (c *connect) Connect(ctx context.Context, args payd.ConnectArgs) error {
25-
if err := c.cli.JoinChannel(c.cfg.ServerHost, args.InvoiceID, nil); err != nil {
25+
if err := c.cli.JoinChannel(c.cfg.ServerHost, args.InvoiceID, nil, map[string]string{
26+
"internal": "true",
27+
}); err != nil {
2628
return errors.Wrapf(err, "failed to connect to channel")
2729
}
2830
return nil

data/sockets/pay_async.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (c *paymentChannel) Pay(ctx context.Context, req payd.PayRequest) error {
3636
// parse url to get host connection and invoiceID
3737
parts := reURL.FindStringSubmatch(req.PayToURL)
3838
invoiceID := parts[2]
39-
if err := c.cli.JoinChannel(parts[1], invoiceID, nil); err != nil {
39+
if err := c.cli.JoinChannel(parts[1], invoiceID, nil, nil); err != nil {
4040
return errors.Wrapf(err, "failed to connect to channel %s", invoiceID)
4141
}
4242
// kick off the process - we will receive the messages via the socket transport listeners.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ require (
3333
github.com/gorilla/websocket v1.5.0
3434
github.com/libsv/go-bc v0.1.8
3535
github.com/rs/zerolog v1.26.1
36-
github.com/theflyingcodr/sockets v0.0.11-beta
36+
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5
3737
)
3838

3939
require (

go.sum

+2-6
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
484484
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
485485
github.com/ktrysmt/go-bitbucket v0.6.4/go.mod h1:9u0v3hsd2rqCHRIpbir1oP7F58uo5dq19sBYvuMoyQ4=
486486
github.com/labstack/echo/v4 v4.1.14/go.mod h1:Q5KZ1vD3V5FEzjM79hjwVrC3ABr7F5IdM23bXQMRDGg=
487-
github.com/labstack/echo/v4 v4.6.1/go.mod h1:RnjgMWNDB9g/HucVWhQYNQP9PvbYf6adqftqryo7s9k=
488487
github.com/labstack/echo/v4 v4.6.3 h1:VhPuIZYxsbPmo4m9KAkMU/el2442eB7EBFFhNTTT9ac=
489488
github.com/labstack/echo/v4 v4.6.3/go.mod h1:Hk5OiHj0kDqmFq7aHe7eDqI7CUhuCrfpupQtLGGLm7A=
490489
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
@@ -528,7 +527,6 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
528527
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
529528
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
530529
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
531-
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
532530
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
533531
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
534532
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
@@ -642,7 +640,6 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
642640
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
643641
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
644642
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
645-
github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo=
646643
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
647644
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
648645
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -710,8 +707,8 @@ github.com/theflyingcodr/lathos v0.0.6 h1:xIHMZTinurvodmFOgvSGD+OrDhSj42+Xz+FOXY
710707
github.com/theflyingcodr/lathos v0.0.6/go.mod h1:68tGFEbAqAzydWDb1KEJZPQY57l3hH32GXO11Hf1zGQ=
711708
github.com/theflyingcodr/migrate/v4 v4.15.1-0.20210927160112-79da889ca18e h1:gfOQ8DVRKwc97bXeR6I6ogosWhFi4mredpUtZcx/gvg=
712709
github.com/theflyingcodr/migrate/v4 v4.15.1-0.20210927160112-79da889ca18e/go.mod h1:g9qbiDvB47WyrRnNu2t2gMZFNHKnatsYRxsGZbCi4EM=
713-
github.com/theflyingcodr/sockets v0.0.11-beta h1:73rvasQ8aQkuzX1usPjAu9YyE73Kn6ffMfCucRGu1Pw=
714-
github.com/theflyingcodr/sockets v0.0.11-beta/go.mod h1:9WuWIyja/Q8PF3WmblAjFmfzkKabEBLto2Gx1XYnerc=
710+
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5 h1:39/z+O7p2ND6GgvOHZAr7o1gjm6UGls8FCcki8hbXRE=
711+
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5/go.mod h1:u4PMKd3yqHkt9Jn0VgQRZ33PG9ynL8/j53csVO1huyk=
715712
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
716713
github.com/tonicpow/go-minercraft v0.4.0 h1:o7ndFm0NDIWZ6ml5qXGzGIh7xhGDWQhQixjPCJec2PY=
717714
github.com/tonicpow/go-minercraft v0.4.0/go.mod h1:+mJZAtlRy89vbL/gLAH4kft46lxueHUGMhsBJF2E9Fg=
@@ -997,7 +994,6 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc
997994
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
998995
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
999996
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1000-
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1001997
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1002998
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1003999
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

service/health.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func (h *healthSvc) Health(ctx context.Context) error {
3131
}
3232
switch u.Scheme {
3333
case "ws", "wss":
34-
if err := h.c.JoinChannel(h.cfg.ServerHost, "health", nil); err != nil {
34+
if err := h.c.JoinChannel(h.cfg.ServerHost, "health", nil, map[string]string{
35+
"internal": "true",
36+
}); err != nil {
3537
return err
3638
}
3739
if err := h.c.Publish(sockets.Request{

vendor/github.com/theflyingcodr/sockets/client/client.go

+16-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/theflyingcodr/sockets/client/connections.go

+4-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/theflyingcodr/sockets/errors.go

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/theflyingcodr/sockets/server/server.go

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/theflyingcodr/sockets/socket.go

+7-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ github.com/theflyingcodr/govalidator
245245
## explicit; go 1.17
246246
github.com/theflyingcodr/lathos
247247
github.com/theflyingcodr/lathos/errs
248-
# github.com/theflyingcodr/sockets v0.0.11-beta
248+
# github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5
249249
## explicit; go 1.17
250250
github.com/theflyingcodr/sockets
251251
github.com/theflyingcodr/sockets/client

0 commit comments

Comments
 (0)