mirror of
https://github.com/soypat/lneto.git
synced 2026-08-03 06:27:52 +00:00
1884cfc9b7
* add Exchange.WriteBodyString * Mux.MaxPathValues and other improvements * Mux PathValue improvemnt and fixes * MuxSlice more method muxing improvements * diagram out interesting approach to form parsing for clanker * refactor RequestParseForm and achieve greatness in API design * explicit naming of headerCapacityKV value in kvBuffer.Reset * fix Mux bug not matching paths correctly; httpraw HTTP V1 naming applied * rename many examples,use httphi in examples,remove useless maxAwaitingConn field * add ipv4.String * add ipv4 UnspecifiedAddr and BroadcastAddr * add ethernet.String
httφ
Heapless HTTP/1.1 router with net/http-shaped handlers. Benchmarked against net/http and friends at 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
Handlerefuses the connection unclosed, rather than allocating one more of everything. Handletakes anio.ReadWriteCloser, so the router runs over a raw socket, anlnetoTCP stack or a test pipe. No listener, no OS, no clock.
Parsing is httpraw.
Example
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.
RequestHeaderBufferSize: 1024,
ResponseHeaderMinBufferSize: 32, // Shares the request buffer.
RequestNumHeaderKVCap: 32,
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.
Naming
Gonna be honest with y'all. I initially wanted it to be named httplo until I saw I could write httphi.MethHead with a small change.