-
Notifications
You must be signed in to change notification settings - Fork 434
JavaScript RPC changes #5636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
JavaScript RPC changes #5636
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4b5b771
Remove `extractBundledInstallation` from API
knutwannheden a5d26a5
Remove `batchSize()` from Builder API
knutwannheden 783b73a
JS server will now append to given log file
knutwannheden b6646f8
Revert some accidental changes again
knutwannheden 69e68ec
Remove lazy initialization for `RewriteRpc`
knutwannheden c603362
Make marketplace a required builder parameter
knutwannheden 3ad0f45
Add `ClearObjectCaches`
knutwannheden 108bcb8
Rename to `ClearObjectMaps`
knutwannheden 835b037
Remove `RewriteRpc.Context`
knutwannheden 25a05b0
no clearobjectmaps (#5648)
knutwannheden 81235c4
Add missing codecs and avoid conflicts on registration
knutwannheden 2b049de
Add missing last line terminators
knutwannheden b82a85f
Merge remote-tracking branch 'origin/main' into js-rpc
knutwannheden c32267c
Merge remote-tracking branch 'origin/main' into js-rpc
knutwannheden 75cca15
Properly register `GetRef` handler in JS `RewriteRpc`
knutwannheden 9bba9ce
Fixes
knutwannheden ad89f6d
Fixes
knutwannheden dc36d78
More fixes
knutwannheden 3cf77bc
Merge remote-tracking branch 'origin/main' into js-rpc
knutwannheden cc958c4
Slightly refactor JavaScriptRewriteRpc
knutwannheden b7a7fd1
Polish
knutwannheden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
196 changes: 196 additions & 0 deletions
196
rewrite-core/src/main/java/org/openrewrite/internal/ManagedThreadLocal.java
This file contains hidden or 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,196 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.openrewrite.internal; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.experimental.FieldDefaults; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* ThreadLocal wrapper for managed resources that need lifecycle management. | ||
* <p> | ||
* Designed for AutoCloseable resources like database connections, file handles, | ||
* network connections, etc. that require proper cleanup. | ||
* <p> | ||
* Typical usage: | ||
* <pre> | ||
* // At entry point (e.g., request handler) | ||
* try (var managed = DatabaseConnection.current().requireOrCreate(() -> new DatabaseConnection(...))) { | ||
* // Application code can use require() throughout call chain | ||
* processRequest(); | ||
* } // Automatic cleanup of both ThreadLocal and resource | ||
* | ||
* // In application code | ||
* public void processRequest() { | ||
* DatabaseConnection conn = DatabaseConnection.current().require(); | ||
* conn.executeQuery("SELECT ..."); | ||
* } | ||
* </pre> | ||
*/ | ||
public class ManagedThreadLocal<T extends AutoCloseable> { | ||
private final ThreadLocal<@Nullable T> threadLocal = new ThreadLocal<>(); | ||
|
||
/** | ||
* Gets the current resource, throwing if not present. | ||
* <p> | ||
* This is the primary method for accessing the resource throughout your call chain. | ||
* It fails fast with a clear error if no resource has been established. | ||
* | ||
* @return the current resource | ||
* @throws IllegalStateException if no resource is present in the current thread | ||
*/ | ||
public T require() { | ||
T value = threadLocal.get(); | ||
if (value == null) { | ||
throw new IllegalStateException("No managed resource found in current thread context"); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* Gets or creates a managed resource with automatic cleanup. | ||
* <p> | ||
* If a resource already exists, returns it in a scope with no-op cleanup. | ||
* If no resource exists, creates one using the factory and sets up automatic cleanup | ||
* of both the ThreadLocal state and the resource itself when the returned | ||
* Scope is closed. | ||
* <p> | ||
* Use this at entry points where you want to establish the resource context. | ||
* | ||
* @param factory supplier to create the resource if none exists | ||
* @return a Scope that provides access to the resource and handles cleanup | ||
*/ | ||
public Scope<T> requireOrCreate(Supplier<T> factory) { | ||
T existing = threadLocal.get(); | ||
if (existing != null) { | ||
// Return existing value with no-op cleanup | ||
return new Scope<>(existing, () -> { | ||
}); | ||
} | ||
|
||
T newValue = factory.get(); | ||
threadLocal.set(newValue); | ||
|
||
return new Scope<>(newValue, () -> { | ||
try { | ||
threadLocal.remove(); | ||
} finally { | ||
newValue.close(); // Close the resource | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Temporarily replace the resource, returning an AutoCloseable that restores the original. | ||
* <p> | ||
* Use this when you need to temporarily use a different resource within a scope. | ||
* The original resource is automatically restored when the returned AutoCloseable is closed. | ||
* <p> | ||
* Note: This does NOT close the temporary resource - you're responsible for its lifecycle. | ||
* | ||
* @param value the temporary resource to use | ||
* @return a Scope that restores the previous resource when closed | ||
*/ | ||
public Scope<T> using(T value) { | ||
T previousValue = threadLocal.get(); | ||
threadLocal.set(value); | ||
|
||
return new Scope<>(value, () -> { | ||
if (previousValue != null) { | ||
threadLocal.set(previousValue); | ||
} else { | ||
threadLocal.remove(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Provides access to the underlying ThreadLocal for advanced use cases. | ||
* <p> | ||
* Use with caution - direct ThreadLocal manipulation bypasses the managed | ||
* resource lifecycle. Prefer the managed methods (require, requireOrCreate, using) | ||
* for typical usage. | ||
* | ||
* @return the underlying ThreadLocal instance | ||
*/ | ||
public ThreadLocal<@Nullable T> asThreadLocal() { | ||
return threadLocal; | ||
} | ||
|
||
/** | ||
* Checks if a resource is present in the current thread. | ||
* | ||
* @return true if a resource is present, false otherwise | ||
*/ | ||
public boolean isPresent() { | ||
return threadLocal.get() != null; | ||
} | ||
|
||
/** | ||
* A scoped resource that provides both the resource value and automatic cleanup. | ||
* <p> | ||
* This is returned by requireOrCreate() and using(). It handles both ThreadLocal | ||
* restoration and resource cleanup when the scope is closed. | ||
*/ | ||
@RequiredArgsConstructor | ||
@FieldDefaults(level = AccessLevel.PRIVATE) | ||
public static class Scope<T extends AutoCloseable> implements AutoCloseable { | ||
final T resource; | ||
final AutoCloseable cleanup; | ||
|
||
/** | ||
* Applies a function to the scoped resource and returns the result. | ||
* <p> | ||
* This provides functional access to the resource without exposing it directly. | ||
* The function is called with the current resource as its argument. | ||
* <p> | ||
* Example usage: | ||
* <pre> | ||
* try (var scope = DatabaseConnection.current().requireOrCreate(...)) { | ||
* String result = scope.map(conn -> { | ||
* conn.executeQuery("SELECT ..."); | ||
* return conn.getLastResult(); | ||
* }); | ||
* } | ||
* </pre> | ||
* | ||
* @param <R> the type of the result | ||
* @param mapper function to apply to the scoped resource | ||
* @return the result of applying the function to the resource | ||
*/ | ||
public <R> R map(Function<T, R> mapper) { | ||
return mapper.apply(resource); | ||
} | ||
|
||
/** | ||
* Performs cleanup of both ThreadLocal state and the resource. | ||
* <p> | ||
* This is called automatically when used with try-with-resources. | ||
*/ | ||
@Override | ||
public void close() { | ||
try { | ||
cleanup.close(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} | ||
knutwannheden marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.