mirror of
https://github.com/soypat/lneto.git
synced 2026-08-13 19:33:42 +00:00
5c54030f19
* add http/httphi * begin adding httphi tests * claude found neat bugs * add low level Handle function and more tests * more tests, run go generate * add Hijacker-like functionality * improve locking and acquisition of Exchanges in reconfiguring * several bugfixes, add internal.IntLen, round up http-linux example with new router API * small nit * add benchmarks * add query handling * remove ForEach pattern, allocates in TinyGo * massive documentation push and code reordering in files * Router.Handle returns error after being torn down * run go fix * rework Mux interface to receive a string request path * add MethodFrom * minor doc nit * fail on incomplete staging * add raw buffer access * add streaming API distinct from Exchange * begin adding multipart form logic * finish rounding up multipart form parsing * remove status type * first Multipart approach * begin adding readMultiPart * add Exchange.ReadMultiparts reimagining of clanker slop * ai insists with backoffs * simplify clanker slop * apply go fix * add a pattern argument to Mux * explicit header key/value alloc and add ExchangeConfig * fix tests after excplicit header alloc change * fix examples * run go fix * expose rawsock as experimental package (will use for external benchmarks) * remove backoff from form parsing * @MDr164 suggestions get potential fixes * apply go fix * add examples * add README.md * fix rawsock tinygo implementation * apply @MDr164 various fixes * update documentation on ContentLength methods and fix bug in Form reset on empty body * fix tests * add fuzz tests * run go fix * io.ErrNoProgress on parsing form spin * run go fix * remove backoff assumption from Router * httphi.Handle rejects unsupported protocols * go format router.go * add kvbuffer * rewrite Cookie with KVBuffer * mid refactor of KVBuffer into Header * work on KVBuffer exhausted semantics * add Go's ServeMux Request.PathValue access semantics to Exchange, Mux and MuxSlice * add PathValue example * document all the things; improve req Query semantics; add Form.EnableBufferGrowth * unexport kvBuffer * add Exchange.PathValueAppend * use stdlib in example instead of rawsock * remove rawsock from http example * add darwin arch rawsock * fix example * rename Router.TeardownGoroutines to Shutdown matching http.Server.Shutdown * rename types and identifiers * @MDr164 Content-Type and Transfer-Encoding bug catches
52 lines
1.9 KiB
Markdown
52 lines
1.9 KiB
Markdown
# httφ
|
|
|
|
Heapless HTTP/1.1 router with `net/http`-shaped handlers. Benchmarked against `net/http` and friends at [**soypat/httpbench**](https://github.com/soypat/httpbench).
|
|
|
|
## Why
|
|
|
|
`net/http` allocates per request: `Request`, header map, response buffer, goroutine. Fine on a
|
|
server, fatal on a microcontroller. httφ pays that cost once, at `Router.Configure`:
|
|
|
|
- Exchanges and goroutines are fixed there. Serving allocates nothing; memory does not grow with load.
|
|
- No free exchange means `Handle` refuses the connection unclosed, rather than allocating one more of everything.
|
|
- `Handle` takes an `io.ReadWriteCloser`, so the router runs over a raw socket, an [`lneto`](https://github.com/soypat/lneto) TCP stack or a test pipe. No listener, no OS, no clock.
|
|
|
|
Parsing is [`httpraw`](../httpraw).
|
|
|
|
## Example
|
|
|
|
```go
|
|
var mux httphi.MuxSlice
|
|
mux.Handle("GET /", func(ex *httphi.Exchange) {
|
|
ex.WriteBody([]byte("hello world"))
|
|
})
|
|
|
|
var router httphi.Router
|
|
err := router.Configure(httphi.RouterConfig{
|
|
FixedNumGoroutines: 4, // 4 workers, 4 exchanges, allocated here and never again.
|
|
MaxAwaitingConns: 8, // Queue depth. Full queue drops connections.
|
|
RequestHeaderBufferSize: 1024,
|
|
ResponseHeaderMinBufferSize: 32, // Shares the request buffer.
|
|
RequestNumHeaderKVCap: 32,
|
|
Backoff: func(uint) time.Duration { return time.Millisecond },
|
|
Mux: &mux,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for {
|
|
conn, err := listener.Accept() // Accepting is the caller's job.
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
if err = router.Handle(conn); err != nil {
|
|
conn.Close() // Refused: never blocks, never queues unboundedly.
|
|
}
|
|
}
|
|
```
|
|
|
|
`FixedNumGoroutines: -1` gives the unbounded flavor, a goroutine and an exchange per connection.
|
|
|
|
Runnable server over raw Linux sockets, plus query, form and multipart handlers:
|
|
[`example_test.go`](./example_test.go).
|