Skip to content

Commit

Permalink
Fix: Re-establish socket connections on startup (#123)
Browse files Browse the repository at this point in the history
* reestablish socket connection on start up

* remove 32 bit
  • Loading branch information
Tíghearnán Carroll authored Feb 23, 2022
1 parent e0694a6 commit 53bff9f
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ cover.html
*.swp
run
.vscode

bin/
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ archives:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
Expand Down
35 changes: 35 additions & 0 deletions cmd/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package internal
import (
"context"
"fmt"
"net/url"
"time"

"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/libsv/payd"
"github.com/libsv/payd/config"
paydSQL "github.com/libsv/payd/data/sqlite"
"github.com/libsv/payd/docs"
Expand All @@ -16,6 +19,7 @@ import (
paydMiddleware "github.com/libsv/payd/transports/http/middleware"
tsoc "github.com/libsv/payd/transports/sockets"
socMiddleware "github.com/libsv/payd/transports/sockets/middleware"
"github.com/pkg/errors"
"github.com/spf13/viper"
echoSwagger "github.com/swaggo/echo-swagger"
"github.com/theflyingcodr/sockets/client"
Expand Down Expand Up @@ -125,6 +129,37 @@ func ResumeActiveChannels(deps *SocketDeps) error {
return nil
}

// ResumeSocketConnections resume socket connections with the P4 host.
func ResumeSocketConnections(deps *SocketDeps, cfg *config.P4) error {
u, err := url.Parse(cfg.ServerHost)
if err != nil {
return errors.Wrap(err, "failed to parse p4 host")
}

// No need to re-establish socket conn when running over http
if u.Scheme != "ws" && u.Scheme != "wss" {
return nil
}

ctx := context.Background()
invoices, err := deps.InvoiceService.Invoices(ctx)
if err != nil {
return errors.Wrap(err, "failed to retrieve invoices")
}

for _, invoice := range invoices {
if time.Now().UTC().Unix() <= invoice.ExpiresAt.Time.UTC().Unix() && invoice.State == payd.StateInvoicePending {
if err := deps.ConnectService.Connect(ctx, payd.ConnectArgs{
InvoiceID: invoice.ID,
}); err != nil {
return errors.Wrapf(err, "failed to connect invoice %s", invoice.ID)
}
}
}

return nil
}

func wsHandler(svr *server.SocketServer) echo.HandlerFunc {
upgrader := websocket.Upgrader{}
return func(c echo.Context) error {
Expand Down
3 changes: 3 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func main() {
if err := internal.ResumeActiveChannels(deps); err != nil {
log.Fatal(err, "failed to resume active peer channels")
}
if err := internal.ResumeSocketConnections(deps, cfg.P4); err != nil {
log.Error(err, "failed to reconnect invoices with p4")
}

if cfg.Deployment.IsDev() {
internal.PrintDev(e)
Expand Down
4 changes: 3 additions & 1 deletion data/sockets/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ func NewConnect(cfg *config.P4, cli *client.Client) *connect {

// Connect will join payd with a socket server and kick off the payment process.
func (c *connect) Connect(ctx context.Context, args payd.ConnectArgs) error {
if err := c.cli.JoinChannel(c.cfg.ServerHost, args.InvoiceID, nil); err != nil {
if err := c.cli.JoinChannel(c.cfg.ServerHost, args.InvoiceID, nil, map[string]string{
"internal": "true",
}); err != nil {
return errors.Wrapf(err, "failed to connect to channel")
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion data/sockets/pay_async.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *paymentChannel) Pay(ctx context.Context, req payd.PayRequest) error {
// parse url to get host connection and invoiceID
parts := reURL.FindStringSubmatch(req.PayToURL)
invoiceID := parts[2]
if err := c.cli.JoinChannel(parts[1], invoiceID, nil); err != nil {
if err := c.cli.JoinChannel(parts[1], invoiceID, nil, nil); err != nil {
return errors.Wrapf(err, "failed to connect to channel %s", invoiceID)
}
// kick off the process - we will receive the messages via the socket transport listeners.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/libsv/go-bc v0.1.8
github.com/rs/zerolog v1.26.1
github.com/theflyingcodr/sockets v0.0.11-beta
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5
)

require (
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/ktrysmt/go-bitbucket v0.6.4/go.mod h1:9u0v3hsd2rqCHRIpbir1oP7F58uo5dq19sBYvuMoyQ4=
github.com/labstack/echo/v4 v4.1.14/go.mod h1:Q5KZ1vD3V5FEzjM79hjwVrC3ABr7F5IdM23bXQMRDGg=
github.com/labstack/echo/v4 v4.6.1/go.mod h1:RnjgMWNDB9g/HucVWhQYNQP9PvbYf6adqftqryo7s9k=
github.com/labstack/echo/v4 v4.6.3 h1:VhPuIZYxsbPmo4m9KAkMU/el2442eB7EBFFhNTTT9ac=
github.com/labstack/echo/v4 v4.6.3/go.mod h1:Hk5OiHj0kDqmFq7aHe7eDqI7CUhuCrfpupQtLGGLm7A=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
Expand Down Expand Up @@ -528,7 +527,6 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
Expand Down Expand Up @@ -642,7 +640,6 @@ github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/rs/zerolog v1.26.0/go.mod h1:yBiM87lvSqX8h0Ww4sdzNSkVYZ8dL2xjZJG1lAuGZEo=
github.com/rs/zerolog v1.26.1 h1:/ihwxqH+4z8UxyI70wM1z9yCvkWcfz/a3mj48k/Zngc=
github.com/rs/zerolog v1.26.1/go.mod h1:/wSSJWX7lVrsOwlbyTRSOJvqRlc+WjWlfes+CiJ+tmc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -710,8 +707,8 @@ github.com/theflyingcodr/lathos v0.0.6 h1:xIHMZTinurvodmFOgvSGD+OrDhSj42+Xz+FOXY
github.com/theflyingcodr/lathos v0.0.6/go.mod h1:68tGFEbAqAzydWDb1KEJZPQY57l3hH32GXO11Hf1zGQ=
github.com/theflyingcodr/migrate/v4 v4.15.1-0.20210927160112-79da889ca18e h1:gfOQ8DVRKwc97bXeR6I6ogosWhFi4mredpUtZcx/gvg=
github.com/theflyingcodr/migrate/v4 v4.15.1-0.20210927160112-79da889ca18e/go.mod h1:g9qbiDvB47WyrRnNu2t2gMZFNHKnatsYRxsGZbCi4EM=
github.com/theflyingcodr/sockets v0.0.11-beta h1:73rvasQ8aQkuzX1usPjAu9YyE73Kn6ffMfCucRGu1Pw=
github.com/theflyingcodr/sockets v0.0.11-beta/go.mod h1:9WuWIyja/Q8PF3WmblAjFmfzkKabEBLto2Gx1XYnerc=
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5 h1:39/z+O7p2ND6GgvOHZAr7o1gjm6UGls8FCcki8hbXRE=
github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5/go.mod h1:u4PMKd3yqHkt9Jn0VgQRZ33PG9ynL8/j53csVO1huyk=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tonicpow/go-minercraft v0.4.0 h1:o7ndFm0NDIWZ6ml5qXGzGIh7xhGDWQhQixjPCJec2PY=
github.com/tonicpow/go-minercraft v0.4.0/go.mod h1:+mJZAtlRy89vbL/gLAH4kft46lxueHUGMhsBJF2E9Fg=
Expand Down Expand Up @@ -997,7 +994,6 @@ golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210816183151-1e6c022a8912/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
4 changes: 3 additions & 1 deletion service/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (h *healthSvc) Health(ctx context.Context) error {
}
switch u.Scheme {
case "ws", "wss":
if err := h.c.JoinChannel(h.cfg.ServerHost, "health", nil); err != nil {
if err := h.c.JoinChannel(h.cfg.ServerHost, "health", nil, map[string]string{
"internal": "true",
}); err != nil {
return err
}
if err := h.c.Publish(sockets.Request{
Expand Down
20 changes: 16 additions & 4 deletions vendor/github.com/theflyingcodr/sockets/client/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion vendor/github.com/theflyingcodr/sockets/client/connections.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/theflyingcodr/sockets/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions vendor/github.com/theflyingcodr/sockets/server/server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions vendor/github.com/theflyingcodr/sockets/socket.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ github.com/theflyingcodr/govalidator
## explicit; go 1.17
github.com/theflyingcodr/lathos
github.com/theflyingcodr/lathos/errs
# github.com/theflyingcodr/sockets v0.0.11-beta
# github.com/theflyingcodr/sockets v0.0.11-beta.0.20220222160101-76100ef886b5
## explicit; go 1.17
github.com/theflyingcodr/sockets
github.com/theflyingcodr/sockets/client
Expand Down

0 comments on commit 53bff9f

Please sign in to comment.