Copilot commented on code in PR #7582:
URL: https://github.com/apache/shenyu/pull/7582#discussion_r4279410799


##########
shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/loader/WasmLoader.java:
##########
@@ -282,17 +392,114 @@ private Optional<ExportFunction> findWasmExtern(final 
String funcName) {
                 return Optional.of(instance.export(funcName));
             }
         }
-        LOG.debug("WASM export function '{}' not found in {}", funcName, 
wasmName);
         return Optional.empty();
     }
-    
+
+    private long[] executeOnPool(final String funcName, final long[] args) {
+        final Instance lease = acquire(funcName);
+        Throwable failure = null;
+        try {
+            return executeBound(lease, lease.export(funcName), args);
+        } catch (final Throwable t) {
+            // remembered so a release failure below cannot mask the original 
failure
+            failure = t;
+            throw t;
+        } finally {
+            try {
+                release(lease);
+            } catch (final RuntimeException e) {
+                if (Objects.nonNull(failure)) {
+                    failure.addSuppressed(e);
+                } else {
+                    throw e;
+                }
+            }
+        }
+    }
+
+    // binds the thread to the instance so guest host callbacks resolve 
getMemory() to it;
+    // pops on exit (nested calls each push their own entry)
+    private long[] executeBound(final Instance instance, final ExportFunction 
export, final long[] args) {
+        final ArrayDeque<Instance> stack = bindings.get();
+        stack.push(instance);
+        try {
+            return export.apply(args);
+        } finally {
+            stack.pop();
+        }
+    }
+
     /**
-     * Get the WASM module's linear memory.
+     * Max time to wait for a free pooled instance before failing the call.
+     * Overridable so tests can exercise the timeout path quickly.
      *
-     * @return the WASM linear memory
+     * @return the lease timeout in milliseconds
+     */
+    protected long leaseTimeoutMillis() {
+        return DEFAULT_LEASE_TIMEOUT_MILLIS;
+    }
+
+    private Instance acquire(final String funcName) {
+        if (!ready) {
+            // reached only from a host callback during construction (e.g. a 
nested
+            // export call from _initialize): no initialized instance is free 
yet,
+            // so waiting could never succeed
+            throw new IllegalStateException(
+                    "Nested export call during construction of " + wasmName
+                            + "; the instance pool is not ready yet");
+        }
+        final ArrayDeque<Instance> active = bindings.get();
+        if (active.size() >= poolSize) {
+            // nesting deeper than the pool: this thread already leases every 
instance,
+            // so waiting could never succeed
+            throw new IllegalStateException(
+                    "Nested export calls exceed the WASM instance pool size 
for " + wasmName);
+        }
+        final long timeoutMillis = leaseTimeoutMillis();
+        final Instance lease;
+        try {
+            lease = freeInstances.poll(timeoutMillis, TimeUnit.MILLISECONDS);

Review Comment:
   When the pool is exhausted, this synchronous poll can park a Netty 
event-loop thread for up to 30 seconds, delaying unrelated connections on that 
loop. Both WASM plugin templates call exports synchronously, including 
before/after hooks; ShenyuWebHandler.java:305-316 invokes these on the 
subscriber thread, and ShenyuConfig.Scheduler disables offloading by default. A 
bounded wait does not provide reactive backpressure. Keep the exclusive leases, 
but offload acquisition and guest execution—including request hooks—to an 
appropriate worker scheduler, or integrate asynchronous acquisition at the 
plugin boundary.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to