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
Unverified
Commit
5b86ce0d
authored
May 27, 2026
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: optimize batch update process
parent
74985fa8
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
11 deletions
+43
-11
model/user.go
+17
-0
model/utils.go
+26
-11
No files found.
model/user.go
View file @
5b86ce0d
...
...
@@ -984,6 +984,23 @@ func updateUserUsedQuotaAndRequestCount(id int, quota int, count int) {
//}
}
func
updateUserQuotaUsedQuotaAndRequestCount
(
id
int
,
quota
int
,
usedQuota
int
,
requestCount
int
)
{
if
quota
==
0
&&
usedQuota
==
0
&&
requestCount
==
0
{
return
}
err
:=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
map
[
string
]
interface
{}{
"quota"
:
gorm
.
Expr
(
"quota + ?"
,
quota
),
"used_quota"
:
gorm
.
Expr
(
"used_quota + ?"
,
usedQuota
),
"request_count"
:
gorm
.
Expr
(
"request_count + ?"
,
requestCount
),
},
)
.
Error
if
err
!=
nil
{
common
.
SysLog
(
"failed to batch update user quota, used quota and request count: "
+
err
.
Error
())
}
}
func
updateUserUsedQuota
(
id
int
,
quota
int
)
{
err
:=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Updates
(
map
[
string
]
interface
{}{
...
...
model/utils.go
View file @
5b86ce0d
...
...
@@ -67,33 +67,48 @@ func batchUpdate() {
}
common
.
SysLog
(
"batch update started"
)
stores
:=
make
([]
map
[
int
]
int
,
BatchUpdateTypeCount
)
for
i
:=
0
;
i
<
BatchUpdateTypeCount
;
i
++
{
batchUpdateLocks
[
i
]
.
Lock
()
store
:
=
batchUpdateStores
[
i
]
store
s
[
i
]
=
batchUpdateStores
[
i
]
batchUpdateStores
[
i
]
=
make
(
map
[
int
]
int
)
batchUpdateLocks
[
i
]
.
Unlock
()
// TODO: maybe we can combine updates with same key?
}
for
i
,
store
:=
range
stores
{
if
i
==
BatchUpdateTypeUserQuota
||
i
==
BatchUpdateTypeUsedQuota
||
i
==
BatchUpdateTypeRequestCount
{
continue
}
for
key
,
value
:=
range
store
{
switch
i
{
case
BatchUpdateTypeUserQuota
:
err
:=
increaseUserQuota
(
key
,
value
)
if
err
!=
nil
{
common
.
SysLog
(
"failed to batch update user quota: "
+
err
.
Error
())
}
case
BatchUpdateTypeTokenQuota
:
err
:=
increaseTokenQuota
(
key
,
value
)
if
err
!=
nil
{
common
.
SysLog
(
"failed to batch update token quota: "
+
err
.
Error
())
}
case
BatchUpdateTypeUsedQuota
:
updateUserUsedQuota
(
key
,
value
)
case
BatchUpdateTypeRequestCount
:
updateUserRequestCount
(
key
,
value
)
case
BatchUpdateTypeChannelUsedQuota
:
updateChannelUsedQuota
(
key
,
value
)
}
}
}
userQuotaStore
:=
stores
[
BatchUpdateTypeUserQuota
]
usedQuotaStore
:=
stores
[
BatchUpdateTypeUsedQuota
]
requestCountStore
:=
stores
[
BatchUpdateTypeRequestCount
]
userIDs
:=
make
(
map
[
int
]
struct
{},
len
(
userQuotaStore
)
+
len
(
usedQuotaStore
)
+
len
(
requestCountStore
))
for
key
:=
range
userQuotaStore
{
userIDs
[
key
]
=
struct
{}{}
}
for
key
:=
range
usedQuotaStore
{
userIDs
[
key
]
=
struct
{}{}
}
for
key
:=
range
requestCountStore
{
userIDs
[
key
]
=
struct
{}{}
}
for
key
:=
range
userIDs
{
updateUserQuotaUsedQuotaAndRequestCount
(
key
,
userQuotaStore
[
key
],
usedQuotaStore
[
key
],
requestCountStore
[
key
])
}
common
.
SysLog
(
"batch update finished"
)
}
...
...
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