在Go 中偵測失效的RabbitMQ 連結
在這種情況下,使用streadway/amqp 函式庫用Go 編寫的消費者腳本在以下情況下無法退出: RabbitMQ 伺服器已停止。此外,一旦伺服器重新啟動,消費者就會停止接收訊息。
解決方案:使用連線通知
amqp.Connection 類型有一個 NotifyClose() 方法,該方法提供了用於發送傳輸或協定錯誤訊號的通道。透過使用此方法,我們可以處理連接斷開事件並優雅地處理它們。
以下程式碼片段示範如何使用連線通知來偵測死連線並執行必要的操作:
import ( "log" "time" "github.com/streadway/amqp" ) func main() { for { // Reconnection loop conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/") // Setup if err != nil { log.Fatal(err) } notify := conn.NotifyClose(make(chan *amqp.Error)) // Error channel ch, err := conn.Channel() if err != nil { log.Fatal(err) } msgs, err := ch.Consume( "test_task_queue", // Queue "", // Consumer false, // Auto-ack false, // Exclusive false, // No-local false, // No-wait nil, // Args ) if err != nil { log.Fatal(err) } for { // Receive loop select { // Check connection case err = <p>在這個修改後的腳本中,我們將主要的消費者邏輯包裝在一個循環中,此循環使用通知通道持續檢查連線關閉。如果收到錯誤,它會記錄錯誤,然後進程重新連線。 </p><p>透過利用連線通知,我們可以偵測失效的 RabbitMQ 連線並採取適當的操作,例如重新連線、記錄錯誤或終止腳本。 </p>
以上是如何在 Go 中優雅地處理死掉的 RabbitMQ 連結?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。

本文討論了使用GO的“字符串”軟件包進行字符串操作,詳細介紹了共同的功能和最佳實踐,以提高效率並有效地處理Unicode。

本文詳細介紹了GO的“時間”包用於處理日期,時間和時區,包括獲得當前時間,創建特定時間,解析字符串以及測量經過的時間。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境