Go 中的「http:寫入超過聲明的 Content-Length」錯誤
php小編新一今天將為大家介紹一個常見的錯誤,也就是在Go語言中出現的「http:寫入超過宣告的Content-Length」錯誤。當我們在使用Go語言來寫HTTP請求時,有時會遇到這個錯誤。這個錯誤的原因是我們在請求頭中聲明的Content-Length與實際發送的請求體長度不一致。接下來,我們將詳細解釋這個錯誤的產生原因以及如何解決它。
問題內容
我正在嘗試 Go 並編寫了一個應用程式來管理由工作協程處理的 HTTP 請求佇列。
並發的東西似乎工作正常,但在發送回 http 回應時出現此錯誤:寫入的內容超過聲明的 Content-Length
。
完整程式碼如下:
package main import ( "log" "net/http" "sync" ) // Job represents a unit of work to be processed by a worker. type Job struct { r *http.Request // HTTP request to be processed w http.ResponseWriter // Response writer to send the result } // Queue manages a list of jobs to be processed by workers. type Queue struct { jobs []*Job // List of jobs in the queue mu sync.Mutex // Mutex to synchronize access to the queue cond *sync.Cond // Condition variable for signaling } var q Queue // Global instance of the queue var WORKER_POOL_SIZE = 4 // Number of workers // Push adds a job to the queue. func (q *Queue) Push(j *Job) { q.mu.Lock() defer q.mu.Unlock() q.jobs = append(q.jobs, j) q.cond.Signal() // Signal a waiting worker that a job is available log.Println("Job added to queue") } // Pop retrieves and removes a job from the queue. func (q *Queue) Pop() (*Job, bool) { q.mu.Lock() defer q.mu.Unlock() if len(q.jobs) == 0 { q.cond.Wait() // If the queue is empty, wait for a signal } job := q.jobs[0] q.jobs = q.jobs[1:] log.Println("Job removed from queue") return job, true } // handler adds a job to the queue. func handler(w http.ResponseWriter, r *http.Request) { // Create a job with the request and response. job := &Job{r, w} // Push the job onto the queue. q.Push(job) log.Println("Received request and added job to queue") } // init initializes the condition variable and starts worker goroutines. func init() { q.cond = sync.NewCond(&q.mu) for i := 0; i < WORKER_POOL_SIZE; i++ { go worker() } } // worker processes jobs from the queue. func worker() { for { job, ok := q.Pop() if ok { log.Println("Worker processing job") doWork(job) } } } // doWork simulates processing a job and sends a response. func doWork(job *Job) { // Extract the "Name" parameter from the request query. name := job.r.URL.Query().Get("Name") // Check if the name is not empty. if name != "" { // Send the name as the response. _, err := job.w.Write([]byte("Hello, " + name)) if err != nil { log.Println("Error writing response:", err) } log.Println("Response sent: Hello,", name) } else { // If the "Name" parameter is missing or empty, send an error response. http.Error(job.w, "Name parameter is missing or empty", http.StatusBadRequest) log.Println("Error: Name parameter is missing or empty") } } func main() { http.HandleFunc("/addJob", handler) log.Println("Server started and listening on :8080") err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("Error starting server:", err) } }
知道如何解決這個問題嗎?另外,由於我對 go 中的並發性很陌生,你認為它可以改進嗎?謝謝!
解決方法
根據 http.Handler
文件:
您的 handler
正在將請求/編寫器推送到佇列中,然後返回。這表示您在處理程序返回後嘗試寫入 ResponseWriter
,違反了上述規定(由於沒有同步,寫入也可能實際發生在 return
之前或同時發生)。
有很多方法可以解決這個問題;一種技術是:
// Job represents a unit of work to be processed by a worker. type Job struct { r *http.Request // HTTP request to be processed w http.ResponseWriter // Response writer to send the result done chan struct{} // Closed when write competed } ... func handler(w http.ResponseWriter, r *http.Request) { // Create a job with the request and response. job := &Job{r, w, make(chan struct{})} // Push the job onto the queue. q.Push(job) log.Println("Received request and added job to queue") <-job.done // Wait until job has been processed } ... // worker processes jobs from the queue. func worker() { for { job, ok := q.Pop() if ok { log.Println("Worker processing job") doWork(job) close(job.done) // Notify requester that we are done } } }
這實際上取決於要求。一個常見的解決方案是僅使用通道(處理程序將請求發送到通道,多個工作人員從同一通道接收)。
以上是Go 中的「http:寫入超過聲明的 Content-Length」錯誤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Go語言中用於浮點數運算的庫介紹在Go語言(也稱為Golang)中,進行浮點數的加減乘除運算時,如何確保精度是�...

Go爬蟲Colly中的Queue線程問題探討在使用Go語言的Colly爬蟲庫時,開發者常常會遇到關於線程和請求隊列的問題。 �...

Go語言中字符串打印的區別:使用Println與string()函數的效果差異在Go...

Go語言中使用RedisStream實現消息隊列時類型轉換問題在使用Go語言與Redis...

GoLand中自定義結構體標籤不顯示怎麼辦?在使用GoLand進行Go語言開發時,很多開發者會遇到自定義結構體標籤在�...

Go語言中結構體定義的兩種方式:var與type關鍵字的差異Go語言在定義結構體時,經常會看到兩種不同的寫法:一�...

Go語言中哪些庫是大公司開發或知名開源項目?在使用Go語言進行編程時,開發者常常會遇到一些常見的需求,�...

Go編程中的資源管理:Mysql和Redis的連接與釋放在學習Go編程過程中,如何正確管理資源,特別是與數據庫和緩存�...
