无法同时响应多个请求

发布于 2024-11-02 07:48:03 字数 1470 浏览 3 评论 0原文

我正在尝试将我的 Node.js HTTP 服务器转换为 Go。这就是我想要发生的情况:

我有一个间歇性生成的资源(例如每秒左右),并且我希望对该资源的所有请求都等到下次生成时为止。这样客户端就可以轮询并保证只获得最新的资源。我正在使用 web.go 来消除运行 HTTP 服务器的许多复杂性。

这是我的代码的简短版本:

package main

import (
    "time"
    "web"
    "fmt"
    vector "container/vector"
)

var listeners vector.Vector;

func resource (ctx *web.Context) {
    c := make(chan int)
    listeners.Push(c)
    go func() {
        <-c
        go func() {
            ctx.WriteString("Cool")
            fmt.Println("Wrote something")
        }()
    }()
}

func resourceLoop() {
    time.Sleep(5 * 1000 * 1000000) // sleep for 5 seconds
    for ; listeners.Len() > 0 ; {
        c := listeners.Pop().(chan int)
        c <- 1
    }

    go resourceLoop()
}

func main() {
    web.Get("/resource", resource)

    go resourceLoop()

    web.Run("localhost:4000")
}

我希望有一个 Context.End() 或类似的函数,但它似乎没有。我阅读了 web.go 的源代码,但我无法弄清楚它在哪里结束响应(web.go:268 是调用我的resource() 的地方)。在 Node.js 中,这很简单,您可以调用 ServerResponse.end()。

当我在 Chrome 中运行脚本时终止服务器时,我得到以下输出(似乎是正确的,除了响应没有结束):

4
Cool
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Sat, 16 Apr 2011 00:37:58 GMT
Server: web.go
Transfer-Encoding: chunked

0

这是 web.go 框架的问题还是我做错了什么?如果框架有问题,我会向他提出问题。

我对 Go 还很陌生,所以我的做法可能完全错误。

I am trying to convert my node.js HTTP server to Go. Here's what I want to happen:

I have a resource that gets generated intermittently (say every second or so), and I want all requests for this resource to wait until the next time it is generated. This way clients can poll and be guaranteed to get only the up-to-date resource. I am using web.go to remove a lot of the complexity of running an HTTP server.

Here is a brief version of my code:

package main

import (
    "time"
    "web"
    "fmt"
    vector "container/vector"
)

var listeners vector.Vector;

func resource (ctx *web.Context) {
    c := make(chan int)
    listeners.Push(c)
    go func() {
        <-c
        go func() {
            ctx.WriteString("Cool")
            fmt.Println("Wrote something")
        }()
    }()
}

func resourceLoop() {
    time.Sleep(5 * 1000 * 1000000) // sleep for 5 seconds
    for ; listeners.Len() > 0 ; {
        c := listeners.Pop().(chan int)
        c <- 1
    }

    go resourceLoop()
}

func main() {
    web.Get("/resource", resource)

    go resourceLoop()

    web.Run("localhost:4000")
}

I would expect there to be a Context.End() or similar function, but it doesn't seem to have one. I read the source for web.go, but I couldn't figure out where it was ending the response (web.go:268 is where my resource() is called). In node.js this is trivial, you can call a ServerResponse.end().

When I kill the server while running the script in Chrome, I get this output (seems to be correct, except that the response isn't ending):

4
Cool
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Date: Sat, 16 Apr 2011 00:37:58 GMT
Server: web.go
Transfer-Encoding: chunked

0

Is this a problem with the web.go framework or am I doing something wrong? If it's a problem with the framework, I'll file an issue with him.

I'm pretty new to Go, so I could be going about this completely wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2024-11-09 07:48:03

我从未使用过 web.go,但您的示例似乎太复杂了。为什么需要一个 goroutine 来生成一个 goroutine?我假设框架本身会处理并发性,然后这样写:

func resource (ctx *web.Context, val string) string {
    c := make(chan int)
    listeners.Push(c)
    <-c
    return "Cool"
}

否则,看起来它正在做你想要的事情,如果你真的完成了它,你只需要关闭连接:

ctx.Close()

I've never used web.go, but it seems that your example is entirely too complicated. Why do you need a goroutine to spawn a goroutine? I would assume the framework itself would take care of concurrency and just write this:

func resource (ctx *web.Context, val string) string {
    c := make(chan int)
    listeners.Push(c)
    <-c
    return "Cool"
}

Otherwise, it looks like it is doing exactly what you want and you just need to close the connection if you're truly done with it:

ctx.Close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文