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
ad4d3efd
authored
Jun 10, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
feat(middleware): add HTTP statistics middleware
parent
7dc7805c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
50 additions
and
2 deletions
+50
-2
controller/misc.go
+7
-2
middleware/stats.go
+42
-0
router/relay-router.go
+1
-0
No files found.
controller/misc.go
View file @
ad4d3efd
...
...
@@ -6,6 +6,7 @@ import (
"net/http"
"one-api/common"
"one-api/constant"
"one-api/middleware"
"one-api/model"
"one-api/setting"
"one-api/setting/operation_setting"
...
...
@@ -24,14 +25,18 @@ func TestStatus(c *gin.Context) {
})
return
}
// 获取HTTP统计信息
httpStats
:=
middleware
.
GetStats
()
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
"Server is running"
,
"success"
:
true
,
"message"
:
"Server is running"
,
"http_stats"
:
httpStats
,
})
return
}
func
GetStatus
(
c
*
gin
.
Context
)
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
...
...
middleware/stats.go
0 → 100644
View file @
ad4d3efd
package
middleware
import
(
"sync/atomic"
"github.com/gin-gonic/gin"
)
// HTTPStats 存储HTTP统计信息
type
HTTPStats
struct
{
activeConnections
int64
}
var
globalStats
=
&
HTTPStats
{}
// StatsMiddleware 统计中间件
func
StatsMiddleware
()
gin
.
HandlerFunc
{
return
func
(
c
*
gin
.
Context
)
{
// 增加活跃连接数
atomic
.
AddInt64
(
&
globalStats
.
activeConnections
,
1
)
// 确保在请求结束时减少连接数
defer
func
()
{
atomic
.
AddInt64
(
&
globalStats
.
activeConnections
,
-
1
)
}()
c
.
Next
()
}
}
// StatsInfo 统计信息结构
type
StatsInfo
struct
{
ActiveConnections
int64
`json:"active_connections"`
}
// GetStats 获取统计信息
func
GetStats
()
StatsInfo
{
return
StatsInfo
{
ActiveConnections
:
atomic
.
LoadInt64
(
&
globalStats
.
activeConnections
),
}
}
\ No newline at end of file
router/relay-router.go
View file @
ad4d3efd
...
...
@@ -11,6 +11,7 @@ import (
func
SetRelayRouter
(
router
*
gin
.
Engine
)
{
router
.
Use
(
middleware
.
CORS
())
router
.
Use
(
middleware
.
DecompressRequestMiddleware
())
router
.
Use
(
middleware
.
StatsMiddleware
())
// https://platform.openai.com/docs/api-reference/introduction
modelsRouter
:=
router
.
Group
(
"/v1/models"
)
modelsRouter
.
Use
(
middleware
.
TokenAuth
())
...
...
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