From 68cece624e2f761fcbd07f1ee285c69daf871d7d Mon Sep 17 00:00:00 2001 From: Benjamin Schmitt Date: Wed, 17 Apr 2024 18:44:31 +0200 Subject: [PATCH 1/2] add documentation docstring --- lightbug_http/sys/net.mojo | 22 ++++++++++++++++++++++ lightbug_http/sys/server.mojo | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lightbug_http/sys/net.mojo b/lightbug_http/sys/net.mojo index 8bd6aeae..71923274 100644 --- a/lightbug_http/sys/net.mojo +++ b/lightbug_http/sys/net.mojo @@ -81,6 +81,10 @@ fn getaddrinfo[ @value struct SysListener: + """ + TCP server that listens for incoming connections. + """ + var fd: c_int var __addr: TCPAddr @@ -267,6 +271,15 @@ struct addrinfo_macos(AnAddrInfo): ) fn get_ip_address(self, host: String) raises -> in_addr: + """ + Get IP address from host. + + Args: + host: String - The host to get IP from. + + Returns: + UInt32 - The IP address. + """ var host_ptr = to_char_ptr(host) var servinfo = Pointer[Self]().alloc(1) servinfo.store(Self()) @@ -323,6 +336,15 @@ struct addrinfo_unix(AnAddrInfo): ) fn get_ip_address(self, host: String) raises -> in_addr: + """ + Get IP address from host. + + Args: + host: String - The host to get IP from. + + Returns: + UInt32 - The IP address. + """ var host_ptr = to_char_ptr(host) var servinfo = Pointer[Self]().alloc(1) servinfo.store(Self()) diff --git a/lightbug_http/sys/server.mojo b/lightbug_http/sys/server.mojo index 4b562976..4ba96a0e 100644 --- a/lightbug_http/sys/server.mojo +++ b/lightbug_http/sys/server.mojo @@ -12,6 +12,10 @@ from lightbug_http.strings import next_line, NetworkType struct SysServer: + """ + Server that accept request and deliver HTTP services. + """ + var error_handler: ErrorHandler var name: String @@ -42,6 +46,13 @@ struct SysServer: self.ln = SysListener() fn get_concurrency(self) -> Int: + """ + Get the concurrency level which is either : + The configured max_concurrent_connections or the DefaultConcurrency. + + Returns: + Int: concurrency level for the server. + """ var concurrency = self.max_concurrent_connections if concurrency <= 0: concurrency = DefaultConcurrency @@ -50,11 +61,28 @@ struct SysServer: fn listen_and_serve[ T: HTTPService ](inout self, address: String, handler: T) raises -> None: + """ + Listen for incoming connections and serve HTTP requests. + + Args: + address : String - The address (host:port) to listen on. + handler : HTTPService - An object that handles incoming HTTP requests. + """ var __net = SysNet() var listener = __net.listen(NetworkType.tcp4.value, address) self.serve(listener, handler) fn serve[T: HTTPService](inout self, ln: SysListener, handler: T) raises -> None: + """ + Serve HTTP requests. + + Args: + ln : SysListener - TCP server that listens for incoming connections. + handler : HTTPService - An object that handles incoming HTTP requests. + + Raises: + If there is an error while serving requests. + """ # var max_worker_count = self.get_concurrency() # TODO: logic for non-blocking read and write here, see for example https://github.com/valyala/fasthttp/blob/9ba16466dfd5d83e2e6a005576ee0d8e127457e2/server.go#L1789 From bb6ad9e81f1233838a909c1eb50e251b196da35c Mon Sep 17 00:00:00 2001 From: Val Date: Wed, 17 Apr 2024 21:08:46 +0200 Subject: [PATCH 2/2] minor edits --- lightbug_http/sys/net.mojo | 13 ++++++++----- lightbug_http/sys/server.mojo | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lightbug_http/sys/net.mojo b/lightbug_http/sys/net.mojo index 71923274..bdd73d51 100644 --- a/lightbug_http/sys/net.mojo +++ b/lightbug_http/sys/net.mojo @@ -82,7 +82,7 @@ fn getaddrinfo[ @value struct SysListener: """ - TCP server that listens for incoming connections. + A TCP listener that listens for incoming connections and can accept them. """ var fd: c_int @@ -272,10 +272,11 @@ struct addrinfo_macos(AnAddrInfo): fn get_ip_address(self, host: String) raises -> in_addr: """ - Get IP address from host. + Returns an IP address based on the host. + This is a MacOS-specific implementation. Args: - host: String - The host to get IP from. + host: String - The host to get the IP from. Returns: UInt32 - The IP address. @@ -337,7 +338,8 @@ struct addrinfo_unix(AnAddrInfo): fn get_ip_address(self, host: String) raises -> in_addr: """ - Get IP address from host. + Returns an IP address based on the host. + This is a Unix-specific implementation. Args: host: String - The host to get IP from. @@ -380,7 +382,8 @@ struct addrinfo_unix(AnAddrInfo): fn create_connection(sock: c_int, host: String, port: UInt16) raises -> SysConnection: - """Connect to a server using a socket. + """ + Connect to a server using a socket. Args: sock: Int32 - The socket file descriptor. diff --git a/lightbug_http/sys/server.mojo b/lightbug_http/sys/server.mojo index 4ba96a0e..d0a08137 100644 --- a/lightbug_http/sys/server.mojo +++ b/lightbug_http/sys/server.mojo @@ -13,7 +13,7 @@ from lightbug_http.strings import next_line, NetworkType struct SysServer: """ - Server that accept request and deliver HTTP services. + A Mojo-based server that accept incoming requests and delivers HTTP services. """ var error_handler: ErrorHandler @@ -47,8 +47,8 @@ struct SysServer: fn get_concurrency(self) -> Int: """ - Get the concurrency level which is either : - The configured max_concurrent_connections or the DefaultConcurrency. + Retrieve the concurrency level which is either + the configured max_concurrent_connections or the DefaultConcurrency. Returns: Int: concurrency level for the server.