-
Notifications
You must be signed in to change notification settings - Fork 843
fix(a2a): handle streaming backpressure #1734
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
|
|
||
| import io.a2a.client.TaskUpdateEvent; | ||
| import io.a2a.spec.TaskArtifactUpdateEvent; | ||
| import io.a2a.spec.TaskState; | ||
| import io.a2a.spec.TaskStatus; | ||
| import io.a2a.spec.TaskStatusUpdateEvent; | ||
| import io.a2a.spec.UpdateEvent; | ||
|
|
@@ -89,6 +90,22 @@ private static class TaskStatusUpdateEventHandler | |
| public void handle(TaskStatusUpdateEvent event, ClientEventContext context) { | ||
| String currentRequestId = context.getCurrentRequestId(); | ||
| if (event.isFinal()) { | ||
| TaskState state = event.getStatus().state(); | ||
| if (!TaskState.COMPLETED.equals(state)) { | ||
| String errorMsg = | ||
| "A2A task ended with state: " | ||
| + state | ||
| + (event.getStatus().message() != null | ||
| ? ", message: " + event.getStatus().message() | ||
| : ""); | ||
| LoggerUtil.warn( | ||
| log, | ||
| "[{}] A2aAgent task ended with non-completed state: {}.", | ||
| currentRequestId, | ||
| state); | ||
| context.getSink().success(Msg.builder().textContent(errorMsg).build()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] If the |
||
| return; | ||
| } | ||
| Msg msg = | ||
| MessageConvertUtil.convertFromArtifact( | ||
| context.getTask().getArtifacts(), context.getAgent().getName()); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ | |
| import org.reactivestreams.FlowAdapters; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import reactor.core.publisher.BufferOverflowStrategy; | ||
| import reactor.core.publisher.Flux; | ||
|
|
||
| /** | ||
|
|
@@ -77,6 +78,16 @@ public class JsonRpcTransportWrapper implements TransportWrapper<String, Object> | |
|
|
||
| private static final Logger log = LoggerFactory.getLogger(JsonRpcTransportWrapper.class); | ||
|
|
||
| private static final String STREAMING_BACKPRESSURE_BUFFER_SIZE_PROPERTY = | ||
| "agentscope.a2a.streaming.backpressure-buffer-size"; | ||
|
|
||
| private static final int DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE = 8192; | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] Consider adding a defensive parse: private static final int STREAMING_BACKPRESSURE_BUFFER_SIZE = parseBufferSize();
private static int parseBufferSize() {
String raw = System.getProperty(STREAMING_BACKPRESSURE_BUFFER_SIZE_PROPERTY);
if (raw == null) return DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE;
try {
return Integer.parseInt(raw);
} catch (NumberFormatException e) {
LoggerFactory.getLogger(JsonRpcTransportWrapper.class)
.warn("Invalid value for {}: '{}', using default {}",
STREAMING_BACKPRESSURE_BUFFER_SIZE_PROPERTY, raw,
DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE);
return DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE;
}
}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] Consider adding a defensive parse: private static final int STREAMING_BACKPRESSURE_BUFFER_SIZE = parseBufferSize();
private static int parseBufferSize() {
String raw = System.getProperty(STREAMING_BACKPRESSURE_BUFFER_SIZE_PROPERTY);
if (raw == null) return DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE;
try {
return Integer.parseInt(raw);
} catch (NumberFormatException e) {
LoggerFactory.getLogger(JsonRpcTransportWrapper.class)
.warn("Invalid value for {}: '{}', using default {}",
STREAMING_BACKPRESSURE_BUFFER_SIZE_PROPERTY, raw,
DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE);
return DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE;
}
} |
||
| private static final int STREAMING_BACKPRESSURE_BUFFER_SIZE = | ||
| Integer.getInteger( | ||
| STREAMING_BACKPRESSURE_BUFFER_SIZE_PROPERTY, | ||
| DEFAULT_STREAMING_BACKPRESSURE_BUFFER_SIZE); | ||
|
|
||
| private final JSONRPCHandler jsonRpcHandler; | ||
|
|
||
| public JsonRpcTransportWrapper(JSONRPCHandler jsonrpcHandler) { | ||
|
|
@@ -160,10 +171,38 @@ private Flux<? extends JSONRPCResponse<?>> handleStreamRequest( | |
| return Flux.just(generateErrorResponse(request, new UnsupportedOperationError())); | ||
| } | ||
|
|
||
| return Flux.from(FlowAdapters.toPublisher(publisher)) | ||
| String method = (String) context.getState().get(JSONRPCContextKeys.METHOD_NAME_KEY); | ||
| Object requestId = request.getId(); | ||
| return applyStreamingBackpressureBuffer( | ||
| Flux.from(FlowAdapters.toPublisher(publisher)), method, requestId) | ||
| .delaySubscription(Duration.ofMillis(10)); | ||
| } | ||
|
|
||
| private Flux<? extends JSONRPCResponse<?>> applyStreamingBackpressureBuffer( | ||
| Flux<? extends JSONRPCResponse<?>> stream, String method, Object requestId) { | ||
| if (STREAMING_BACKPRESSURE_BUFFER_SIZE <= 0) { | ||
| return stream.onBackpressureBuffer(); | ||
| } | ||
| return stream.onBackpressureBuffer( | ||
| STREAMING_BACKPRESSURE_BUFFER_SIZE, | ||
| response -> | ||
| log.error( | ||
| "JsonRpcTransportWrapper.stream backpressure buffer overflow:" | ||
| + " method={}, requestId={}, bufferSize={}, dropped={}", | ||
| method, | ||
| requestId, | ||
| STREAMING_BACKPRESSURE_BUFFER_SIZE, | ||
| summarizeResponse(response)), | ||
| BufferOverflowStrategy.ERROR); | ||
| } | ||
|
|
||
| private String summarizeResponse(JSONRPCResponse<?> response) { | ||
| if (response == null) { | ||
| return "responseType=null"; | ||
| } | ||
| return "responseType=" + response.getClass().getName(); | ||
| } | ||
|
|
||
| private JSONRPCResponse<?> handleNonStreamRequest(String body, ServerCallContext context) | ||
| throws JsonProcessingException { | ||
| NonStreamingJSONRPCRequest<?> request = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[minor]
context.getSink().success(...)is used to propagate a non-COMPLETED terminal state (e.g. FAILED, CANCELED) as if it were a normal completion. This is semantically misleading — downstream consumers of theSinkcannot distinguish a genuinely successful task from a failed one without inspecting theMsg.textContent.If the
SinkAPI supports it, prefersink.error(new RuntimeException(errorMsg))or a dedicated failure path. Ifsuccess()is the only option (e.g. the sink type doesn't support error emission), add a code comment explaining why a failure is propagated viasuccess(), so future maintainers are not confused.