登录
首页 >  Golang >  Go问答

Golang http:优雅关闭服务的通道

来源:stackoverflow

时间:2024-02-25 14:09:25 490浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《Golang http:优雅关闭服务的通道》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

尝试清理我的代码,getdecislisteners() 中的代码以前位于 livedecision() 内,没有出现紧急错误。

//getdecislistener return listener subscribed to channels
func getdecislistener(dstructs map[string]map[string]interface{}) (chan interface{}, error) {
    listener := make(chan interface{})
    for r, _ := range dstructs {
        decischannels[r].register(listener)
        key := r
        defer func() {
            decision(key).unregister(listener)
            close(listener)
        }()
        fmt.printf("done %v\n", r)
    }
    fmt.print("done inside\n")
    return listener, nil
}
func livedecision(ctx *gin.context) {
    ...
    listener, err := getdecislistener(dstructs)
    fmt.print("done outside\n")    // cannot reach here
    if err != nil {return}

    ctx.stream(func(w io.writer) bool {
        select {
        case msg := <-listener:
            return true
            ...
    }}
}

dstructs只是一个2d界面图; decisiondecischannels 是从这里引用的。

var decischannels = make(map[string]broadcast.broadcaster)
var deciscols = make(map[string]string)

func openlistener(decisid string) chan interface{} {
    ite := make(chan interface{})
    decision(decisid).register(ite)
    return ite
}

func closelistener(decisid string, ite chan interface{}) {
    decision(decisid).unregister(ite)
    close(ite)
}

func decision(decisid string) broadcast.broadcaster {
    b, ok := decischannels[decisid]
    if !ok {
        b = broadcast.newbroadcaster(10)
        decischannels[decisid] = b
    }
    return b
}

日志显示它尚未到达 之外的 done,但在 getdecislistener() 之后显示 http:panic

Done item1
Done item2
Done item3
Done item4
Done inside
2021/09/14 14:50:45 http: panic serving 127.0.0.1:39450: close of closed channel
goroutine 40 [running]:
net/http.(*conn).serve.func1(0xc0000c4280)
        /usr/lib/go-1.13/src/net/http/server.go:1767 +0x139
panic(0xd271c0, 0xf5c2b0)
        /usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
somewhere/internal/app/route/client.GetDecisListener.func1(0xc0001d8290, 0xe, 0xc0003660c0)
        elsewhere/internal/app/route/client/client.go:42 +0x68
panic(0xd271c0, 0xf5c2b0)
        /usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
somewhere/internal/app/route/client.GetDecisListener.func1(0xc0001d8300, 0xd, 0xc0003660c0)
        elsewhere/internal/app/route/client/client.go:42 +0x68
panic(0xd271c0, 0xf5c2b0)
        /usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
somewhere/internal/app/route/client.GetDecisListener.func1(0xc0001d8390, 0xd, 0xc0003660c0)
        elsewhere/internal/app/route/client/client.go:42 +0x68
somewhere/internal/app/route/client.GetDecisListener(0xc0001228a0, 0xc0003660c0, 0x0, 0x0)
        elsewhere/internal/app/route/client/client.go:47 +0x27f
somewhere/internal/app/route/client.LiveDecision(0xc0000f8200)
        elsewhere/internal/app/route/client/client.go:77 +0xb0
github.com/gin-gonic/gin.(*Context).Next(0xc0000f8200)
        /home/simon/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:165 +0x3b
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc000470000, 0xc0000f8200)
        /home/simon/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:489 +0x614
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc000470000, 0xf7cdc0, 0xc0001020e0, 0xc0000f8100)
        /home/simon/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:445 +0x15d
net/http.serverHandler.ServeHTTP(0xc00046e0e0, 0xf7cdc0, 0xc0001020e0, 0xc0000f8100)
        /usr/lib/go-1.13/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000c4280, 0xf7ff00, 0xc00004a040)
        /usr/lib/go-1.13/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
        /usr/lib/go-1.13/src/net/http/server.go:2928 +0x384

我怀疑 decision(key)defer func() 导致了恐慌。如果是这种情况,我不知道该把它放在哪里。


正确答案


您不能两次关闭通道:它会出现恐慌。

listener := make(chan interface{})
for r, _ := range dstructs {
    decischannels[r].register(listener)
    key := r
    defer func() {
        decision(key).unregister(listener)
        close(listener)
    }()
    fmt.printf("done %v\n", r)
}

这里如果len(dstructs) > 1,两个延迟调用会尝试关闭listener

也许你可以这样做:

listener := make(chan interface{})
shouldClose := false
for r, _ := range dStructs {
    DecisChannels[r].Register(listener)
    key := r
    defer func() {
        Decision(key).Unregister(listener)
    }()
    shouldClose = true
    fmt.Printf("Done %v\n", r)
}
if shouldClose {
    close(listener)    
}

本篇关于《Golang http:优雅关闭服务的通道》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>