自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

五分鐘搞懂 Golang 數(shù)據(jù)庫連接管理

開發(fā) 數(shù)據(jù)庫
本文介紹了如何在 Golang 中優(yōu)化數(shù)據(jù)庫連接,通過有效管理連接來提高應(yīng)用程序吞吐量。

Go 的 database/sql 軟件包提供了自動(dòng)化數(shù)據(jù)庫連接池,能夠幫助開發(fā)人員有效管理連接。通常情況下,開發(fā)人員會(huì)請(qǐng)求某個(gè)打開的連接,執(zhí)行查詢,然后關(guān)閉連接以確保連接返回到池中。

開發(fā)人員常犯的一個(gè)錯(cuò)誤是長(zhǎng)時(shí)間持有數(shù)據(jù)庫連接,從而導(dǎo)致性能瓶頸。新請(qǐng)求不得不等待可用連接,造成連接池的效率受到影響。

本文將探討如何避免這一問題,并通過確定常見問題域和學(xué)習(xí)解決方法,優(yōu)化 Go 應(yīng)用以提高吞吐量。

基本示例

我們以一個(gè)返回雇員記錄的基本 HTTP 處理程序?yàn)槔?/p>

func GetEmployeesHandler(w http.ResponseWriter, r *http.Request) {
    rows, err := db.Query(`SELECT id, name, email FROM employee`)
    if err != nil {
        http.Error(w, fmt.Sprintf("error querying database: %v", err), http.StatusInternalServerError)
        return
    }
    defer rows.Close()

    var employees []Employee
    for rows.Next() {
        var e Employee
        if err := rows.Scan(&e.ID, &e.Name, &e.Email); err != nil {
            http.Error(w, fmt.Sprintf("Error scanning row: %v", err), http.StatusInternalServerError)
            return
        }
        decorateEmployee(&e)
        employees = append(employees, e)
    }

    if err = rows.Err(); err != nil {
        http.Error(w, fmt.Sprintf("error during row iteration: %v", err), http.StatusInternalServerError)
        return
    }

    w.Header().Set("Content-Type", "application/json")
    if err := json.NewEncoder(w).Encode(employees); err != nil {
        http.Error(w, "Error encoding response", http.StatusInternalServerError)
        return
    }
}

在這個(gè)處理程序中:

  • 查詢數(shù)據(jù)庫中的雇員記錄。
  • 通過 defer rows.Close() 確保在處理完結(jié)果集后關(guān)閉連接。
  • 掃描每一行,并用從外部獲取的數(shù)據(jù)對(duì)其進(jìn)行裝飾。
  • 將最終結(jié)果追加到數(shù)組中。
  • 檢查迭代過程中的任何錯(cuò)誤,并以 JSON 格式返回結(jié)果。

乍一看,似乎沒有什么特別的地方。不過,你會(huì)期待在壓力測(cè)試的時(shí)候獲得更好的性能。

初步性能結(jié)果

使用 Vegeta[2] 等壓力測(cè)試工具,可以模擬該端點(diǎn)的負(fù)載情況。在每秒 10 個(gè)請(qǐng)求(RPS,requests per second)的初始速率下,應(yīng)用在 30 秒的測(cè)試運(yùn)行中表現(xiàn)相對(duì)較好:

$ echo "GET http://localhost:8080/employees" | vegeta attack -duration=30s -rate=10 | tee results.bin | vegeta report
Requests      [total, rate, throughput]         300, 10.03, 5.45
Duration      [total, attack, wait]             52.095s, 29.9s, 22.196s
Latencies     [min, mean, 50, 90, 95, 99, max]  2.318s, 11.971s, 8.512s, 26.222s, 30.001s, 30.001s, 30.001s
Bytes In      [total, mean]                     2290991, 7636.64
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           94.67%
Status Codes  [code:count]                      0:16  200:284

然而,當(dāng)我們將負(fù)載增加到 50 RPS 時(shí),就會(huì)發(fā)現(xiàn)吞吐量大幅下降,請(qǐng)求失敗率急劇上升:

