spring-cloud / spring-cloud/spring-cloud-gateway

There are exceptions when Spring Cloud Gateway proxies the SSE interface

Open
#3,833 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Describe the bug
We have an SSE (Server-Sent Events) interface that forwards requests to OpenAI's chat API (v1/chat/completions). When directly accessing this SSE interface, the streaming response works fine. However, when the request is forwarded through Spring Cloud Gateway, the streaming response only returns partially—it suddenly gets blocked mid-stream while the connection remains open without disconnecting.

Sample
-- sse api code

// other code ignored 
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;

	@NotNull
	private static WebClient getWebClient() {
		ConnectionProvider provider =
				ConnectionProvider.builder("custom")
						.maxConnections(120)
						.maxIdleTime(Duration.ofSeconds(60))
						.maxLifeTime(Duration.ofSeconds(60))
						.pendingAcquireTimeout(Duration.ofSeconds(120))
						.evictInBackground(Duration.ofSeconds(240))
						.build();
		HttpClient httpClient = HttpClient.create(provider);
		// 使用创建的 HttpClient 来构建 WebClient
		WebClient webClient = WebClient.builder()
				.clientConnector(new ReactorClientHttpConnector(httpClient))
				.build();
		return webClient;
	}

	@PostMapping(path = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
	public Flux<String> chat(@RequestBody Map<?,?> param) {
		// 关闭Nginx缓存,直接推流
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("X-Accel-Buffering", "no");
		// 流式事件流响应
		response.setContentType(MediaType.TEXT_EVENT_STREAM_VALUE);
		WebClient webClient = getWebClient();
		return webClient
				.post()
				.uri("http://some-domain.com/v1/chat/completions")
                                 .header("Authorization", "Bearer your_api_key")
				.contentType(MediaType.APPLICATION_JSON)
				.bodyValue(JSONUtil.objectToJson(param))
				.retrieve()
				.bodyToFlux(String.class)
				.doOnNext(it->{
					System.out.println(it);
				});
	}

// ignore other code

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported SSE endpoint and the Spring Cloud Gateway proxy path, using the provided WebClient example to reproduce the difference between direct and forwarded requests. Identify why the streamed response stops while the connection remains open; done means the gateway forwards the complete SSE stream without stalling.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.