這篇文章主要講解了“怎么用go配置MySQL連接池”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用go配置mysql連接池”吧!
創(chuàng)新互聯(lián)公司網(wǎng)絡(luò)公司擁有10多年的成都網(wǎng)站開發(fā)建設(shè)經(jīng)驗(yàn),1000+客戶的共同信賴。提供網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、網(wǎng)站開發(fā)、網(wǎng)站定制、賣友情鏈接、建網(wǎng)站、網(wǎng)站搭建、響應(yīng)式網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格,提供周到的售前咨詢和貼心的售后服務(wù)
mysql配置文檔
max-connections=10000
代碼
orm
-c: 并發(fā)量100
-n: 總請(qǐng)求量300
? ~ ab -c 100 -n 300 "http://localhost:8080/pool" This is ApacheBench, Version 2.3 <$Revision: 1826891 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Finished 300 requests Server Software: Server Hostname: localhost Server Port: 8080 Document Path: /pool Document Length: 304 bytes Concurrency Level: 100 Time taken for tests: 4.073 seconds Complete requests: 300 Failed requests: 0 Total transferred: 128400 bytes HTML transferred: 91200 bytes Requests per second: 73.66 [#/sec] (mean) Time per request: 1357.601 [ms] (mean) Time per request: 13.576 [ms] (mean, across all concurrent requests) Transfer rate: 30.79 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 2.2 1 8 Processing: 1001 1012 8.2 1010 1036 Waiting: 1001 1012 8.1 1010 1036 Total: 1002 1014 9.5 1012 1043 Percentage of the requests served within a certain time (ms) 50% 1012 66% 1016 75% 1019 80% 1021 90% 1031 95% 1035 98% 1039 99% 1040 100% 1043 (longest request) ? ~
設(shè)置空閑連接為10個(gè)
最大可以打開的連接為10個(gè) mysql.conf中可以修改最大連接數(shù).
db/db.go
sqlDB.SetMaxIdleConns(10) // SetMaxOpenConns 設(shè)置打開數(shù)據(jù)庫(kù)連接的最大數(shù)量。 sqlDB.SetMaxOpenConns(10) // SetConnMaxLifetime 設(shè)置了連接可復(fù)用的最大時(shí)間。 sqlDB.SetConnMaxLifetime(time.Hour)
controller/pool.go
func PoolHandler(c *gin.Context) { time.Sleep(time.Second) var user model.User tx := db.DB.Raw("SELECT id, name, age FROM users WHERE name = ? limit 1", "D42").Scan(&user) if tx.Error != nil { panic(tx.Error) } logger.Debugf("raw sql id:%v name:%v age:%v", user.ID, user.Name, user.Age) c.JSON(200, user) }
SHOW PROCESSLIST;
Id User Host db Command Time State Info 8 root localhost:51148 demo_go Query 0 starting SHOW PROCESSLIST 900 root localhost:64598 demo_go Sleep 0 901 root localhost:64754 demo_go Sleep 0 902 root localhost:64755 demo_go Sleep 0 903 root localhost:64756 demo_go Sleep 0 904 root localhost:64757 demo_go Sleep 0 905 root localhost:64758 demo_go Sleep 0 906 root localhost:64759 demo_go Sleep 0 907 root localhost:64760 demo_go Sleep 0 908 root localhost:64761 demo_go Sleep 0 909 root localhost:64762 demo_go Sleep 0
感謝各位的閱讀,以上就是“怎么用go配置mysql連接池”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用go配置mysql連接池這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!