|
| 1 | +# In-Memory HTTP Transport |
| 2 | + |
| 3 | +_note: written as if it will be split into its own package_ |
| 4 | + |
| 5 | +A high-performance, zero-network-overhead HTTP transport implementation that bypasses the network layer entirely by calling handlers directly using lazy evaluation. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +The `inmemory` package provides an `http.RoundTripper` implementation that directly invokes HTTP handlers in memory, eliminating all network serialization, parsing, and connection overhead. This is ideal for embedded http services or testing and development environments. |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +```go |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "io" |
| 19 | + "net/http" |
| 20 | + |
| 21 | + "github.com/authzed/spicedb-kubeapi-proxy/pkg/inmemory" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + // Create your HTTP handler |
| 26 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 27 | + w.Header().Set("Content-Type", "application/json") |
| 28 | + w.WriteHeader(http.StatusOK) |
| 29 | + w.Write([]byte(`{"message": "Hello, World!"}`)) |
| 30 | + }) |
| 31 | + |
| 32 | + // Create an HTTP client with in-memory transport |
| 33 | + client := inmemory.NewClient(handler) |
| 34 | + |
| 35 | + // Make requests - no network involved! |
| 36 | + resp, err := client.Get("http://api.example.com/hello") |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + defer resp.Body.Close() |
| 41 | + |
| 42 | + // Headers and status are available after reading the body |
| 43 | + io.Copy(io.Discard, resp.Body) // Triggers handler execution |
| 44 | + fmt.Printf("Status: %d\n", resp.StatusCode) |
| 45 | + fmt.Printf("Content-Type: %s\n", resp.Header.Get("Content-Type")) |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## API Reference |
| 50 | + |
| 51 | +### `New(handler http.Handler) *Transport` |
| 52 | + |
| 53 | +Creates a new in-memory transport that will invoke the provided handler directly. |
| 54 | + |
| 55 | +```go |
| 56 | +transport := inmemory.New(myHandler) |
| 57 | +client := &http.Client{Transport: transport} |
| 58 | +``` |
| 59 | + |
| 60 | +### `NewClient(handler http.Handler) *http.Client` |
| 61 | + |
| 62 | +Convenience function that creates an HTTP client with an in-memory transport. |
| 63 | + |
| 64 | +```go |
| 65 | +client := inmemory.NewClient(myHandler) |
| 66 | +``` |
| 67 | + |
| 68 | +### Lazy Execution |
| 69 | + |
| 70 | +The transport uses lazy evaluation - the handler is executed only when the response body is read: |
| 71 | + |
| 72 | +```go |
| 73 | +resp, _ := client.Get("http://example.com/") |
| 74 | +// Handler not executed yet, headers/status not available |
| 75 | + |
| 76 | +// Reading body triggers execution |
| 77 | +body, _ := io.ReadAll(resp.Body) |
| 78 | +// Now headers and status are available |
| 79 | + |
| 80 | +// Or manually trigger execution without reading body |
| 81 | +if lazyBody, ok := resp.Body.(*inmemory.LazyResponseBody); ok { |
| 82 | + lazyBody.TriggerExecution() // Execute handler without reading body |
| 83 | + // Headers and status now available |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +## Examples |
| 88 | + |
| 89 | +### Basic Usage |
| 90 | + |
| 91 | +```go |
| 92 | +handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 93 | + w.WriteHeader(http.StatusOK) |
| 94 | + w.Write([]byte("Hello!")) |
| 95 | +}) |
| 96 | + |
| 97 | +client := inmemory.NewClient(handler) |
| 98 | +resp, _ := client.Get("http://example.com/") |
| 99 | +io.ReadAll(resp.Body) // Triggers execution |
| 100 | +// Response contains "Hello!" with status 200 |
| 101 | +``` |
| 102 | + |
| 103 | +### With Request Bodies |
| 104 | + |
| 105 | +```go |
| 106 | +handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 107 | + body, _ := io.ReadAll(r.Body) |
| 108 | + w.Write([]byte(fmt.Sprintf("Echo: %s", body))) |
| 109 | +}) |
| 110 | + |
| 111 | +client := inmemory.NewClient(handler) |
| 112 | +resp, _ := client.Post("http://example.com/echo", "text/plain", |
| 113 | + strings.NewReader("test data")) |
| 114 | +io.ReadAll(resp.Body) // Triggers execution |
| 115 | +// Response contains "Echo: test data" |
| 116 | +``` |
| 117 | + |
| 118 | +### Complex Handler |
| 119 | + |
| 120 | +```go |
| 121 | +mux := http.NewServeMux() |
| 122 | +mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { |
| 123 | + w.WriteHeader(http.StatusOK) |
| 124 | + w.Write([]byte("healthy")) |
| 125 | +}) |
| 126 | +mux.HandleFunc("/api/users", func(w http.ResponseWriter, r *http.Request) { |
| 127 | + w.Header().Set("Content-Type", "application/json") |
| 128 | + w.Write([]byte(`[{"id": 1, "name": "Alice"}]`)) |
| 129 | +}) |
| 130 | + |
| 131 | +client := inmemory.NewClient(mux) |
| 132 | + |
| 133 | +// Both endpoints work normally |
| 134 | +healthResp, _ := client.Get("http://api.com/health") |
| 135 | +usersResp, _ := client.Get("http://api.com/api/users") |
| 136 | + |
| 137 | +// Read responses to trigger execution |
| 138 | +io.ReadAll(healthResp.Body) |
| 139 | +io.ReadAll(usersResp.Body) |
| 140 | +``` |
0 commit comments