gpt4 book ai didi

go - 使用 gorutine (golang) 的无效内存地址

转载 作者:IT王子 更新时间:2023-10-29 02:21:58 27 4
gpt4 key购买 nike

我正在使用的代码只有在我将其作为阻塞例程运行时才有效...但是如果我使用 go executeTheFunction我总是得到 panic: runtime error: invalid memory address or nil pointer dereference .稍微总结一下代码就是这样。

func (c App) Index() revel.Result {
app = c; //this is a "global" variable to be used in callBackFunction
options = "foo";
client = NewClient()
client.RequestAsynch(options, callBackFunction);
}
func callBackFunction(message string){
reader := bytes.NewBufferString(message)
_, err := xmlpath.Parse( reader )
if err != nil {
panic(err)
}
flusher, ok := airshopping.Response.Out.(http.Flusher)
if !ok {
panic("expected http.ResponseWriter to be an http.Flusher")
}

fmt.Fprintf(airshopping.Response.Out, "whatever")
flusher.Flush()
}

func (client * Client)RequestAsynch(message Message, callback func(string)) {
Response := client.Request(message)

message_aux := ""
reader := bufio.NewReader(Response.Body)
for {
line, err := reader.ReadBytes('\n')
if err==nil {
message_aux = message_aux + string(line)
if strings.Contains(message_aux, "<!-- AG-EOM -->"){
go callback(message_aux)
message_aux = ""
}
}else{fmt.Println("ERROR", err);break;}
}
}

好的...我要解释一下。 RequestAsynch在不同的包中,它向其他服务器发起请求。此其他服务器响应多个 xml...这些 xml 的主体如下所示:

<!-- Header status:ok -->
<all_the_xml_response>
<!-- AG-EOM -->

所以...当逐行循环检测到这个 "<!-- AG-EOM -->" 时是其中一个 xml 已完全接收,并且应该执行回调函数(使用 gorutine)。这个callBack函数的任务是解析xml,经过解析后,将一些数据返回给客户端的javascript。

我经常遇到的错误是:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x20 pc=0x4f8c09]

goroutine 80 [running]:
bufio.(*Writer).Write(0xc042454540, 0xc0422d4000, 0x11076, 0x12000, 0xc04202fb30, 0x4683b7, 0x11076)
C:/Go/src/bufio/bufio.go:598 +0x149
net/http.(*response).write(0xc04234d0a0, 0x11076, 0xc0422d4000, 0x11076, 0x12000, 0x0, 0x0, 0xc0422d4000, 0x0, 0x0)
C:/Go/src/net/http/server.go:1525 +0x157
net/http.(*response).Write(0xc04234d0a0, 0xc0422d4000, 0x11076, 0x12000, 0x0, 0x0, 0xc042824000)
C:/Go/src/net/http/server.go:1495 +0x6b
fmt.Fprintf(0xb65e80, 0xc04234d0a0, 0xc042824000, 0x11076, 0x0, 0x0, 0x0, 0xc0421445f0, 0xb, 0xc0422f5e00)
C:/Go/src/fmt/print.go:182 +0xb0
ndc-console-test/app/controllers/ndcmethods.GetOffers_AS(0xc042594000, 0xef7f)
F:/Proyectos_Trabajo/work/src/ndc-console- test/app/controllers/ndcmethods/airshopping.go:98 +0x7a1
created by github.com/open-ndc/ndc-go-sdk.(*Client).RequestAsynch
F:/Proyectos_Trabajo/work/src/github.com/open-ndc/ndc-go-sdk/ndc_client.go:143 +0x25c
2017/05/17 16:42:56 reverseproxy.go:316: http: proxy error: dial tcp [::1]:55058: connectex:A connection can not be established because the target computer expressly denied the connection.

在这个错误之后...我需要重新启动服务器,因为它不允许建立新连接。

我看到的一件事是,如果我删除此行 _, err := xmlpath.Parse( reader )在 callBackFunction 中它工作正常。当然,如果我更改此行:go callback(message_aux)对于 callback(message_aux) (没有 goroutine)它也工作正常并且解析 xml 没有问题。

有谁知道如何使用 goroutines 避免此错误的解决方案?

非常感谢。

编辑

我想我没有很好地表达自己对其他服务器的响应方式,这是(多个)响应的样子:

<!-- Header status:ok -->
<the_xml_response_of_one_response_or_one_chunk>
<!-- AG-EOM -->
<!-- Header status:ok -->
<other_xml_response>
<!-- AG-EOM -->
<!-- Header status:ok -->
<other_more_xml_response>
<!-- AG-EOM -->

最佳答案

根据您的堆栈跟踪,问题似乎是在父函数返回后立即写出 HTTP 响应,当新的 goroutine 有机会尝试时,编写器不再可用。

这是 net/http 设计的一部分,因此我怀疑您的 RequestAsynch 无法按预期编写。为什么需要异步?每个请求都已在其自己的 goroutine 中处理。

顺便说一句,strings.NewReader 可能比 bytes.NewBufferString 更合适,因为您不需要完整的缓冲区接口(interface)。

关于go - 使用 gorutine (golang) 的无效内存地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44028438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com