$ echo "GET http://localhost:8080/employees" | vegeta attack -duration=30s -rate=50 | tee results.bin | vegeta report
Requests      [total, rate, throughput]         1500, 50.03, 4.20
Duration      [total, attack, wait]             59.981s, 29.981s, 30s
Latencies     [min, mean, 50, 90, 95, 99, max]  2.208s, 27.175s, 30.001s, 30.001s, 30.001s, 30.002s, 30.002s
Bytes In      [total, mean]                     2032879, 1355.25
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           16.80%
Status Codes  [code:count]                      0:1248  200:252

(上述狀態(tài)代碼為 0 表示測(cè)試運(yùn)行過程中出現(xiàn)客戶端超時(shí))

定位瓶頸

當(dāng) RPS 為 50 時(shí),成功率急劇下降,吞吐量降至每秒僅 4.2 個(gè)請(qǐng)求。為什么會(huì)這樣?其中一個(gè)可能的原因是,考慮到當(dāng)前資源,50 RPS 是一個(gè)不合理的目標(biāo)。為了確認(rèn)代碼是否可以通過修改獲得更好的性能,我們可以研究收集一些指標(biāo)。其中一個(gè)指標(biāo)來源是裝飾過程,但在本文中,我們將重點(diǎn)關(guān)注數(shù)據(jù)庫連接池統(tǒng)計(jì)數(shù)據(jù)。

Go 的 database/sql 軟件包可通過 DBStats 函數(shù)查看應(yīng)用的數(shù)據(jù)庫池性能,會(huì)返回我們感興趣的統(tǒng)計(jì)信息:

  • InUse: 當(dāng)前正在使用的連接數(shù)。
  • Idle:空閑連接數(shù)。
  • WaitCount:等待的連接總數(shù)。

可以通過添加另一個(gè)端點(diǎn)處理程序來輸出這些值:

func GetInfoHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    if err := json.NewEncoder(w).Encode(db.Stats()); err != nil {
        http.Error(w, "Error encoding response", http.StatusInternalServerError)
        return
    }
}

重新運(yùn)行上述壓力測(cè)試,并對(duì) /info 端點(diǎn)進(jìn)行監(jiān)控:

$ while true; do curl -s http://localhost:8080/info; sleep 2; done
...
{"MaxOpenConnections":15,"OpenConnections":15,"InUse":15,"Idle":0,"WaitCount":1434,"WaitDuration":1389188829869,"MaxIdleClosed":0,"MaxIdleTimeClosed":0,"MaxLifetimeClosed":0}
{"MaxOpenConnections":15,"OpenConnections":15,"InUse":15,"Idle":0,"WaitCount":1485,"WaitDuration":1582086078604,"MaxIdleClosed":0,"MaxIdleTimeClosed":0,"MaxLifetimeClosed":0}
{"MaxOpenConnections":15,"OpenConnections":15,"InUse":15,"Idle":0,"WaitCount":1485,"WaitDuration":1772844971842,"MaxIdleClosed":0,"MaxIdleTimeClosed":0,"MaxLifetimeClosed":0}
...

上述結(jié)果表明連接池已達(dá)到最大值(InUse: 15, Idle: 0),每個(gè)新請(qǐng)求都被迫等待(WaitCount 不斷增加)。換句話說,連接池基本上處于停滯狀態(tài),從而導(dǎo)致了之前觀察到的延遲和超時(shí)問題!

優(yōu)化連接使用

查看原始代碼,我們發(fā)現(xiàn)問題要么出在查詢本身性能不佳,要么出在遍歷結(jié)果集時(shí)每次調(diào)用裝飾函數(shù)都需要很長(zhǎng)時(shí)間才能返回??梢試L試在 rows.Next() 循環(huán)之外裝飾記錄,并將其移至數(shù)據(jù)庫連接使用之下,從而找出問題所在。

以下是更新后的代碼:

func GetEmployeesHandler(w http.ResponseWriter, r *http.Request) {
    rows, err := db.Query(`SELECT id, name, email FROM employee`)
    if err != nil {
        http.Error(w, fmt.Sprintf("error querying database: %v", err), http.StatusInternalServerError)
        return
    }

    var employees []Employee
    for rows.Next() {
        var e Employee
        if err := rows.Scan(&e.ID, &e.Name, &e.Email); err != nil {
            http.Error(w, fmt.Sprintf("Error scanning row: %v", err), http.StatusInternalServerError)
            return
        }
        employees = append(employees, e)
    }

    if err = rows.Err(); err != nil {
        http.Error(w, fmt.Sprintf("error during row iteration: %v", err), http.StatusInternalServerError)
        return
    }
    rows.Close()

    for i := range employees {
        decorateEmployee(&employees[i])
    }

    w.Header().Set("Content-Type", "application/json")
    if err := json.NewEncoder(w).Encode(employees); err != nil {
        http.Error(w, "Error encoding response", http.StatusInternalServerError)
        return
    }
}

