Skip to content

Commit

Permalink
backend: add ManualReconnect method and call on iOS
Browse files Browse the repository at this point in the history
When the iOS app goes into the background, its TCP
connetions (Electrum server connetions) are killed after a
while (~30s).

When the user goes back into the app, we want to immediatelly trigger
a reconnect, not waiting for the regular 30s reconnect timer.
  • Loading branch information
benma committed Sep 30, 2024
1 parent 2cbe6ab commit f2ecdd9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
37 changes: 37 additions & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,43 @@ func (backend *Backend) Coin(code coinpkg.Code) (coinpkg.Coin, error) {
return coin, nil
}

// ManualReconnect triggers reconnecting to Electrum servers if their connection is down.
// Only coin connections that were previously established are reconnected.
// Calling this is a no-op for coins that are already connected.
func (backend *Backend) ManualReconnect() {
var electrumCoinCodes []coinpkg.Code
if backend.arguments.Testing() {
electrumCoinCodes = []coinpkg.Code{
coinpkg.CodeTBTC,
coinpkg.CodeTLTC,
}
} else {
electrumCoinCodes = []coinpkg.Code{
coinpkg.CodeBTC,
coinpkg.CodeLTC,
}
}
backend.log.Info("Manually reconnecting")
for _, code := range electrumCoinCodes {
c, err := backend.Coin(code)
if err != nil {
backend.log.WithError(err).Errorf("could not find coin: %s", code)
continue
}
btcCoin, ok := c.(*btc.Coin)
if !ok {
backend.log.Errorf("Expected %s to be a btc coin", code)
continue
}
blockchain := btcCoin.Blockchain()
if blockchain == nil {
// Not initialized yet
continue
}
blockchain.ManualReconnect()
}
}

// Testing returns whether this backend is for testing only.
func (backend *Backend) Testing() bool {
return backend.arguments.Testing()
Expand Down
12 changes: 12 additions & 0 deletions backend/bridgecommon/bridgecommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ func UsingMobileDataChanged() {
})
}

// ManualReconnect exposes the ManualReconnect backend method.
func ManualReconnect() {
mu.Lock()
defer mu.Unlock()

if globalBackend == nil {
return
}
globalBackend.ManualReconnect()

}

// BackendEnvironment implements backend.Environment.
type BackendEnvironment struct {
NotifyUserFunc func(string)
Expand Down
1 change: 1 addition & 0 deletions backend/coins/btc/blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,5 @@ type Interface interface {
Close()
ConnectionError() error
RegisterOnConnectionErrorChangedEvent(func(error))
ManualReconnect()
}
4 changes: 4 additions & 0 deletions backend/coins/btc/electrum/failoverclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ func (f *failoverClient) TransactionGet(txHash chainhash.Hash) (*wire.MsgTx, err
})
}

func (f *failoverClient) ManualReconnect() {
f.failover.ManualReconnect()
}

func (f *failoverClient) Close() {
f.failover.Close()
}
7 changes: 6 additions & 1 deletion backend/mobileserver/mobileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,13 @@ func CancelAuth() {
bridgecommon.CancelAuth()
}

// AuthResult triggers an auth feeedback notification (auth-ok/auth-err) towards the frontend,
// AuthResult triggers an auth feedback notification (auth-ok/auth-err) towards the frontend,
// depending on the input value.
func AuthResult(ok bool) {
bridgecommon.AuthResult(ok)
}

// ManualReconnect wraps bridgecommon.ManualReconnet
func AuthResult() {
bridgecommon.ManualReconnect()
}

0 comments on commit f2ecdd9

Please sign in to comment.