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
fd36feb7
authored
Oct 01, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: check user quota when pre-consume quota
parent
e1597379
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
8 additions
and
1 deletions
+8
-1
controller/relay-audio.go
+4
-0
controller/relay-image.go
+1
-1
controller/relay-text.go
+3
-0
No files found.
controller/relay-audio.go
View file @
fd36feb7
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"bytes"
"bytes"
"context"
"context"
"encoding/json"
"encoding/json"
"errors"
"fmt"
"fmt"
"io"
"io"
"net/http"
"net/http"
...
@@ -31,6 +32,9 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
...
@@ -31,6 +32,9 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
if
err
!=
nil
{
if
err
!=
nil
{
return
errorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
errorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
}
}
if
userQuota
-
preConsumedQuota
<
0
{
return
errorWrapper
(
errors
.
New
(
"user quota is not enough"
),
"insufficient_user_quota"
,
http
.
StatusForbidden
)
}
err
=
model
.
CacheDecreaseUserQuota
(
userId
,
preConsumedQuota
)
err
=
model
.
CacheDecreaseUserQuota
(
userId
,
preConsumedQuota
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errorWrapper
(
err
,
"decrease_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
errorWrapper
(
err
,
"decrease_user_quota_failed"
,
http
.
StatusInternalServerError
)
...
...
controller/relay-image.go
View file @
fd36feb7
...
@@ -99,7 +99,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
...
@@ -99,7 +99,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
quota
:=
int
(
ratio
*
sizeRatio
*
1000
)
*
imageRequest
.
N
quota
:=
int
(
ratio
*
sizeRatio
*
1000
)
*
imageRequest
.
N
if
consumeQuota
&&
userQuota
-
quota
<
0
{
if
consumeQuota
&&
userQuota
-
quota
<
0
{
return
errorWrapper
(
err
,
"insufficient_user_quota"
,
http
.
StatusForbidden
)
return
errorWrapper
(
err
ors
.
New
(
"user quota is not enough"
)
,
"insufficient_user_quota"
,
http
.
StatusForbidden
)
}
}
req
,
err
:=
http
.
NewRequest
(
c
.
Request
.
Method
,
fullRequestURL
,
requestBody
)
req
,
err
:=
http
.
NewRequest
(
c
.
Request
.
Method
,
fullRequestURL
,
requestBody
)
...
...
controller/relay-text.go
View file @
fd36feb7
...
@@ -204,6 +204,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
...
@@ -204,6 +204,9 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
if
err
!=
nil
{
if
err
!=
nil
{
return
errorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
errorWrapper
(
err
,
"get_user_quota_failed"
,
http
.
StatusInternalServerError
)
}
}
if
userQuota
-
preConsumedQuota
<
0
{
return
errorWrapper
(
errors
.
New
(
"user quota is not enough"
),
"insufficient_user_quota"
,
http
.
StatusForbidden
)
}
err
=
model
.
CacheDecreaseUserQuota
(
userId
,
preConsumedQuota
)
err
=
model
.
CacheDecreaseUserQuota
(
userId
,
preConsumedQuota
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errorWrapper
(
err
,
"decrease_user_quota_failed"
,
http
.
StatusInternalServerError
)
return
errorWrapper
(
err
,
"decrease_user_quota_failed"
,
http
.
StatusInternalServerError
)
...
...
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