Skip to content

Commit

Permalink
await transaction efficiently with semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
Hecate2 committed Dec 20, 2022
1 parent 2184a22 commit ecf9264
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Fairy.Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ internal void InitializeTimer()
timer = new(OnTimer, null, settings.SessionExpirationTime.Milliseconds, 60000);
}

internal void OnTimer(object state)
internal void OnTimer(object? state)
{
List<(string Id, FairySession Session)> toBeDestroyed = new();
foreach (var (id, session) in sessionStringToFairySession)
Expand Down
16 changes: 5 additions & 11 deletions Fairy.Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,13 @@ protected virtual JToken AwaitConfirmedTransaction(JArray _params)
{
return json;
}
uint count = 1;
bool expectBlockIn15Secs = false;
CommittedHandler getConfirmedTransactionAfterCommitted = delegate(NeoSystem @system, Block @block){ count += 1; expectBlockIn15Secs = true; json = GetConfirmedTransaction(hash, verbose); };
SemaphoreSlim signal = new SemaphoreSlim(0, 1);
uint count = 0;
CommittedHandler getConfirmedTransactionAfterCommitted = delegate(NeoSystem @system, Block @block){ json = GetConfirmedTransaction(hash, verbose); count += 1; signal.Release(); };
Blockchain.Committed += getConfirmedTransactionAfterCommitted;
while (count <= waitBlockCount)
while (count < waitBlockCount)
{
if (expectBlockIn15Secs == false)
Thread.Sleep(500);
else
{
expectBlockIn15Secs = false;
Thread.Sleep(14500);
}
signal.Wait();
if (json != null)
{
Blockchain.Committed -= getConfirmedTransactionAfterCommitted;
Expand Down

0 comments on commit ecf9264

Please sign in to comment.