[英]Gorm & Postgres Connection reset by peer
Context语境
Currently I have a REST API that manages customer's data in a db.目前我有一个 REST API 在数据库中管理客户的数据。 I'm using the following stack:我正在使用以下堆栈:
Go 1.13 Go 1.13
github.com/jinzhu/gorm v1.9.1 github.com/jinzhu/gorm v1.9.1
Postgres 11 Postgres 11
I have the following connection settings.我有以下连接设置。
// NewConnection ...
func NewConnection() (*gorm.DB, error) {
config := getConfig()
connStr := "host=xx.xx.xx port=5432 user=chavista-hatter dbname=my-db password=abc sslmode=verify-ca sslrootcert=/path/to/rcert sslcert=/path/to/cert sslkey=/path/to/key connect_timeout=0"
db, err := gorm.Open("postgres", conn)
if err != nil {
return nil, err
}
db.DB().SetMaxOpenConns(25)
db.DB().SetMaxIdleConns(25)
db.DB().SetConnMaxLifetime(5 * time.Minute)
db.SingularTable(true)
if config.LogQueries {
db = db.Debug()
}
return db, nil
}
I get a connection in the main class and inject that connection into a repository class that executes the queries through Gorm (ORM)我在主 class 中获得了一个连接,并将该连接注入到通过 Gorm (ORM) 执行查询的存储库 class
Main class主class
db, err := database.NewConnection()
if err != nil {
panic(fmt.Sprintf("failed to connect database --> %v", err))
}
fmt.Println("database connection established successfully")
defer db.Close()
customerRepo := customer.NewRepository(db)
Repository class存储库 class
type repository struct {
db *gorm.DB
}
//NewRepository
func NewRepository(db *gorm.DB) Repository {
return &repository{
db: db,
}
}
func (r *repository) Register(customer *models.Customer) (string, error) {
err := r.db.Create(&customer).Error
if err != nil {
return "", err
}
return customer.key, nil
}
Problem问题
I sending over 500k request (INSERTS) to my db which have 512 connections available and after a few minutes the following error starts to come up repeatedly in postgres log:我向我的数据库发送了超过 50 万个请求(插入),其中有 512 个可用连接,几分钟后,postgres 日志中开始反复出现以下错误:
unexpected EOF on client connection with an open transaction
could not receive data from client: Connection reset by peer
Any help?有什么帮助吗?
How are you using GetConnection
in your code?您如何在代码中使用GetConnection
? It's creating a new connection pool every time it's called - ideally you'd only want to call it once, and pass that single connection around wherever it's used.每次调用它都会创建一个新的连接池 - 理想情况下,您只想调用它一次,然后在使用它的任何地方传递该单个连接。
I would try changing it to this:我会尝试将其更改为:
var db *gorm.DB
func NewConnection() (*gorm.DB, error) {
if db != nil {
return db, nil
}
config := getConfig()
connStr := "host=xx.xx.xx port=5432 user=chavista-hatter dbname=my-db password=abc sslmode=verify-ca sslrootcert=/path/to/rcert sslcert=/path/to/cert sslkey=/path/to/key connect_timeout=0"
var err error
db, err = gorm.Open("postgres", conn)
if err != nil {
return nil, err
}
db.DB().SetMaxOpenConns(25)
db.DB().SetMaxIdleConns(25)
db.DB().SetConnMaxLifetime(5 * time.Minute)
db.SingularTable(true)
if config.LogQueries {
db = db.Debug()
}
return db, nil
}
and see if it solves the issue.看看它是否能解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.