spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

Gateway cannot repeatedly create SSE connections #2988

Closed jlibx closed 1 year ago

jlibx commented 1 year ago

Describe the bug SpringCloud Gateway 2.1.1

Sample In the springboot project,I created a SSE Emitter like this:

@GetMapping(value = "/init", produces = "text/event-stream")
    public SseEmitter init(HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/event-stream");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Connection", "keep-alive");

        SseSession sseSession = robotAppService.createSseSession(token());

        return sseSession.getSseEmitter();
    }

The specific code of RobotAppService is as follows:

public SseSession createSseSession(String token) {
        SseSession sseSession = sseSessionManager.getSseSession(token);
        // return existing session
        if (Objects.nonNull(sseSession)) {
            return sseSession;
        } else {
            Robot robot = robotService.initRobot(token);
            // this will be create SseEmitter
            sseSession = sseSessionManager.createSseSession(token, robot);
            // init dailog
            RobotDialogDetail robotDialogDetail = robotService.initRobotDialog(robot);
            robot.setLastDialog(robotDialogDetail);
            // sse message
            SseMessage sseMessage = sseMessageFactory.transform(robotDialogDetail);
            sseSession.sendMessageByRobot(sseMessage);
            iRedisCache.remove(String.join("", "ymtd:robot:chat:", token));
        }
        return sseSession;
    }

Request init through the gateway agent, the first time it is normal, and it has been sending after the second time。 image image

gowthaman-basuvaraj commented 1 year ago

i am also facing the similar issue, though the sse service is in javalin in our case

jlibx commented 1 year ago

This is right, the SSE service does not have any return body, it will be like this. Make sure you have the return body.