Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
|
7ff80c8a9f | |
|
a0d1274bbe | |
|
01d48e9537 | |
|
5e79fba1b4 |
6
LICENSE
6
LICENSE
|
@ -1 +1,5 @@
|
|||
Handy-Http by Andrew Lalis is marked with CC0 1.0 Universal. To view a copy of this license, visit https://creativecommons.org/publicdomain/zero/1.0/
|
||||
Copyright 2025 Andrew Lalis
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
|
|
@ -26,7 +26,7 @@ class MyHandler : HttpRequestHandler {
|
|||
}
|
||||
|
||||
void main() {
|
||||
HttpTransport tp = new TaskPoolHttp1Transport(new MyHandler(), 8080);
|
||||
HttpTransport tp = new TaskPoolHttp1Transport(new MyHandler());
|
||||
tp.start();
|
||||
}
|
||||
```
|
|
@ -7,34 +7,79 @@ import handy_http_transport.http1.transport;
|
|||
import handy_http_primitives;
|
||||
import slf4d;
|
||||
|
||||
/**
|
||||
* Configuration options to provide when creating a new Http1Transport
|
||||
* instance.
|
||||
*/
|
||||
struct Http1TransportConfig {
|
||||
/// The host address to bind to.
|
||||
string host;
|
||||
/// The port to bind to.
|
||||
ushort port;
|
||||
/// The number of workers to use in the task pool.
|
||||
size_t workerCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the default configuration options if none are provided. They are:
|
||||
* * Host address 127.0.0.1
|
||||
* * Port 8080
|
||||
* * Worker count of 5.
|
||||
* Returns: The default configuration.
|
||||
*/
|
||||
Http1TransportConfig defaultConfig() {
|
||||
return Http1TransportConfig(
|
||||
"127.0.0.1",
|
||||
8080,
|
||||
5
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of Http1Transport which uses D's standard library
|
||||
* parallelization, where each incoming client request is turned into a task
|
||||
* and submitted to the standard task pool.
|
||||
*/
|
||||
class TaskPoolHttp1Transport : Http1Transport {
|
||||
this(HttpRequestHandler requestHandler, ushort port = 8080) {
|
||||
super(requestHandler, port);
|
||||
private TaskPool httpTaskPool;
|
||||
private immutable Http1TransportConfig config;
|
||||
|
||||
/**
|
||||
* Creates a new transport instance using a std.parallelism TaskPool for
|
||||
* handling requests.
|
||||
* Params:
|
||||
* requestHandler = The handler to call for each incoming request.
|
||||
* workerCount = The number of workers to use in the task pool.
|
||||
* port = The port.
|
||||
*/
|
||||
this(HttpRequestHandler requestHandler, in Http1TransportConfig config = defaultConfig()) {
|
||||
super(requestHandler, config.port);
|
||||
this.config = config;
|
||||
this.httpTaskPool = new TaskPool(config.workerCount);
|
||||
}
|
||||
|
||||
override void runServer() {
|
||||
Socket serverSocket = new TcpSocket();
|
||||
serverSocket.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, 1);
|
||||
serverSocket.bind(parseAddress("127.0.0.1", port));
|
||||
serverSocket.bind(parseAddress(config.host, config.port));
|
||||
debugF!"Bound the server socket to %s"(serverSocket.localAddress);
|
||||
serverSocket.listen(1024);
|
||||
debug_("Server is now listening.");
|
||||
|
||||
while (super.isRunning) {
|
||||
try {
|
||||
trace("Waiting to accept a new socket.");
|
||||
Socket clientSocket = serverSocket.accept();
|
||||
trace("Accepted a new socket.");
|
||||
auto t = task!handleClient(clientSocket, requestHandler);
|
||||
taskPool().put(t);
|
||||
this.httpTaskPool.put(t);
|
||||
trace("Added handleClient() task to the task pool.");
|
||||
} catch (SocketAcceptException e) {
|
||||
warn("Failed to accept socket connection.", e);
|
||||
}
|
||||
}
|
||||
serverSocket.close();
|
||||
this.httpTaskPool.stop();
|
||||
}
|
||||
|
||||
override void stop() {
|
||||
|
|
Loading…
Reference in New Issue