|
5 | 5 | package com.agentclientprotocol.sdk.agent.transport; |
6 | 6 |
|
7 | 7 | import java.io.IOException; |
8 | | -import java.io.PrintWriter; |
9 | 8 | import java.nio.channels.ClosedChannelException; |
10 | 9 | import java.nio.charset.StandardCharsets; |
11 | 10 | import java.time.Duration; |
|
30 | 29 | import com.agentclientprotocol.sdk.spec.AcpSchema.JSONRPCMessage; |
31 | 30 | import com.agentclientprotocol.sdk.util.Assert; |
32 | 31 | import jakarta.servlet.AsyncContext; |
| 32 | +import jakarta.servlet.AsyncEvent; |
| 33 | +import jakarta.servlet.AsyncListener; |
33 | 34 | import jakarta.servlet.ServletException; |
| 35 | +import jakarta.servlet.ServletOutputStream; |
| 36 | +import jakarta.servlet.WriteListener; |
34 | 37 | import jakarta.servlet.http.HttpServlet; |
35 | 38 | import jakarta.servlet.http.HttpServletRequest; |
36 | 39 | import jakarta.servlet.http.HttpServletResponse; |
@@ -92,6 +95,8 @@ public class StreamableHttpAcpAgentTransport { |
92 | 95 |
|
93 | 96 | private static final int MAX_REPLAY_EVENTS = 1024; |
94 | 97 |
|
| 98 | + private static final int MAX_PENDING_SSE_EVENTS = 1024; |
| 99 | + |
95 | 100 | private static final Duration INITIALIZE_TIMEOUT = Duration.ofSeconds(30); |
96 | 101 |
|
97 | 102 | private enum ScopeKind { |
@@ -827,68 +832,118 @@ synchronized void subscribe(AsyncContext asyncContext, HttpServletResponse respo |
827 | 832 | } |
828 | 833 | SseSubscriber subscriber = new SseSubscriber(this, asyncContext, response); |
829 | 834 | subscribers.add(subscriber); |
| 835 | + subscriber.start(); |
830 | 836 | if (replayOpen) { |
831 | 837 | for (String payload : new ArrayList<>(replay)) { |
832 | 838 | subscriber.send(payload); |
833 | 839 | } |
834 | 840 | replay.clear(); |
835 | 841 | replayOpen = false; |
836 | 842 | } |
837 | | - subscriber.flush(); |
| 843 | + subscriber.drain(); |
838 | 844 | } |
839 | 845 |
|
840 | 846 | void remove(SseSubscriber subscriber) { |
841 | 847 | subscribers.remove(subscriber); |
842 | 848 | } |
843 | 849 |
|
844 | | - void close() { |
| 850 | + synchronized void close() { |
845 | 851 | if (closed.compareAndSet(false, true)) { |
846 | 852 | subscribers.forEach(SseSubscriber::close); |
847 | 853 | subscribers.clear(); |
848 | | - synchronized (this) { |
849 | | - replay.clear(); |
850 | | - } |
| 854 | + replay.clear(); |
851 | 855 | } |
852 | 856 | } |
853 | 857 |
|
854 | 858 | } |
855 | 859 |
|
856 | | - private final class SseSubscriber { |
| 860 | + private final class SseSubscriber implements AsyncListener, WriteListener { |
857 | 861 |
|
858 | 862 | private final OutboundStream parent; |
859 | 863 |
|
860 | 864 | private final AsyncContext asyncContext; |
861 | 865 |
|
862 | | - private final PrintWriter writer; |
| 866 | + private final ServletOutputStream output; |
| 867 | + |
| 868 | + private final ArrayDeque<byte[]> pendingEvents = new ArrayDeque<>(); |
863 | 869 |
|
864 | 870 | private final AtomicBoolean closed = new AtomicBoolean(false); |
865 | 871 |
|
866 | 872 | SseSubscriber(OutboundStream parent, AsyncContext asyncContext, HttpServletResponse response) throws IOException { |
867 | 873 | this.parent = parent; |
868 | 874 | this.asyncContext = asyncContext; |
869 | | - this.writer = response.getWriter(); |
| 875 | + this.output = response.getOutputStream(); |
| 876 | + } |
| 877 | + |
| 878 | + void start() { |
| 879 | + asyncContext.addListener(this); |
| 880 | + output.setWriteListener(this); |
870 | 881 | } |
871 | 882 |
|
872 | 883 | synchronized void send(String payload) { |
873 | 884 | if (closed.get()) { |
874 | 885 | return; |
875 | 886 | } |
876 | | - writer.write("data: "); |
877 | | - writer.write(payload); |
878 | | - writer.write("\n\n"); |
879 | | - writer.flush(); |
880 | | - if (writer.checkError()) { |
| 887 | + if (pendingEvents.size() == MAX_PENDING_SSE_EVENTS) { |
| 888 | + logger.warn("Closing backpressured SSE subscriber after {} pending events", MAX_PENDING_SSE_EVENTS); |
881 | 889 | close(); |
| 890 | + return; |
882 | 891 | } |
| 892 | + pendingEvents.addLast(("data: " + payload + "\n\n").getBytes(StandardCharsets.UTF_8)); |
| 893 | + drain(); |
883 | 894 | } |
884 | 895 |
|
885 | | - synchronized void flush() { |
886 | | - writer.flush(); |
| 896 | + synchronized void drain() { |
| 897 | + try { |
| 898 | + while (!closed.get() && output.isReady()) { |
| 899 | + byte[] event = pendingEvents.pollFirst(); |
| 900 | + if (event == null) { |
| 901 | + return; |
| 902 | + } |
| 903 | + output.write(event); |
| 904 | + } |
| 905 | + } |
| 906 | + catch (IOException e) { |
| 907 | + close(); |
| 908 | + } |
| 909 | + } |
| 910 | + |
| 911 | + @Override |
| 912 | + public void onWritePossible() { |
| 913 | + drain(); |
| 914 | + } |
| 915 | + |
| 916 | + @Override |
| 917 | + public void onError(Throwable error) { |
| 918 | + close(); |
| 919 | + } |
| 920 | + |
| 921 | + @Override |
| 922 | + public void onComplete(AsyncEvent event) { |
| 923 | + close(); |
| 924 | + } |
| 925 | + |
| 926 | + @Override |
| 927 | + public void onTimeout(AsyncEvent event) { |
| 928 | + close(); |
| 929 | + } |
| 930 | + |
| 931 | + @Override |
| 932 | + public void onError(AsyncEvent event) { |
| 933 | + close(); |
| 934 | + } |
| 935 | + |
| 936 | + @Override |
| 937 | + public void onStartAsync(AsyncEvent event) { |
| 938 | + event.getAsyncContext().addListener(this); |
887 | 939 | } |
888 | 940 |
|
889 | 941 | void close() { |
890 | 942 | if (closed.compareAndSet(false, true)) { |
891 | 943 | parent.remove(this); |
| 944 | + synchronized (this) { |
| 945 | + pendingEvents.clear(); |
| 946 | + } |
892 | 947 | try { |
893 | 948 | asyncContext.complete(); |
894 | 949 | } |
|
0 commit comments