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
8a2f1b64
authored
Aug 12, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: set connection limits for database
parent
31a75c07
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
1 deletions
+28
-1
README.md
+5
-0
common/utils.go
+13
-0
model/main.go
+10
-1
No files found.
README.md
View file @
8a2f1b64
...
@@ -281,6 +281,11 @@ graph LR
...
@@ -281,6 +281,11 @@ graph LR
+
注意需要提前建立数据库
`oneapi`
,无需手动建表,程序将自动建表。
+
注意需要提前建立数据库
`oneapi`
,无需手动建表,程序将自动建表。
+
如果使用本地数据库:部署命令可添加
`--network="host"`
以使得容器内的程序可以访问到宿主机上的 MySQL。
+
如果使用本地数据库:部署命令可添加
`--network="host"`
以使得容器内的程序可以访问到宿主机上的 MySQL。
+
如果使用云数据库:如果云服务器需要验证身份,需要在连接参数中添加
`?tls=skip-verify`
。
+
如果使用云数据库:如果云服务器需要验证身份,需要在连接参数中添加
`?tls=skip-verify`
。
+
请根据你的数据库配置修改下列参数(或者保持默认值):
+
`SQL_MAX_IDLE_CONNS`
:最大空闲连接数,默认为
`10`
。
+
`SQL_MAX_OPEN_CONNS`
:最大打开连接数,默认为
`100`
。
+
如果报错
`Error 1040: Too many connections`
,请适当减小该值。
+
`SQL_CONN_MAX_LIFETIME`
:连接的最大生命周期,默认为
`60`
,单位分钟。
4.
`FRONTEND_BASE_URL`
:设置之后将重定向页面请求到指定的地址,仅限从服务器设置。
4.
`FRONTEND_BASE_URL`
:设置之后将重定向页面请求到指定的地址,仅限从服务器设置。
+
例子:
`FRONTEND_BASE_URL=https://openai.justsong.cn`
+
例子:
`FRONTEND_BASE_URL=https://openai.justsong.cn`
5.
`SYNC_FREQUENCY`
:设置之后将定期与数据库同步配置,单位为秒,未设置则不进行同步。
5.
`SYNC_FREQUENCY`
:设置之后将定期与数据库同步配置,单位为秒,未设置则不进行同步。
...
...
common/utils.go
View file @
8a2f1b64
...
@@ -7,6 +7,7 @@ import (
...
@@ -7,6 +7,7 @@ import (
"log"
"log"
"math/rand"
"math/rand"
"net"
"net"
"os"
"os/exec"
"os/exec"
"runtime"
"runtime"
"strconv"
"strconv"
...
@@ -177,3 +178,15 @@ func Max(a int, b int) int {
...
@@ -177,3 +178,15 @@ func Max(a int, b int) int {
return
b
return
b
}
}
}
}
func
GetOrDefault
(
env
string
,
defaultValue
int
)
int
{
if
env
==
""
||
os
.
Getenv
(
env
)
==
""
{
return
defaultValue
}
num
,
err
:=
strconv
.
Atoi
(
os
.
Getenv
(
env
))
if
err
!=
nil
{
SysError
(
fmt
.
Sprintf
(
"failed to parse %s: %s, using default value: %d"
,
env
,
err
.
Error
(),
defaultValue
))
return
defaultValue
}
return
num
}
model/main.go
View file @
8a2f1b64
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"gorm.io/gorm"
"gorm.io/gorm"
"one-api/common"
"one-api/common"
"os"
"os"
"time"
)
)
var
DB
*
gorm
.
DB
var
DB
*
gorm
.
DB
...
@@ -57,10 +58,18 @@ func InitDB() (err error) {
...
@@ -57,10 +58,18 @@ func InitDB() (err error) {
common
.
SysLog
(
"database connected"
)
common
.
SysLog
(
"database connected"
)
if
err
==
nil
{
if
err
==
nil
{
DB
=
db
DB
=
db
sqlDB
,
err
:=
DB
.
DB
()
if
err
!=
nil
{
return
err
}
sqlDB
.
SetMaxIdleConns
(
common
.
GetOrDefault
(
"SQL_MAX_IDLE_CONNS"
,
10
))
sqlDB
.
SetMaxOpenConns
(
common
.
GetOrDefault
(
"SQL_MAX_OPEN_CONNS"
,
100
))
sqlDB
.
SetConnMaxLifetime
(
time
.
Second
*
time
.
Duration
(
common
.
GetOrDefault
(
"SQL_MAX_LIFETIME"
,
60
)))
if
!
common
.
IsMasterNode
{
if
!
common
.
IsMasterNode
{
return
nil
return
nil
}
}
err
:
=
db
.
AutoMigrate
(
&
Channel
{})
err
=
db
.
AutoMigrate
(
&
Channel
{})
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
...
...
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