File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
openai-java-core/src/main/kotlin/com/openai/core Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ package com.openai.core
5
5
import com.openai.errors.OpenAIInvalidDataException
6
6
import java.util.Collections
7
7
import java.util.SortedMap
8
+ import java.util.concurrent.CompletableFuture
9
+ import java.util.concurrent.locks.Lock
8
10
9
11
@JvmSynthetic
10
12
internal fun <T : Any > T?.getOrThrow (name : String ): T =
@@ -101,3 +103,24 @@ internal fun isAzureEndpoint(baseUrl: String): Boolean {
101
103
}
102
104
103
105
internal interface Enum
106
+
107
+ /* *
108
+ * Executes the given [action] while holding the lock, returning a [CompletableFuture] with the
109
+ * result.
110
+ *
111
+ * @param action The asynchronous action to execute while holding the lock
112
+ * @return A [CompletableFuture] that completes with the result of the action
113
+ */
114
+ @JvmSynthetic
115
+ internal fun <T > Lock.withLockAsync (action : () -> CompletableFuture <T >): CompletableFuture <T > {
116
+ lock()
117
+ val future =
118
+ try {
119
+ action()
120
+ } catch (e: Throwable ) {
121
+ unlock()
122
+ throw e
123
+ }
124
+ future.whenComplete { _, _ -> unlock() }
125
+ return future
126
+ }
You can’t perform that action at this time.
0 commit comments