Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/ghttp: stream流式输出可能存在bug #3584

Open
lcm1475 opened this issue May 16, 2024 · 6 comments
Open

os/ghttp: stream流式输出可能存在bug #3584

lcm1475 opened this issue May 16, 2024 · 6 comments
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.

Comments

@lcm1475
Copy link

lcm1475 commented May 16, 2024

Go version

go version go1.21.5 darwin/arm64

GoFrame version

v2.6.1

Can this bug be reproduced with the latest release?

Option Yes

What did you do?

gf的流式输出在移动端会断开把一次的数据截断成2次发送,同样的代码放到gin就没问题
而且这个情况web看不出来,打印输出到web页面上是没问题的,移动端才能复现这个问题,Android,ios,Flutter,这些移动端必现,但是Android,ios,Flutter这些接入官方的openai,bing,文心,或者gin都是正常的,只有gf会断开,
把数据缩短好像断开的情况就很少了,但是实际业务中没办法缩短数据,一串json不可能分开发送
a3346610e45fc4df8ccc102090602e40
05a6970542c3396007cac39c59bfb9c5

What did you see happen?

数据会有断开的情况

What did you expect to see?

希望不要断开

@lcm1475 lcm1475 added the bug It is confirmed a bug, but don't worry, we'll handle it. label May 16, 2024
@lcm1475
Copy link
Author

lcm1475 commented May 16, 2024

func (p *TestController) GetStreamTestV2(r *ghttp.Request) {
r.Response.Header().Set("Content-Type", "text/event-stream")
r.Response.Header().Set("Transfer-Encoding", "chunked")
r.Response.Header().Set("Cache-Control", "no-cache")
r.Response.Header().Set("Connection", "keep-alive")
r.Response.Header().Set("X-Accel-Buffering", "no")

for i := 0; i < 10; i++ {
	time.Sleep(500 * time.Millisecond)
	r.Response.Write("{\"choices\":[{\"message\":{\"content\":\"我是一条示例的数据\"}}],\"req_id\":\"202405161153160000621695B542BC2D9E\"}")
	r.Response.Flush()
}

return

}

@lcm1475
Copy link
Author

lcm1475 commented May 16, 2024

下面是gin的例子 同样输出的内容 gin不会断开

`func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
atomic.AddInt32(&GlobalVarDemo, 1)
c.JSON(200, gin.H{
"gggg": GlobalVarDemo,
})
})

r.GET("/testStream", func(ctx *gin.Context) {
	w := ctx.Writer
	ctx.Writer.Header().Set("Content-Type", "text/event-stream")
	ctx.Writer.Header().Set("Transfer-Encoding", "chunked")
	ctx.Writer.Header().Set("Cache-Control", "no-cache")
	ctx.Writer.Header().Set("Connection", "keep-alive")
	ctx.Writer.Header().Set("X-Accel-Buffering", "no")
	for i := 0; i < 10; i++ {
		time.Sleep(500 * time.Millisecond)
		_, _ = fmt.Fprint(w, "data: %s\n\n", "{\"choices\":[{\"message\":{\"content\":\"我是一条示例的数据\"}}],\"req_id\":\"202405161153160000621695B542BC2D9E\"}")
		w.Flush()
	}
})
http.ListenAndServe(":8081", r)
//r.Run()

}`

@hamster1963
Copy link

// 发送数据 request.Response.Writefln("data: " + gjson.New(sseData).String() + "\n") request.Response.Flush()
试试用Writefln,在使用中还没有出现过截断

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


//Send data request.Response.Writefln("data: " + gjson.New(sseData).String() + "\n") request.Response.Flush()
Try using Writefln, no truncation has occurred during use.

@lcm1475
Copy link
Author

lcm1475 commented May 29, 2024

// 发送数据 request.Response.Writefln("data: " + gjson.New(sseData).String() + "\n") request.Response.Flush() 试试用Writefln,在使用中还没有出现过截断

试过了 不行 截断的情况只有在移动端才能复现

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


// Send data request.Response.Writefln("data: " + gjson.New(sseData).String() + "\n") request.Response.Flush() Try using Writefln, it is not in use yet Truncation occurred

I tried it, but it didn’t work. The truncation situation can only be reproduced on the mobile terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug It is confirmed a bug, but don't worry, we'll handle it.
Projects
None yet
Development

No branches or pull requests

3 participants