go:stdlib:http_server_struct
Differences
This shows you the differences between two versions of the page.
| go:stdlib:http_server_struct [2025/12/29 23:33] – created phong2018 | go:stdlib:http_server_struct [2025/12/29 23:35] (current) – phong2018 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ==== What is it? ==== | ==== What is it? ==== | ||
| - | `http.Server` is a configurable HTTP server type in Go’s `net/http` package. | + | `http.Server` is the configurable HTTP server type in Go’s `net/http` package. |
| - | Compared to `http.ListenAndServe()`, it gives you more control: | + | It gives you more control than `http.ListenAndServe()` |
| ==== What is it used for? ==== | ==== What is it used for? ==== | ||
| - | * Run an HTTP server with production-friendly | + | * Run an HTTP server with production-ready settings (timeouts). |
| * Support graceful shutdown. | * Support graceful shutdown. | ||
| * Serve HTTPS (TLS). | * Serve HTTPS (TLS). | ||
| - | * Customize | + | * Customize handler, error logger, max header |
| ==== Key fields (common) ==== | ==== Key fields (common) ==== | ||
| * `Addr`: address to listen on (e.g. `": | * `Addr`: address to listen on (e.g. `": | ||
| - | * `Handler`: | + | * `Handler`: |
| - | * `ReadHeaderTimeout`: | + | * `ReadHeaderTimeout`: |
| - | * `ReadTimeout`: | + | * `ReadTimeout`: |
| - | * `WriteTimeout`: | + | * `WriteTimeout`: |
| * `IdleTimeout`: | * `IdleTimeout`: | ||
| - | * `MaxHeaderBytes`: maximum size of request headers | + | * `ErrorLog`: custom logger |
| - | * `ErrorLog`: custom logger for server errors | + | * `MaxHeaderBytes`: limit request header size |
| - | ==== Example: server with timeouts | + | ==== Example: server with timeouts ==== |
| <code go> | <code go> | ||
| package main | package main | ||
| Line 49: | Line 49: | ||
| WriteTimeout: | WriteTimeout: | ||
| IdleTimeout: | IdleTimeout: | ||
| - | MaxHeaderBytes: | ||
| } | } | ||
| - | // Start server | + | // Start server |
| go func() { | go func() { | ||
| log.Println(" | log.Println(" | ||
| - | err := srv.ListenAndServe() | + | |
| - | if err != nil && err != http.ErrServerClosed { | + | |
| log.Fatal(err) | log.Fatal(err) | ||
| } | } | ||
| }() | }() | ||
| - | // Wait for termination signal | + | // Graceful shutdown on SIGINT/ |
| stop := make(chan os.Signal, 1) | stop := make(chan os.Signal, 1) | ||
| signal.Notify(stop, | signal.Notify(stop, | ||
| <-stop | <-stop | ||
| - | // Graceful shutdown | ||
| ctx, cancel := context.WithTimeout(context.Background(), | ctx, cancel := context.WithTimeout(context.Background(), | ||
| defer cancel() | defer cancel() | ||
| Line 72: | Line 69: | ||
| log.Println(" | log.Println(" | ||
| _ = srv.Shutdown(ctx) | _ = srv.Shutdown(ctx) | ||
| - | log.Println(" | ||
| } | } | ||
| </ | </ | ||
| ==== Notes / pitfalls ==== | ==== Notes / pitfalls ==== | ||
| - | * `ListenAndServe()` blocks, so run it in a goroutine if you need shutdown | + | * `ListenAndServe()` blocks; use a goroutine if you also need to handle |
| - | * Always set timeouts in production to reduce | + | * Always set timeouts in production to reduce |
| - | * `Shutdown(ctx)` | + | * Use `Shutdown(ctx)` for graceful shutdown; use `Close()` |
| - | * `Close()` | + | |
| - | * If you use `nil` Handler, you’re using the global `http.DefaultServeMux`. | + | |
| - | ==== Related | + | ==== Related ==== |
| - | * [[go: | + | * [[go: |
| - | * [[go: | + | * [[go: |
| - | * [[go: | + | |
| * [[go: | * [[go: | ||
| Line 94: | Line 87: | ||
| * **graceful shutdown** / | * **graceful shutdown** / | ||
| * **TLS** /ˌtiː el ˈes/: bảo mật truyền tải (HTTPS) | * **TLS** /ˌtiː el ˈes/: bảo mật truyền tải (HTTPS) | ||
| - | * **limit** /ˈlɪmɪt/: giới hạn | + | * **keep-alive** /ˌkiːp əˈlaɪv/: giữ kết nối |
| - | * **in-flight request** /ɪn flaɪt rɪˈkwest/: | + | * **slowloris** /ˈsloʊˌlɔːrɪs/: kiểu |
| - | * **deadline** / | + | * **signal** /ˈsɪɡnəl/: tín hiệu hệ điều hành |
| - | * **drop** /drɑːp/: cắt (ngắt) | + | |
| - | * **attack** /əˈtæk/: tấn công | + | |
| - | * **goroutine** /ˈɡoʊruːˌtiːn/: luồng nhẹ của Go | + | |
go/stdlib/http_server_struct.txt · Last modified: by phong2018
