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
efc7c4d7
authored
Mar 04, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Add context-aware goroutine pool for safer concurrent operations
parent
67e17dcd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
2 deletions
+31
-2
common/gopool.go
+25
-0
relay/channel/openai/relay-openai.go
+6
-2
No files found.
common/gopool.go
0 → 100644
View file @
efc7c4d7
package
common
import
(
"context"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"math"
)
var
relayGoPool
gopool
.
Pool
func
init
()
{
relayGoPool
=
gopool
.
NewPool
(
"gopool.RelayPool"
,
math
.
MaxInt32
,
gopool
.
NewConfig
())
relayGoPool
.
SetPanicHandler
(
func
(
ctx
context
.
Context
,
i
interface
{})
{
//check ctx.Value("stop_chan").(chan bool)
if
stopChan
,
ok
:=
ctx
.
Value
(
"stop_chan"
)
.
(
chan
bool
);
ok
{
SafeSendBool
(
stopChan
,
true
)
}
SysError
(
fmt
.
Sprintf
(
"panic in gopool.RelayPool: %v"
,
i
))
})
}
func
CtxGo
(
ctx
context
.
Context
,
f
func
())
{
relayGoPool
.
CtxGo
(
ctx
,
f
)
}
relay/channel/openai/relay-openai.go
View file @
efc7c4d7
...
@@ -3,6 +3,7 @@ package openai
...
@@ -3,6 +3,7 @@ package openai
import
(
import
(
"bufio"
"bufio"
"bytes"
"bytes"
"context"
"encoding/json"
"encoding/json"
"fmt"
"fmt"
"io"
"io"
...
@@ -120,13 +121,16 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel
...
@@ -120,13 +121,16 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel
ticker
:=
time
.
NewTicker
(
streamingTimeout
)
ticker
:=
time
.
NewTicker
(
streamingTimeout
)
defer
ticker
.
Stop
()
defer
ticker
.
Stop
()
stopChan
:=
make
(
chan
bool
)
stopChan
:=
make
(
chan
bool
,
2
)
defer
close
(
stopChan
)
defer
close
(
stopChan
)
var
(
var
(
lastStreamData
string
lastStreamData
string
mu
sync
.
Mutex
mu
sync
.
Mutex
)
)
gopool
.
Go
(
func
()
{
ctx
:=
context
.
WithValue
(
context
.
Background
(),
"stop_chan"
,
stopChan
)
common
.
CtxGo
(
ctx
,
func
()
{
for
scanner
.
Scan
()
{
for
scanner
.
Scan
()
{
//info.SetFirstResponseTime()
//info.SetFirstResponseTime()
ticker
.
Reset
(
time
.
Duration
(
constant
.
StreamingTimeout
)
*
time
.
Second
)
ticker
.
Reset
(
time
.
Duration
(
constant
.
StreamingTimeout
)
*
time
.
Second
)
...
...
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