eye-gu commented on code in PR #7582:
URL: https://github.com/apache/shenyu/pull/7582#discussion_r4271052187


##########
shenyu-plugin/shenyu-plugin-wasm-api/src/main/java/org/apache/shenyu/plugin/wasm/api/loader/WasmLoader.java:
##########
@@ -282,17 +369,62 @@ 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();
+        try {
+            return executeBound(lease, lease.export(funcName), args);
+        } finally {
+            release(lease);
+        }
+    }
+
+    // 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();
+        }
+    }
+
+    private Instance acquire() {
+        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);
+        }
+        try {
+            return freeInstances.take();
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new IllegalStateException(
+                    "Interrupted while waiting for a pooled WASM instance of " 
+ wasmName, e);
+        }
+    }

Review Comment:
   In production WasmLoader is the plugin singleton (AbstractWasmPlugin extends 
WasmLoader) with the JVM's lifetime; nothing in gateway code calls close() — it 
only runs in tests, after futures complete, so the "closed while blocked in 
acquire()" window doesn't exist. Every lease is returned in a try/finally, so 
take() blocks only while all instances are genuinely busy — that unbounded wait 
is the intended backpressure. A poll(timeout) would instead convert queueing 
under load into request failures, a worse failure mode for a gateway.
   
   



-- 
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