简体   繁体   中英

Owin.Websocket exceeding message size causes high CPU usage

I'm using Owin.WebSocket library to receive a stream of images to an Asp.Net WebAPI app. I have created a websocket using Owin.Websocket.WebSocketConnection to receive messages from my Frontend.

 public class MyWebScoket : WebSocketConnection
 {
    protected MyWebScoket(int maxMessageSize = 65536) : base(maxMessageSize)
    {
       //logic
    }

    public override void OnClose(WebSocketCloseStatus? closeStatus, string closeStatusDescription)
    {
        //logic
    }

    public override Task OnMessageReceived(ArraySegment<byte> message, WebSocketMessageType type)
    {
        //logic
    }

    public override void OnOpen()
    {
        //logic
    }

    public override void OnReceiveError(Exception error)
    {
        //logic
    }
}

When the message size is below the set message size, CPU usage of API is normal and everything is functioning as it should, however when the message size exceeds the limit, CPU usage of API increases dramatically, causing my machine to stop responding even to mouse clicks.

This seems strange, as the message sending rate is always the same.

What is the reason of such behavior?

I added breakpoints inside all overridden methods, but none of them is reached, I thought if the message limit size is exceeded the execution should go to OnReceiveError.

Any workarounds/ideas or alternatives are welcomed.

I have struggled with this issue my self when testing, As I do not do it professionally. It seems that, if you do not exceed the message size, the owin web socket will not use the CPU usage much but rather depend on the RAM memory, this way, when not exceeding - cpu usage was at 2-4% and at the top, 10% and my ram was between 5-8% and jumps to 13-14% tops.

However, if you exceed the size, owin web sockets develop unexpected behavior, causing the web sockets to use the free CPU instead.

I tried it on my personal PC - i7 -7700k 4 cors 8 threads, each and every one of the threads was 100% used and my memory stayed at about 2-3% with jumps to 10-14% every two minutes or so.

I do not know the cause for this behavior, only know that for now you should not exceed the message size as it will result in a very unlikely behavior, I am also trying to look if anyone as posted an official issue about it.

The one reason I can see (logically) for this behavior is that when the message exceeds the size, owin mechanics 'fear' that the RAM memory will not be sufficient to run the operation and crush and so it's counting on the cpu to handle the preasure but this is only my assumption.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM