Initial release of queue package
This commit is contained in:
60
examples/example_dlq_test.go
Normal file
60
examples/example_dlq_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package queue_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"git.belvedersky.ru/belvedersky/queue"
|
||||
)
|
||||
|
||||
// Example_DLQ показывает, как использовать Dead Letter Queue
|
||||
// для перехвата задач, вызвавших панику.
|
||||
func Example_dlq() {
|
||||
type Task struct{ ID int }
|
||||
|
||||
mainQ := queue.NewQueue[Task](5)
|
||||
dlq := queue.NewQueue[Task](5)
|
||||
mainQ.SetDLQ(dlq)
|
||||
|
||||
var handled, failed int32
|
||||
|
||||
mainQ.Register(func(t Task) {
|
||||
if t.ID == 3 {
|
||||
panic("boom")
|
||||
}
|
||||
fmt.Printf("✅ handled %d\n", t.ID)
|
||||
atomic.AddInt32(&handled, 1)
|
||||
})
|
||||
|
||||
dlq.Register(func(t Task) {
|
||||
fmt.Printf("🚨 sent to DLQ: %d\n", t.ID)
|
||||
atomic.AddInt32(&failed, 1)
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
mainQ.HandleParallel(ctx, 1)
|
||||
dlq.HandleParallel(ctx, 1)
|
||||
|
||||
for i := range 5 {
|
||||
mainQ.Produce(Task{ID: i})
|
||||
}
|
||||
|
||||
mainQ.Close()
|
||||
mainQ.Wait()
|
||||
dlq.Close()
|
||||
dlq.Wait()
|
||||
|
||||
time.Sleep(50 * time.Millisecond) // на случай асинхронного вывода
|
||||
|
||||
fmt.Printf("Total handled: %d, failed: %d\n", handled, failed)
|
||||
|
||||
// Unordered output:
|
||||
// ✅ handled 0
|
||||
// ✅ handled 1
|
||||
// ✅ handled 2
|
||||
// ✅ handled 4
|
||||
// 🚨 sent to DLQ: 3
|
||||
// Total handled: 4, failed: 1
|
||||
}
|
||||
Reference in New Issue
Block a user