-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
refactor: abusing goroutines and channel #1561
Conversation
zijiren233
commented
Jun 26, 2024
可以写明一下具体更改了什么以及解决了什么问题吗。 |
重写没必要使用goroutine和channel的地方,从而提高性能 |
之前的写法确实问题很大,但是这次改动这么大,最好能提供更多模型的测试案例,而且响应最好能稍微长一点,至少要超过一次 stream event 的长度。 |
@zijiren233 参照 CI 改一下 commit 的格式吧。 |
已修复commit message |
common/utils.go
Outdated
@@ -18,6 +18,7 @@ func LogQuota(quota int64) string { | |||
} | |||
|
|||
func RenderStringData(c *gin.Context, data string) { | |||
data = strings.TrimPrefix(data, "data: ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是哪里需要了呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
openai里面,是直接把获取到的string发送过去,这时候开头会有两个data:
str = strings.TrimPrefix(str, "data: ") | ||
str = strings.TrimSuffix(str, "\r") | ||
c.Render(-1, common.CustomEvent{Data: "data: " + str}) | ||
c.Writer.Flush() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里加上 flush 后有什么变化吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SSE必须加flush,否则即使往conn里面写数据,客户端也不会立即获取到数据
可以看看c.Stream实现,其实就是简单的包装了下
我把 render 相关的抽到 render 包下了,除此之外没有其他改动 |
* refactor: abusing goroutines * fix: trim data prefix * refactor: move functions to render package * refactor: add back trim & flush --------- Co-authored-by: JustSong <quanpengsong@gmail.com>
* refactor: abusing goroutines * fix: trim data prefix * refactor: move functions to render package * refactor: add back trim & flush --------- Co-authored-by: JustSong <quanpengsong@gmail.com>