Skip to content

Commit

Permalink
feat: add connection timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
gigajuwels committed Oct 3, 2023
1 parent 9be25f2 commit c572ced
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
14 changes: 13 additions & 1 deletion WalletConnectSharp.Core/Controllers/Relayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public string Context
/// </summary>
public IJsonRpcProvider Provider { get; private set; }

/// <summary>
/// How long the <see cref="IRelayer"/> should wait before throwing a <see cref="TimeoutException"/> during
/// the connection phase. If this field is null, then the timeout will be infinite.
/// </summary>
public TimeSpan? ConnectionTimeout { get; set; }

/// <summary>
/// Whether this Relayer is connected
/// </summary>
Expand Down Expand Up @@ -133,6 +139,8 @@ public Relayer(RelayerOptions opts)
}

projectId = opts.ProjectId;

ConnectionTimeout = opts.ConnectionTimeout;
}

/// <summary>
Expand Down Expand Up @@ -404,7 +412,11 @@ async void Task2()
this.Events.ListenForOnce<object>(RelayerEvents.TransportClosed, RejectTransportOpen);
try
{
await this.Provider.Connect().WithTimeout(TimeSpan.FromSeconds(5), "socket stalled");
var connectionTask = this.Provider.Connect();
if (ConnectionTimeout != null)
connectionTask = connectionTask.WithTimeout((TimeSpan)ConnectionTimeout, "socket stalled");

await connectionTask;
task2.TrySetResult(true);
}
finally
Expand Down
6 changes: 6 additions & 0 deletions WalletConnectSharp.Core/Interfaces/IRelayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public interface IRelayer : IEvents, IModule
/// </summary>
public IJsonRpcProvider Provider { get; }

/// <summary>
/// How long the <see cref="IRelayer"/> should wait before throwing a <see cref="TimeoutException"/> during
/// the connection phase. If this field is null, then the timeout will be infinite.
/// </summary>
public TimeSpan? ConnectionTimeout { get; set; }

/// <summary>
/// Whether this Relayer is connected to the WalletConnect relay server
/// </summary>
Expand Down
8 changes: 7 additions & 1 deletion WalletConnectSharp.Core/Models/CoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ public class CoreOptions
/// with either the KeyChain option or a default keychain
/// </summary>
public ICrypto CryptoModule;


/// <summary>
/// How long the <see cref="IRelayer"/> should wait before throwing a <see cref="TimeoutException"/> during
/// the connection phase. If this field is null, then the timeout will be infinite.
/// </summary>
public TimeSpan? ConnectionTimeout = TimeSpan.FromSeconds(30);

}
}
6 changes: 6 additions & 0 deletions WalletConnectSharp.Core/Models/Relay/RelayerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@ public class RelayerOptions
/// </summary>
[JsonProperty("projectId")]
public string ProjectId { get; set; }

/// <summary>
/// How long the <see cref="IRelayer"/> should wait before throwing a <see cref="TimeoutException"/> during
/// the connection phase. If this field is null, then the timeout will be infinite.
/// </summary>
public TimeSpan? ConnectionTimeout = TimeSpan.FromSeconds(30);
}
}
3 changes: 2 additions & 1 deletion WalletConnectSharp.Core/WalletConnectCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ public WalletConnectCore(CoreOptions options = null)
{
Core = this,
ProjectId = ProjectId,
RelayUrl = options.RelayUrl
RelayUrl = options.RelayUrl,
ConnectionTimeout = options.ConnectionTimeout,
});

MessageHandler = new TypedMessageHandler(this);
Expand Down

0 comments on commit c572ced

Please sign in to comment.