Skip to content

Commit

Permalink
Add heartbeat for gsrpc connection (#1277)
Browse files Browse the repository at this point in the history
* Add heartbeat option for relayer connection

* whitespace
  • Loading branch information
vgeddes committed Aug 29, 2024
1 parent 3291185 commit 4413bee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
28 changes: 28 additions & 0 deletions relayer/chain/parachain/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package parachain

import (
"context"
"time"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -72,6 +73,33 @@ func (co *Connection) Connect(_ context.Context) error {
return nil
}

func (co *Connection) ConnectWithHeartBeat(ctx context.Context, heartBeat time.Duration) error {
err := co.Connect(ctx)
if err != nil {
return err
}

ticker := time.NewTicker(heartBeat)

go func() {
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
_, err := co.API().RPC.System.Version()
if err != nil {
log.WithField("endpoint", co.endpoint).Error("Connection heartbeat failed")
return
}
}
}
}()

return nil
}

func (co *Connection) Close() {
// TODO: Fix design issue in GSRPC preventing on-demand closing of connections
}
Expand Down
3 changes: 2 additions & 1 deletion relayer/relays/beacon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package beacon

import (
"context"
"time"

"github.com/snowfork/snowbridge/relayer/chain/parachain"
"github.com/snowfork/snowbridge/relayer/crypto/sr25519"
Expand Down Expand Up @@ -36,7 +37,7 @@ func (r *Relay) Start(ctx context.Context, eg *errgroup.Group) error {

paraconn := parachain.NewConnection(r.config.Sink.Parachain.Endpoint, r.keypair.AsKeyringPair())

err := paraconn.Connect(ctx)
err := paraconn.ConnectWithHeartBeat(ctx, 60 * time.Second)
if err != nil {
return err
}
Expand Down

0 comments on commit 4413bee

Please sign in to comment.