在這個(gè)重構(gòu)的處理程序中,我們:

  • 將所有行掃描到內(nèi)存中。
  • 掃描后立即關(guān)閉連接,將其釋放回池。
  • 在內(nèi)存中裝飾雇員記錄,而無需保持連接打開。

優(yōu)化后的性能

優(yōu)化后以 50 RPS 運(yùn)行相同的 Vegeta 測(cè)試,結(jié)果如下:

$ echo "GET http://localhost:8080/employees" | vegeta attack -duration=30s -rate=50 | tee results.bin | vegeta report
Requests      [total, rate, throughput]         1500, 50.03, 45.78
Duration      [total, attack, wait]             32.768s, 29.98s, 2.788s
Latencies     [min, mean, 50, 90, 95, 99, max]  2.045s, 2.502s, 2.499s, 2.692s, 2.741s, 2.856s, 2.995s
Bytes In      [total, mean]                     11817000, 7878.00
Bytes Out     [total, mean]                     0, 0.00
Success       [ratio]                           100.00%
Status Codes  [code:count]                      200:1500
...
{"MaxOpenConnections":15,"OpenConnections":1,"InUse":0,"Idle":1,"WaitCount":0,"WaitDuration":0,"MaxIdleClosed":0,"MaxIdleTimeClosed":0,"MaxLifetimeClosed":0}
{"MaxOpenConnections":15,"OpenConnections":1,"InUse":0,"Idle":1,"WaitCount":0,"WaitDuration":0,"MaxIdleClosed":0,"MaxIdleTimeClosed":0,"MaxLifetimeClosed":0}
{"MaxOpenConnections":15,"OpenConnections":1,"InUse":0,"Idle":1,"WaitCount":0,"WaitDuration":0,"MaxIdleClosed":0,"MaxIdleTimeClosed":0,"MaxLifetimeClosed":0}
...

可以看到,不僅吞吐量和延遲得到了 100% 的大幅改善,而且 OpenConnections 的總數(shù)也沒有超過 1,甚至還有閑置連接處于待機(jī)狀態(tài),從而使 WaitCount 始終為零!

總結(jié)

通過優(yōu)化連接的處理方式,先將所有行獲取到內(nèi)存中,然后立即關(guān)閉連接,而不是在執(zhí)行其他 I/O 綁定操作(如裝飾記錄)時(shí)保持連接打開。這樣,數(shù)據(jù)庫連接就能盡快返回到池中,為其他傳入請(qǐng)求釋放資源,從而提高吞吐量和并發(fā)性。

責(zé)任編輯:趙寧寧 來源: DeepNoMind
相關(guān)推薦

2025-01-20 08:50:00

2025-01-21 07:39:04

Linux堆內(nèi)存Golang

2024-12-11 07:00:00

面向?qū)ο?/a>代碼

2025-03-13 06:22:59

2009-11-20 18:08:37

Oracle數(shù)據(jù)庫

2019-08-09 10:33:36

開發(fā)技能代碼

2009-11-02 18:07:58

Oracle數(shù)據(jù)庫

2018-06-26 09:37:07

時(shí)序數(shù)據(jù)庫FacebookNoSQL

2025-03-18 09:20:00

Go語言Golang

2009-11-20 17:06:49

Oracle數(shù)據(jù)庫字符

2025-02-25 07:49:36

智能體數(shù)據(jù)庫DeepSeek

2023-04-15 20:33:35

圖形數(shù)據(jù)庫數(shù)據(jù)庫

2023-09-18 15:49:40

Ingress云原生Kubernetes

2017-03-27 23:22:23

數(shù)據(jù)庫列式存儲(chǔ)

2023-12-06 08:48:36

Kubernetes組件

2023-07-30 10:09:36

MMD數(shù)據(jù)庫

2024-01-29 00:20:00

GolangGo代碼

2023-10-06 20:21:28

Python鏈表

2023-09-07 23:52:50

Flink代碼

2024-04-29 07:57:46

分布式流控算法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)