因此,我有一个功能要添加到正在编写的程序中。基本上,如果用户的Cookie会说出他的用户名和密码,它将在存储的postgres数据库中查找用户名和Cookie的哈希值,如果得到匹配则返回肯定的响应和JWT令牌(我可以使用bcrypt散列-存储散列密码是否是个好主意,也许是另一个问题。
每次用户进入需要保存密码的页面时,都会向服务器发出此请求。
我的问题是我时不时地得到以下内容
inside CookieLogin
app_1 | search_userinfo_table started
db_1 | FATAL: sorry, too many clients already
app_1 | 2018/08/26 16:22:25 pq: sorry, too many clients already
app_1 | 2018/08/26 16:22:25 http: panic serving 172.21.0.1:37304: pq: sorry, too many clients already
app_1 | goroutine 704 [running]:
app_1 | net/http.(*conn).serve.func1(0xc420156f00)
app_1 | /usr/local/go/src/net/http/server.go:1721 +0xd0
app_1 | panic(0x6d63e0, 0xc420102c20)
app_1 | /usr/local/go/src/runtime/panic.go:489 +0x2cf
app_1 | log.Panic(0xc4200438c0, 0x1, 0x1)
app_1 | /usr/local/go/src/log/log.go:322 +0xc0
app_1 | github.com/patientplatypus/webserver/database.Search_userinfo_table(0xc42017f29d, 0xb, 0xc4201500c0, 0xc4200c22e0, 0xc420043a10)
app_1 | /go/src/github.com/patientplatypus/webserver/database/search.go:18 +0x169
app_1 | github.com/patientplatypus/webserver/authentication.loginHandler(0xc42017f29d, 0xb, 0xc42017f2ba, 0x3, 0x899a80, 0xc420148460)
app_1 | /go/src/github.com/patientplatypus/webserver/authentication/login.go:53 +0x39
app_1 | github.com/patientplatypus/webserver/authentication.CookieLogin(0x899a80, 0xc420148460, 0xc4203fc800)
app_1 | /go/src/github.com/patientplatypus/webserver/authentication/login.go:35 +0x279
app_1 | net/http.HandlerFunc.ServeHTTP(0x75c658, 0x899a80, 0xc420148460, 0xc4203fc800)
app_1 | /usr/local/go/src/net/http/server.go:1942 +0x44
app_1 | github.com/gorilla/mux.(*Router).ServeHTTP(0xc420052460, 0x899a80, 0xc420148460, 0xc4203fc800)
app_1 | /go/src/github.com/gorilla/mux/mux.go:162 +0x101
app_1 | main.JWTHandler.func1(0x899a80, 0xc420148460, 0xc4203fc600)
app_1 | /go/src/github.com/patientplatypus/webserver/main.go:34 +0x342
app_1 | net/http.HandlerFunc.ServeHTTP(0xc4200b12a0, 0x899a80, 0xc420148460, 0xc4203fc600)
app_1 | /usr/local/go/src/net/http/server.go:1942 +0x44
app_1 | github.com/rs/cors.(*Cors).Handler.func1(0x899a80, 0xc420148460, 0xc4203fc600)
app_1 | /go/src/github.com/rs/cors/cors.go:200 +0xe9
app_1 | net/http.HandlerFunc.ServeHTTP(0xc4200b12c0, 0x899a80, 0xc420148460, 0xc4203fc600)
app_1 | /usr/local/go/src/net/http/server.go:1942 +0x44
app_1 | net/http.serverHandler.ServeHTTP(0xc42008a630, 0x899a80, 0xc420148460, 0xc4203fc600)
app_1 | /usr/local/go/src/net/http/server.go:2568 +0x92
app_1 | net/http.(*conn).serve(0xc420156f00, 0x899fc0, 0xc4200506c0)
app_1 | /usr/local/go/src/net/http/server.go:1825 +0x612
app_1 | created by net/http.(*Server).Serve
app_1 | /usr/local/go/src/net/http/server.go:2668 +0x2ce
...,然后golang客户端将崩溃,我将不得不手动重启。这是一个仅在有时发生的错误,这令人难以置信,因为它使调试变得困难(抱歉,冗长的终端日志-我不确定该错误何时再次触发)。
有人有任何建议吗?这是no-duh解决方案的常见问题吗?
谢谢!
编辑:
据我所知,这是令人讨厌的postgres搜索查询:
package data
import(
"fmt"
"log"
_ "github.com/lib/pq"
)
func Search_userinfo_table(searchEmail string)(bool, string) {
fmt.Println("search_userinfo_table started")
rows, err1 := db.Query("SELECT * FROM userinfo")
if err1 != nil {
log.Panic(err1)
}
for rows.Next() {
var email string
var password string
var regString string
var regBool bool
var uid int
err2 := rows.Scan(&email, &password, ®String, ®Bool, &uid)
if err2 != nil {
log.Panic(err2)
}
fmt.Println("email | password")
fmt.Printf("%s | %s", email, password)
if email == searchEmail{
return false, password
}
}
return true, ""
}
将返回用户是否在数据库中以及哈希密码。与一些人交谈,答案似乎是postgres每次读取该内容都会添加一个新连接,然后最终崩溃,但是我希望它重新使用连接(尽管我认为这是自动发生的吗?)。无论如何,更多信息。
答案 0 :(得分:4)
您缺少必填的defer rows.Close()
。
*sql.Rows
的每个实例都有一个与之关联的连接。当您关闭行实例时,关联的连接将被释放并放回池(*DB
)中。
另一方面,不关闭行实例将导致其连接不为released。不释放连接将导致连接池尝试打开太多新连接。