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

Fixed socket timeout #102

Open
tkcstms opened this issue Mar 1, 2023 · 2 comments
Open

Fixed socket timeout #102

tkcstms opened this issue Mar 1, 2023 · 2 comments

Comments

@tkcstms
Copy link

tkcstms commented Mar 1, 2023

I had an issue (and a few hours of figuring out what was going on) with the HTTP server example.
It always loaded for several seconds before anything appeared in the web browser.
I figured out when creating an instance of a WSGIServer, the socket timeout was hard coded to be 20 seconds.
I don't know if I have some error on my side but I always had to wait 20 seconds before the request has been answered.

I modified the constructor of WSGIServer to fit my needs and now it accepts a float parameter to set the socket timeout and it works perfectly for me.

Please feel free to correct me if I missed something!

`Modified constructor of WSGIServer

def __init__(
    self,
    port: int = 80,
    debug: bool = False,
    timeout: float = 1,
    application: Optional[callable] = None,
) -> None:
    """
    :param int port: WSGI server port, defaults to 80.
    :param bool debug: Enable debugging, defaults to False.
    :param Optional[callable] application: Application to call in response to a HTTP request.
    """
    self.application = application
    self.port = port
    self._timeout = timeout
    self._client_sock = []
    self._debug = debug

    self._response_status = None
    self._response_headers = []
    if _the_interface.chip == "w5100s":
        self.MAX_SOCK_NUM = const(2)
    else:
        self.MAX_SOCK_NUM = const(6)
    if self._debug:
        print("Max sockets: ", self.MAX_SOCK_NUM)`
@fieldOfView
Copy link

The real question is why the timeout happens always on consecutive requests.

@kocinski15
Copy link

kocinski15 commented Jun 2, 2023

I noticed exactly the same. When running WSGIServer on RPI2 with Wiznet 5100 Hat , every request took 20 secs to answer.
Below is the debug from simple /led_off request showing long processing time from 'bytes available' to the answer:


Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Wiznet5k WebServer Test(DHCP)
My Link is: 1
Chip Version: w5100s
MAC Address: ['0x0', '0x1', '0x2', '0x3', '0x4', '0x5']
My IP address is: 192.168.1.120
w5100s
Open this IP in your browser: 192.168.1.120
*** Get socket.
Reserved sockets: [False, False, False]
Allocated socket # 1.

  • Listening on port=80, ip=192.168.1.120
    *** Opening socket 1
  • Opening W5k Socket, protocol=33
    *** Get socket.
    Reserved sockets: [True, False, False]
    Allocated socket # 2.
  • Listening on port=80, ip=192.168.1.120
    *** Opening socket 2
  • Opening W5k Socket, protocol=33
    *** Get socket.
    Reserved sockets: [True, True, False]
    Allocated socket # 3.
  • Listening on port=80, ip=192.168.1.120
    *** Opening socket 3
  • Opening W5k Socket, protocol=33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 2, protocol 33
    socket_available called on socket 3, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 2, protocol 33
    socket_available called on socket 3, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    Bytes avail. on sock: 90
  • Processing 90 bytes of data
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 1, protocol 33
    LED off!
    *** Disconnecting socket 1
    *** Closing socket 1
    Waiting for close command to process…
    Waiting for socket to close…
    Socket has closed.
    *** Get socket.
    Reserved sockets: [False, True, True]
    Allocated socket # 1.
  • Listening on port=80, ip=192.168.1.120
    *** Opening socket 1
  • Opening W5k Socket, protocol=33
    socket_available called on socket 2, protocol 33
    socket_available called on socket 3, protocol 33
    socket_available called on socket 1, protocol 33
    socket_available called on socket 2, protocol 33
    socket_available called on socket 3, protocol 33
    socket_available called on socket 1, protocol 33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants