Skip to content
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

websocket server x86 crash with stress test(max conn:2000) #432

Closed
xingyun86 opened this issue May 26, 2021 · 1 comment
Closed

websocket server x86 crash with stress test(max conn:2000) #432

xingyun86 opened this issue May 26, 2021 · 1 comment
Labels
Question Further information is requested

Comments

@xingyun86
Copy link

xingyun86 commented May 26, 2021

websocket server x86 crash with stress test(max conn:2000),x64 is ok(3W)
error string:
“resource unavailable try again: resource unavailable try again”
source code:https://github.com/oatpp/example-websocket/tree/master/server (master newest)
test batch(python):

#!/usr/bin/python
#-*- coding:utf-8 -*-
 
import websocket
import time
import threading
import json
import multiprocessing
from threadpool import ThreadPool, makeRequests
 
#修改成自己的websocket地址
WS_URL = "ws://127.0.0.1:8080/ws" 
#定义进程数
processes=1
#定义线程数(每个文件可能限制1024个,可以修改fs.file等参数)
thread_num=2000
 
def on_message(ws, message):
     print(message)
     pass
 
def on_error(ws, error):
    print(error)
    pass
def on_close(ws):
    print("### closed ###")
    pass
def on_open(ws):
    def send_trhead():
        #设置你websocket的内容
        send_info = {"cmd": "refresh", "data": {"room_id": "58", "wx_user_id": 56431}}
        #每隔10秒发送一下数据使链接不中断
        while True:
            time.sleep(10)
            ws.send(json.dumps(send_info))
 
    t = threading.Thread(target=send_trhead)
    t.start()
 
def on_start(num):
    time.sleep(num%20)
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(WS_URL,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever()
 
def thread_web_socket():
    #线程池
    pool = ThreadPool(thread_num)
    num = list()
    #设置开启线程的数量
    for ir in range(thread_num):
        num.append(ir)
    requests = makeRequests(on_start, num)
    [pool.putRequest(req) for req in requests]
    pool.wait()
 
if __name__ == "__main__":
    pool = multiprocessing.Pool(processes=processes)
    for i in range(processes):
        pool.apply_async(thread_web_socket)
    pool.close()
    pool.join()
@lganzzzo
Copy link
Member

Hey @xingyun86 ,

For this particular example project, it's expected behavior.

The example project is the multithreaded server. Since it spawns a thread per WebSocket connection it has a limit on max possible threads spawned (connections).

If you need to handle a huge number of connections then you have to use an asynchronous WebSocket server

@lganzzzo lganzzzo added the Question Further information is requested label May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants