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

refactor: abusing goroutines and channel #1561

Merged
merged 5 commits into from
Jun 30, 2024

Conversation

zijiren233
Copy link
Collaborator

image

@zijiren233 zijiren233 changed the title Refector: abusing goroutines Refector: abusing goroutines and channel Jun 26, 2024
@MartialBE
Copy link
Collaborator

可以写明一下具体更改了什么以及解决了什么问题吗。

@zijiren233
Copy link
Collaborator Author

重写没必要使用goroutine和channel的地方,从而提高性能

@zijiren233 zijiren233 changed the title Refector: abusing goroutines and channel Refactor: abusing goroutines and channel Jun 26, 2024
@Laisky
Copy link
Collaborator

Laisky commented Jun 27, 2024

之前的写法确实问题很大,但是这次改动这么大,最好能提供更多模型的测试案例,而且响应最好能稍微长一点,至少要超过一次 stream event 的长度。

@zijiren233
Copy link
Collaborator Author

zijiren233 commented Jun 27, 2024

这是SSE请求界面,一个接收消息就是一个event
目前测试了google, openai, cloudflare, 百度,腾讯,阿里
image
image
image
image
image
image
image

@Laisky
Copy link
Collaborator

Laisky commented Jun 27, 2024

@zijiren233 参照 CI 改一下 commit 的格式吧。
另外那个 coverage 挂了,得等 owner 来修。

@zijiren233
Copy link
Collaborator Author

已修复commit message

@songquanpeng songquanpeng changed the title Refactor: abusing goroutines and channel refactor: abusing goroutines and channel Jun 30, 2024
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: ")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是哪里需要了呢?

Copy link
Collaborator Author

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()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里加上 flush 后有什么变化吗?

Copy link
Collaborator Author

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实现,其实就是简单的包装了下

@songquanpeng songquanpeng merged commit b21b3b5 into songquanpeng:main Jun 30, 2024
3 checks passed
@songquanpeng
Copy link
Owner

我把 render 相关的抽到 render 包下了,除此之外没有其他改动

jinjianming pushed a commit to jinjianming/one-api that referenced this pull request Jul 17, 2024
* 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>
mxdlzg pushed a commit to mxdlzg/one-api that referenced this pull request Oct 15, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants