1
1
import type { Peer , PeerId } from "@libp2p/interface" ;
2
- import { Failure , PeerIdStr } from "@waku/interfaces" ;
2
+ import { CoreProtocolResult , PeerIdStr } from "@waku/interfaces" ;
3
3
import { Logger } from "@waku/utils" ;
4
4
5
5
const log = new Logger ( "sdk:sender:reliability_monitor" ) ;
@@ -13,27 +13,45 @@ export class SenderReliabilityMonitor {
13
13
14
14
public constructor ( private renewPeer : ( peerId : PeerId ) => Promise < Peer > ) { }
15
15
16
- public handleSendFailure = async ( failure : Failure ) : Promise < void > => {
17
- if ( failure . peerId ) {
18
- const peerIdStr = failure . peerId . toString ( ) ;
19
- const currentAttempts = this . attempts . get ( peerIdStr ) || 0 ;
20
- this . attempts . set ( peerIdStr , currentAttempts + 1 ) ;
21
-
22
- if ( currentAttempts + 1 >= this . maxAttemptsBeforeRenewal ) {
23
- try {
24
- const newPeer = await this . renewPeer ( failure . peerId ) ;
25
- log . info (
26
- `Renewed peer ${ failure . peerId . toString ( ) } to ${ newPeer . id . toString ( ) } `
27
- ) ;
16
+ public async attemptRetries (
17
+ peerId : PeerId ,
18
+ protocolSend : ( ) => Promise < CoreProtocolResult >
19
+ ) : Promise < void > {
20
+ const peerIdStr = peerId . toString ( ) ;
21
+ const currentAttempts = this . attempts . get ( peerIdStr ) || 0 ;
22
+ this . attempts . set ( peerIdStr , currentAttempts + 1 ) ;
28
23
24
+ if ( currentAttempts + 1 < this . maxAttemptsBeforeRenewal ) {
25
+ try {
26
+ const result = await protocolSend ( ) ;
27
+ if ( result . success ) {
28
+ log . info ( `Successfully sent message after retry to ${ peerIdStr } ` ) ;
29
29
this . attempts . delete ( peerIdStr ) ;
30
- this . attempts . set ( newPeer . id . toString ( ) , 0 ) ;
31
- } catch ( error ) {
30
+ } else {
32
31
log . error (
33
- `Failed to renew peer ${ failure . peerId . toString ( ) } : ${ error } `
32
+ `Failed to send message after retry to ${ peerIdStr } : ${ result . failure } `
34
33
) ;
34
+ await this . attemptRetries ( peerId , protocolSend ) ;
35
35
}
36
+ } catch ( error ) {
37
+ log . error (
38
+ `Failed to send message after retry to ${ peerIdStr } : ${ error } `
39
+ ) ;
40
+ await this . attemptRetries ( peerId , protocolSend ) ;
41
+ }
42
+ } else {
43
+ try {
44
+ const newPeer = await this . renewPeer ( peerId ) ;
45
+ log . info (
46
+ `Renewed peer ${ peerId . toString ( ) } to ${ newPeer . id . toString ( ) } `
47
+ ) ;
48
+
49
+ this . attempts . delete ( peerIdStr ) ;
50
+ this . attempts . set ( newPeer . id . toString ( ) , 0 ) ;
51
+ await protocolSend ( ) ;
52
+ } catch ( error ) {
53
+ log . error ( `Failed to renew peer ${ peerId . toString ( ) } : ${ error } ` ) ;
36
54
}
37
55
}
38
- } ;
56
+ }
39
57
}
0 commit comments