-
Notifications
You must be signed in to change notification settings - Fork 12
/
queue_inprocessing.go
105 lines (95 loc) · 2.3 KB
/
queue_inprocessing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package queue
import (
"os"
"sync"
)
type storageProcessing interface {
FreeRecord(idx StorageIdx) error
UnlockRecord(idx StorageIdx) error
description() string
}
type inProcessingList struct {
list []*QueueItem
cnt uint16
}
type inProcessingPerWorker struct {
sync.RWMutex
maxPerWorker uint16
workers map[WorkerID]*inProcessingList
q *Queue
}
func createInProcessing(q *Queue, workerCount, maxPerWorker uint16) *inProcessingPerWorker {
return &inProcessingPerWorker{
maxPerWorker: maxPerWorker,
workers: make(map[WorkerID]*inProcessingList, workerCount),
q: q,
}
}
func (ipw *inProcessingPerWorker) addToList(worker WorkerID) []*QueueItem {
ipw.RLock()
list, ok := ipw.workers[worker]
ipw.RUnlock()
if !ok {
list = &inProcessingList{
list: make([]*QueueItem, ipw.maxPerWorker),
}
ipw.Lock()
ipw.workers[worker] = list
ipw.Unlock()
}
if list.cnt == ipw.maxPerWorker {
return nil
}
list.cnt++
return list.list[list.cnt-1 : list.cnt]
}
func (ipw *inProcessingPerWorker) decrementList(worker WorkerID) {
ipw.RLock()
list, ok := ipw.workers[worker]
ipw.RUnlock()
if !ok {
return
}
list.cnt--
}
func (ipw *inProcessingPerWorker) processList(worker WorkerID, isOk bool) {
ipw.Lock()
list, ok := ipw.workers[worker]
ipw.Unlock()
if !ok {
ipw.q.log.Trace("[Q:%s] !!!Not records for (%d)", ipw.q.name, worker)
return
}
if list.cnt == 0 {
ipw.q.log.Trace("[Q:%s] !!!!Not records for (%d)", ipw.q.name, worker)
}
for i := uint16(0); i < list.cnt; i++ {
file, isFile := list.list[i].Stream.(*os.File)
if isFile {
file.Close()
}
list.list[i].Stream = nil
if isOk {
ipw.q.log.Trace("[Q:%s:%d] Delete message from %s", ipw.q.name, list.list[i].ID, list.list[i].storage.description())
list.list[i].storage.FreeRecord(list.list[i].idx)
} else {
ipw.q.log.Trace("[Q:%s:%d] Mark message in %s as faulty", ipw.q.name, list.list[i].ID, list.list[i].storage.description())
list.list[i].storage.UnlockRecord(list.list[i].idx)
}
}
list.cnt = 0
}
func (ipw *inProcessingPerWorker) messagesInProcess(worker WorkerID) uint16 {
ipw.RLock()
list, ok := ipw.workers[worker]
ipw.RUnlock()
if !ok {
return 0
}
return list.cnt
}
func (ipw *inProcessingPerWorker) delete(worker WorkerID) {
ipw.Lock()
delete(ipw.workers, worker)
ipw.Unlock()
}