Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e7a4aa05
authored
Jun 27, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 支持设置流模式超时时间
parent
4b45e550
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
1 deletions
+31
-1
README.md
+2
-0
common/go-channel.go
+19
-0
constant/system-setting.go
+4
-0
relay/channel/openai/relay-openai.go
+6
-1
No files found.
README.md
View file @
e7a4aa05
...
...
@@ -83,6 +83,8 @@
```
可以实现400错误转为500错误,从而重试
## 比原版One API多出的配置
-
`STREAMING_TIMEOUT`
:设置流式一次回复的超时时间,默认为 30 秒
## 部署
### 部署要求
...
...
common/go-channel.go
View file @
e7a4aa05
...
...
@@ -3,6 +3,7 @@ package common
import
(
"fmt"
"runtime/debug"
"time"
)
func
SafeGoroutine
(
f
func
())
{
...
...
@@ -45,3 +46,21 @@ func SafeSendString(ch chan string, value string) (closed bool) {
// If the code reaches here, then the channel was not closed.
return
false
}
// SafeSendStringTimeout send, return true, else return false
func
SafeSendStringTimeout
(
ch
chan
string
,
value
string
,
timeout
int
)
(
closed
bool
)
{
defer
func
()
{
// Recover from panic if one occured. A panic would mean the channel was closed.
if
recover
()
!=
nil
{
closed
=
false
}
}()
// This will panic if the channel is closed.
select
{
case
ch
<-
value
:
return
true
case
<-
time
.
After
(
time
.
Duration
(
timeout
)
*
time
.
Second
)
:
return
false
}
}
constant/system-setting.go
View file @
e7a4aa05
package
constant
import
"one-api/common"
var
ServerAddress
=
"http://localhost:3000"
var
WorkerUrl
=
""
var
WorkerValidKey
=
""
var
StreamingTimeout
=
common
.
GetOrDefault
(
"STREAMING_TIMEOUT"
,
30
)
func
EnableWorker
()
bool
{
return
WorkerUrl
!=
""
}
relay/channel/openai/relay-openai.go
View file @
e7a4aa05
...
...
@@ -8,6 +8,7 @@ import (
"io"
"net/http"
"one-api/common"
"one-api/constant"
"one-api/dto"
relaycommon
"one-api/relay/common"
relayconstant
"one-api/relay/constant"
...
...
@@ -51,7 +52,11 @@ func OpenaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.
if
data
[
:
6
]
!=
"data: "
&&
data
[
:
6
]
!=
"[DONE]"
{
continue
}
common
.
SafeSendString
(
dataChan
,
data
)
if
!
common
.
SafeSendStringTimeout
(
dataChan
,
data
,
constant
.
StreamingTimeout
)
{
// send data timeout, stop the stream
common
.
LogInfo
(
c
,
"send data timeout, stop the stream"
)
break
}
data
=
data
[
6
:
]
if
!
strings
.
HasPrefix
(
data
,
"[DONE]"
)
{
streamItems
=
append
(
streamItems
,
data
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment