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
17243a5d
authored
Apr 28, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: safe send channel
parent
8e3c5276
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
3 deletions
+18
-3
common/go-channel.go
+16
-1
relay/channel/openai/relay-openai.go
+2
-2
No files found.
common/go-channel.go
View file @
17243a5d
...
...
@@ -16,7 +16,22 @@ func SafeGoroutine(f func()) {
}()
}
func
SafeSend
(
ch
chan
bool
,
value
bool
)
(
closed
bool
)
{
func
SafeSendBool
(
ch
chan
bool
,
value
bool
)
(
closed
bool
)
{
defer
func
()
{
// Recover from panic if one occured. A panic would mean the channel was closed.
if
recover
()
!=
nil
{
closed
=
true
}
}()
// This will panic if the channel is closed.
ch
<-
value
// If the code reaches here, then the channel was not closed.
return
false
}
func
SafeSendString
(
ch
chan
string
,
value
string
)
(
closed
bool
)
{
defer
func
()
{
// Recover from panic if one occured. A panic would mean the channel was closed.
if
recover
()
!=
nil
{
...
...
relay/channel/openai/relay-openai.go
View file @
17243a5d
...
...
@@ -50,7 +50,7 @@ func OpenaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*d
if
data
[
:
6
]
!=
"data: "
&&
data
[
:
6
]
!=
"[DONE]"
{
continue
}
dataChan
<-
data
common
.
SafeSendString
(
dataChan
,
data
)
data
=
data
[
6
:
]
if
!
strings
.
HasPrefix
(
data
,
"[DONE]"
)
{
streamItems
=
append
(
streamItems
,
data
)
...
...
@@ -123,7 +123,7 @@ func OpenaiStreamHandler(c *gin.Context, resp *http.Response, relayMode int) (*d
// wait data out
time
.
Sleep
(
2
*
time
.
Second
)
}
common
.
SafeSend
(
stopChan
,
true
)
common
.
SafeSend
Bool
(
stopChan
,
true
)
}()
service
.
SetEventStreamHeaders
(
c
)
c
.
Stream
(
func
(
w
io
.
Writer
)
bool
{
...
...
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