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
4ea2556b
authored
Feb 19, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Replace manual goroutine creation with gopool.Go
parent
ff66890d
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
11 additions
and
21 deletions
+11
-21
common/go-channel.go
+0
-13
common/logger.go
+3
-2
main.go
+2
-2
relay/relay-text.go
+3
-2
service/notify-limit.go
+3
-2
No files found.
common/go-channel.go
View file @
4ea2556b
package
common
import
(
"fmt"
"runtime/debug"
"time"
)
func
SafeGoroutine
(
f
func
())
{
go
func
()
{
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
SysError
(
fmt
.
Sprintf
(
"child goroutine panic occured: error: %v, stack: %s"
,
r
,
string
(
debug
.
Stack
())))
}
}()
f
()
}()
}
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.
...
...
common/logger.go
View file @
4ea2556b
...
...
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"github.com/gin-gonic/gin"
"io"
"log"
...
...
@@ -80,9 +81,9 @@ func logHelper(ctx context.Context, level string, msg string) {
if
logCount
>
maxLogCount
&&
!
setupLogWorking
{
logCount
=
0
setupLogWorking
=
true
go
func
()
{
go
pool
.
Go
(
func
()
{
SetupLogger
()
}
(
)
})
}
}
...
...
main.go
View file @
4ea2556b
...
...
@@ -119,9 +119,9 @@ func main() {
}
if
os
.
Getenv
(
"ENABLE_PPROF"
)
==
"true"
{
go
func
()
{
go
pool
.
Go
(
func
()
{
log
.
Println
(
http
.
ListenAndServe
(
"0.0.0.0:8005"
,
nil
))
}
(
)
})
go
common
.
Monitor
()
common
.
SysLog
(
"pprof enabled"
)
}
...
...
relay/relay-text.go
View file @
4ea2556b
...
...
@@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"io"
"math"
"net/http"
...
...
@@ -307,14 +308,14 @@ func preConsumeQuota(c *gin.Context, preConsumedQuota int, relayInfo *relaycommo
func
returnPreConsumedQuota
(
c
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
userQuota
int
,
preConsumedQuota
int
)
{
if
preConsumedQuota
!=
0
{
go
func
()
{
go
pool
.
Go
(
func
()
{
relayInfoCopy
:=
*
relayInfo
err
:=
service
.
PostConsumeQuota
(
&
relayInfoCopy
,
-
preConsumedQuota
,
0
,
false
)
if
err
!=
nil
{
common
.
SysError
(
"error return pre-consumed quota: "
+
err
.
Error
())
}
}
(
)
})
}
}
...
...
service/notify-limit.go
View file @
4ea2556b
...
...
@@ -2,6 +2,7 @@ package service
import
(
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"one-api/common"
"one-api/constant"
"strconv"
...
...
@@ -27,7 +28,7 @@ func getDuration() time.Duration {
// startCleanupTask starts a background task to clean up expired entries
func
startCleanupTask
()
{
go
func
()
{
go
pool
.
Go
(
func
()
{
for
{
time
.
Sleep
(
time
.
Hour
)
now
:=
time
.
Now
()
...
...
@@ -40,7 +41,7 @@ func startCleanupTask() {
return
true
})
}
}
(
)
})
}
// CheckNotificationLimit checks if the user has exceeded their notification limit
...
...
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