forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInternalExecutorProvider.java
More file actions
65 lines (58 loc) · 2.54 KB
/
Copy pathInternalExecutorProvider.java
File metadata and controls
65 lines (58 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
/**
* Resolves the {@link Executor} used by {@link CopilotClient} for internal
* asynchronous work.
*
* <p>This is the <strong>JDK 25+ multi-release variant</strong>. It is
* compiled with {@code --release 25} into
* {@code META-INF/versions/25/com/github/copilot/InternalExecutorProvider.class}
* inside the packaged JAR and is automatically loaded in preference to the
* baseline class when the JVM runtime feature version is 25 or greater.
* When no user-provided executor is supplied, it creates an SDK-owned
* {@link Executors#newVirtualThreadPerTaskExecutor() virtual-thread executor}
* that is shut down by {@link CopilotClient#close()}.
*
* <p><strong>Multi-release JAR contract.</strong> This class is the
* JDK 25 sibling of the baseline implementation at
* {@code src/main/java/com/github/copilot/InternalExecutorProvider.java}.
* The package-private surface of both classes
* ({@link #InternalExecutorProvider(Executor) constructor},
* {@link #get()}, {@link #canBeShutdown()}) <strong>must be kept in
* lock-step</strong>; only the default-executor strategy and ownership
* semantics differ.
*
* @implNote
* Maintainers: when editing this file, also edit
* {@code src/main/java/com/github/copilot/InternalExecutorProvider.java}.
* The packaged JAR is verified at build time (see the
* {@code java25-multi-release} profile in {@code pom.xml}) to ensure this
* overlay class is present.
*/
final class InternalExecutorProvider {
private final Executor executor;
private final boolean owned;
InternalExecutorProvider(Executor userProvided) {
if (userProvided != null) {
this.executor = userProvided;
this.owned = false;
} else {
this.executor = Executors.newVirtualThreadPerTaskExecutor();
this.owned = true;
}
}
Executor get() {
return executor;
}
boolean canBeShutdown() {
// We can only shut down the executor if we created it (i.e., if it's owned)
// such as when using Executors.newVirtualThreadPerTaskExecutor(),
// which creates an executor that we are responsible for shutting down.
return owned;
}
}