LeaYeh / webserver

MIT License
3 stars 0 forks source link

[FEAT] Add impl. of keep alive correctly #42

Closed LeaYeh closed 2 months ago

LeaYeh commented 2 months ago

Logger created: console mode Log level set to: DEBUG 2024-09-02 22:38:39 [DEBUG] Global scope:
2024-09-02 22:38:39 [DEBUG] worker_processes: 1
2024-09-02 22:38:39 [DEBUG] worker_connections: 1024
2024-09-02 22:38:39 [DEBUG] HTTP scope:
2024-09-02 22:38:39 [DEBUG] client_max_body_size: 1048576
2024-09-02 22:38:39 [DEBUG] default_type: text/html
2024-09-02 22:38:39 [DEBUG] error_page:
2024-09-02 22:38:39 [DEBUG] 404 /404.html
2024-09-02 22:38:39 [DEBUG] Server block [0]:
2024-09-02 22:38:39 [DEBUG] Server block:
2024-09-02 22:38:39 [DEBUG] Server name: example1.com
2024-09-02 22:38:39 [DEBUG] Listen: 127.0.0.1:8888
2024-09-02 22:38:39 [DEBUG] Error log: logs/error.log error
2024-09-02 22:38:39 [DEBUG] Error log: log/error.log error
2024-09-02 22:38:39 [DEBUG] Keepalive timeout: 65
2024-09-02 22:38:39 [DEBUG] Location block [0]:
2024-09-02 22:38:39 [DEBUG] route: /
2024-09-02 22:38:39 [DEBUG] limit_except:
2024-09-02 22:38:39 [DEBUG] GET
2024-09-02 22:38:39 [DEBUG] POST
2024-09-02 22:38:39 [DEBUG] root: /var/www/html
2024-09-02 22:38:39 [DEBUG] index: default.html
2024-09-02 22:38:39 [DEBUG] autoindex: 0
2024-09-02 22:38:39 [DEBUG] cgi_extension:
2024-09-02 22:38:39 [DEBUG] cgi_path:
2024-09-02 22:38:39 [DEBUG] enable_upload: 0
2024-09-02 22:38:39 [DEBUG] upload_path:
2024-09-02 22:38:39 [INFO] Creating single reactor and single worker structure
2024-09-02 22:38:39 [DEBUG] Reactor::Reactor(ReactorType type)
2024-09-02 22:38:39 [DEBUG] Created epoll fd: 5
2024-09-02 22:38:39 [INFO] Listening on 127.0.0.1:8888
2024-09-02 22:38:39 [DEBUG] Acceptor setup with server_id: 0
2024-09-02 22:38:39 [DEBUG] Registering handler and set to non-blocking with fd: 6
2024-09-02 22:38:39 [INFO] Registered acceptor with fd: 6
2024-09-02 22:38:39 [DEBUG] Kernel::run()
2024-09-02 22:38:39 [INFO] Reactor is waiting for epoll events
2024-09-02 22:38:53 [DEBUG] Reactor received 1 events
2024-09-02 22:38:53 [INFO] Accepted connection on fd: 7
2024-09-02 22:38:53 [DEBUG] ConnectionHandler created with server_id: 0
2024-09-02 22:38:53 [DEBUG] Registering handler and set to non-blocking with fd: 7
2024-09-02 22:38:53 [DEBUG] Registered connection handler with fd: 7
2024-09-02 22:38:53 [INFO] Reactor is waiting for epoll events
2024-09-02 22:38:53 [DEBUG] Reactor received 1 events
2024-09-02 22:38:53 [DEBUG] ConnectionHandler::handle_event() on fd: 7 with events: EPOLLIN
2024-09-02 22:38:53 [DEBUG] Elapsed time: 0
2024-09-02 22:38:53 [DEBUG] Read 732 bytes from fd: 7
2024-09-02 22:38:53 [DEBUG] Buffer: GET / HTTP/1.1 Host: 127.0.0.1:8888 Connection: keep-alive Cache-Control: max-age=0 sec-ch-ua: "Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126" sec-ch-ua-mobile: ?0 sec-ch-ua-platform: "macOS" Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.7 Sec-Fetch-Site: cross-site Sec-Fetch-Mode: navigate Sec-Fetch-User: ?1 Sec-Fetch-Dest: document Accept-Encoding: gzip, deflate, br, zstd Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6

�� 2024-09-02 22:38:53 [INFO] Request is complete
2024-09-02 22:38:53 [DEBUG] Processing request
2024-09-02 22:38:53 [DEBUG] Responding to request
2024-09-02 22:38:53 [INFO] Keep-alive timeout or close request on fd: 7
2024-09-02 22:38:53 [DEBUG] Removed handler with fd: 7
2024-09-02 22:38:53 [INFO] Reactor is waiting for epoll events
2024-09-02 22:38:54 [DEBUG] Reactor received 1 events
2024-09-02 22:38:54 [INFO] Accepted connection on fd: 7
2024-09-02 22:38:54 [DEBUG] ConnectionHandler created with server_id: 0
2024-09-02 22:38:54 [DEBUG] Registering handler and set to non-blocking with fd: 7
2024-09-02 22:38:54 [DEBUG] Registered connection handler with fd: 7
2024-09-02 22:38:54 [INFO] Reactor is waiting for epoll events
2024-09-02 22:38:54 [DEBUG] Reactor received 1 events
2024-09-02 22:38:54 [DEBUG] ConnectionHandler::handle_event() on fd: 7 with events: EPOLLIN
2024-09-02 22:38:54 [DEBUG] Elapsed time: 0
2024-09-02 22:38:54 [DEBUG] Read 626 bytes from fd: 7
2024-09-02 22:38:54 [DEBUG] Buffer: GET /favicon.ico HTTP/1.1 Host: 127.0.0.1:8888 Connection: keep-alive sec-ch-ua: "Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126" sec-ch-ua-mobile: ?0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 sec-ch-ua-platform: "macOS" Accept: image/avif,image/webp,image/apng,image/svg+xml,image/,/*;q=0.8 Sec-Fetch-Site: same-origin Sec-Fetch-Mode: no-cors Sec-Fetch-Dest: image Referer: http://127.0.0.1:8888/ Accept-Encoding: gzip, deflate, br, zstd Accept-Language: zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6

2024-09-02 22:38:54 [INFO] Request is complete
2024-09-02 22:38:54 [DEBUG] Processing request
2024-09-02 22:38:54 [DEBUG] Responding to request
2024-09-02 22:38:54 [INFO] Keep-alive timeout or close request on fd: 7
2024-09-02 22:38:54 [DEBUG] Removed handler with fd: 7
2024-09-02 22:38:54 [INFO] Reactor is waiting for epoll events


This is really interested function, check the logger with code will be more easier to understand. Notice the logging info which I highlight in grey color.

  1. When the client tries to send requests then the connection with fd(7) be created
  2. Then the timeout happened, after 5sec in this example, the handler be removed and close fd(7)
  3. When a new request coming, the connection will be re-build and with fd(7) again
LeaYeh commented 2 months ago

I also fixe some memory leaks and fd leaks

LeaYeh commented 2 months ago

@yenthing i think empty might not mean correctly, I didn't list all event type in the explain function