总结
豆包 AI 助手文章总结
首页 > 后端开发 > Golang > 正文

Go 中的“http:写入超过声明的 Content-Length”错误

WBOY
发布: 2024-02-09 12:50:07
转载
766人浏览过

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 
<p>知道如何解决这个问题吗?另外,由于我对 go 中的并发性很陌生,你认为它可以改进吗?谢谢!</p><h2 class="daan">解决方法</h2><p>根据 <a href="https://www.php.cn/link/79f56e5e3e0e999b3c139f225838d41f" rel="noreferrer">http.Handler 文档</a>:</p>

<p>您的 handler 正在将请求/编写器推送到队列中,然后返回。这意味着您在处理程序返回后尝试写入 ResponseWriter ,违反了上述规定(由于没有同步,写入也可能实际发生在 return 之前或同时发生)。</p>
<p>有很多方法可以解决这个问题;一种技术是:</p>
<pre class="brush:php;toolbar:false;">// 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 := &amp;Job{r, w, make(chan struct{})}

    // Push the job onto the queue.
    q.Push(job)
    log.Println("Received request and added job to queue")
    

<p>这实际上取决于要求。一个常见的解决方案是仅使用通道(处理程序将请求发送到通道,多个工作人员从同一通道接收)。</p>
登录后复制

以上就是Go 中的“http:写入超过声明的 Content-Length”错误的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:stackoverflow网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号