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

tcp connection amount of websocket is increasing when a large cookie is existed

Open
#3,467 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

Hi, there,
I have a backend service【websocket service】(https://github.com/mrniko/netty-socketio).

when there is a large cookie in the request , the backend【websocket side】 will print such error:
image

io.netty.handler.codec.http.websocketx.WebSockeetHandshakeException: not a WebSocket handshakerequest: missing upgrade

image
image
image

although I close the browser , the ESTABLISHED connection is still existed.
image

if I visit the socket server page directly in the browser, when I close the browser the connection will be gone.

Is there anyone also meet such scenario ?
How can we handle it ?

netty:
4.1.96.Final

spring-cloud:
2021.0.8

(1)html page for client:

<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>My Netty Socket client</title>
		<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
		<script src="https://cdn.bootcss.com/socket.io/2.1.0/socket.io.js"></script>
		<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
		<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
		<style>
		body {
		padding:20px;
		}
		#console {
		height: 400px;
		overflow: auto;
		}
		.username-msg {color:orange;}
		.connect-msg {color:green;}
		.disconnect-msg {color:red;}
		.send-msg {color:#888}
		</style>
	
		<script type="text/javascript">
			var clientid = 'testclient1';
			var targetClientId = 'testclient2';
			
			var socket;
		
			function sendDisconnect() {
				socket.disconnect();
			}
			function sendMessage() {
				var message = $('#msg').val();
				$('#msg').val('');
				var jsonObject = {sourceClientId: clientid,
				targetClientId: targetClientId,
				msgType: 'chat',
				msgContent: message};
				socket.emit('messageevent', jsonObject);
			}
			function output(message) {
				var currentTime = "<span class='time'>" + moment().format('HH:mm:ss.SSS') + "</span>";
				var element = $("<div>" + currentTime + " " + message + "</div>");
				$('#console').prepend(element);
			}

			$(function() {
				var hostAddress = $('#hostAddress').val()+'?clientid=' + clientid;
				console.log('------>'+hostAddress);
				console.log('DOM is fully loaded and parsed');
				socket = io.connect(hostAddress);
				socket.on('connect', function() {
					output('<span class="connect-msg">Client has connected to the server!</span>');
				});
				socket.on('messageevent', function(data) {
					output('<span class="username-msg">' + data.sourceClientId + ':</span> ' + data.msgContent);
				});
				socket.on('disconnect', function() {
					output('<span class="disconnect-msg">The client has disconnected!</span>');
				});
				
			});
		</script>
	</head>

	<body>
		<h1>Netty-socketio Demo Chat</h1>
		<br/>
		<div id="console" class="well">
		</div>
		<form class="well form-inline" onsubmit="return false;">
			<input type="hidden" id="hostAddress" th:value="${hostAddress}" />
			<input id="msg" class="input-xlarge" type="text" placeholder="Type something..."/>
			<button type="button" onClick="sendMessage()" class="btn" id="send">Send</button>
			<button type="button" onClick="sendDisconnect()" class="btn">Disconnect</button>
		</form>
	</body>
</html>
</html>

(2) socket server

@SpringBootApplication
public class App {
        @Bean
	public SocketIOServer socketIOServer() {
		Configuration config = new Configuration();
		config.setHostname(host);
		config.setPort(port);
		config.setPingInterval(milliSecond);
		config.setPingTimeout(milliSecond*2);
		config.setUpgradeTimeout(milliSecond*2);
		config.setExceptionListener(new ExceptionListener(){

			@Override
			public void onEventException(Exception e, List<Object> args, SocketIOClient client) {
				System.out.println("onEventException"+e.getMessage());
			}

			@Override
			public void onDisconnectException(Exception e, SocketIOClient client) {
				System.out.println("onDisconnectException"+e.getMessage());
			}

			@Override
			public void onConnectException(Exception e, SocketIOClient client) {
				System.out.println("onConnectException"+e.getMessage());
			}

			@Override
			public void onPingException(Exception e, SocketIOClient client) {
				System.out.println("onPingException"+e.getMessage());
			}

			@Override
			public void onPongException(Exception e, SocketIOClient client) {
				System.out.println("onPongException"+e.getMessage());
			}

			@Override
			public boolean exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception {
				System.out.println("exceptionCaught");
				return false;
			}

			@Override
			public void onAuthException(Throwable e, SocketIOClient client) {
				System.out.println("onAuthException");
			}
			
		});
		config.setAuthorizationListener(new AuthorizationListener(){
			@Override
			public AuthorizationResult getAuthorizationResult(HandshakeData data) {
				AuthorizationResult r = new AuthorizationResult(true);
				return r;
			}
			
		});
		final SocketIOServer server = new SocketIOServer(config);
		return server;
	}

       @Bean
	public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
		return new SpringAnnotationScanner(socketServer);
	}

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}
 // -------------------------------------------------------
@Component
public class MessageEventHandler {
	private final SocketIOServer server;

	@Autowired
	public MessageEventHandler(SocketIOServer server) {
		this.server = server;
	}

	@OnConnect
	public void onConnect(SocketIOClient client) {
		String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
                client.sendEvent("message", "onConnect back");
		System.out.println("onConnect----->"+clientId);
	}

	@OnDisconnect
	public void onDisconnect(SocketIOClient client) {
		String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
		System.out.println("onDisconnect----->"+clientId);
	}

	@OnEvent(value = "messageevent")
	public void onEvent(SocketIOClient client, AckRequest request, MessageInfo data) {
		System.out.println("onEvent----->"+data.toString());
	}
}

(3)my gateway route configuration

        {
             // the route for the socket io , the index html page will trigger the websocket request
            "predicate": "Paths: [/socket.io/**], match trailing slash: true",
            "route_id": "a60ff04c-2f9d-4298-9a1e-4909badf343c",
            "filters": [],
            "uri": "http://10.11.9.68:8081",
            "order": 60
        },
        {
             // the route for the index page , such as visit  https://xxxxxxx.xxxx.com/demo-socket/index
            "predicate": "Paths: [/demo-socket/**], match trailing slash: true",    
            "route_id": "d9bd4835-91a6-4192-a5c2-fd7e8234f191",
            "filters": [
                "[[StripPrefix parts = 1], order = 1]"
            ],
            "uri": "http://backend-demo-socket-svc.gateway:80",
            "order": 61
        }

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 Spring Cloud Gateway route configuration, the Netty 4.1.96.Final WebSocket handshake error, and the supplied browser and server examples. Reproduce the behavior with a large cookie and compare direct access with gateway access; done requires identifying whether Gateway causes the lingering connections and documenting or fixing the confirmed behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend, networking
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.