forked from bterlson/openai-in-typespec
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Realtime: complete port of fixes and tests for sendinputaudio behavior (
#333) * realtime: complete port of fixes and tests for sendinputaudio behavior * run-checks.ps1
- Loading branch information
Showing
4 changed files
with
190 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Diagnostics.Contracts; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAI; | ||
|
||
internal static class SemaphoreSlimExtensions | ||
{ | ||
public static async Task<IDisposable> AutoReleaseWaitAsync( | ||
this SemaphoreSlim semaphore, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
Contract.Requires(semaphore != null); | ||
var wrapper = new ReleaseableSemaphoreSlimWrapper(semaphore); | ||
await semaphore.WaitAsync(cancellationToken); | ||
return wrapper; | ||
} | ||
|
||
public static IDisposable AutoReleaseWait( | ||
this SemaphoreSlim semaphore, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
Contract.Requires(semaphore != null); | ||
var wrapper = new ReleaseableSemaphoreSlimWrapper(semaphore); | ||
semaphore.Wait(cancellationToken); | ||
return wrapper; | ||
} | ||
|
||
private class ReleaseableSemaphoreSlimWrapper | ||
: IDisposable | ||
{ | ||
private readonly SemaphoreSlim semaphore; | ||
private bool alreadyDisposed = false; | ||
|
||
public ReleaseableSemaphoreSlimWrapper(SemaphoreSlim semaphore) | ||
=> this.semaphore = semaphore; | ||
|
||
public void Dispose() | ||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected void Dispose(bool disposeActuallyCalled) | ||
{ | ||
if (!this.alreadyDisposed) | ||
{ | ||
if (disposeActuallyCalled) | ||
{ | ||
this.semaphore?.Release(); | ||
} | ||
|
||
this.alreadyDisposed = true; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters