forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_store.go
124 lines (103 loc) · 2.87 KB
/
backup_store.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package quickfix
import (
"fmt"
)
const (
OperationSetNextSenderMsgSeqNum int = iota + 1
OperationSetNextTargetMsgSeqNum
OperationSaveMessage
OperationReset
)
type BackupMessage struct {
Operation int
SeqNum int
Msg []byte
}
type backupStoreFactory struct {
messagesQueue chan *BackupMessage
backupFactory MessageStoreFactory
}
type backupStore struct {
messagesQueue chan *BackupMessage
store MessageStore
}
func NewBackupStoreFactory(messagesQueue chan *BackupMessage, backupFactory MessageStoreFactory) *backupStoreFactory {
return &backupStoreFactory{messagesQueue: messagesQueue, backupFactory: backupFactory}
}
func (f backupStoreFactory) Create(sessionID SessionID) (msgStore *backupStore, err error) {
backupStore, err := f.backupFactory.Create(sessionID)
if err != nil {
return nil, err
}
return newBackupStore(backupStore, f.messagesQueue), nil
}
func newBackupStore(store MessageStore, messagesQueue chan *BackupMessage) *backupStore {
backup := &backupStore{messagesQueue: messagesQueue, store: store}
backup.start()
return backup
}
func (s *backupStore) start() {
if s == nil {
return
}
go func() {
for message := range s.messagesQueue {
switch message.Operation {
case OperationSetNextSenderMsgSeqNum:
if err := s.store.SetNextSenderMsgSeqNum(message.SeqNum); err != nil {
}
case OperationSetNextTargetMsgSeqNum:
if err := s.store.SetNextTargetMsgSeqNum(message.SeqNum); err != nil {
}
case OperationSaveMessage:
if err := s.store.SaveMessage(message.SeqNum, message.Msg); err != nil {
}
case OperationReset:
if err := s.store.Reset(); err != nil {
}
default:
fmt.Printf("backup store: unsupported operation(%v)\n", message.Operation)
}
}
}()
}
func (s *backupStore) SetNextSenderMsgSeqNum(next int) {
if s == nil {
return
}
select {
case s.messagesQueue <- &BackupMessage{Operation: OperationSetNextSenderMsgSeqNum, SeqNum: next}:
default:
fmt.Println("encountering a large amount of traffic, drop the SetNextSenderMsgSeqNum operation")
}
}
func (s *backupStore) SetNextTargetMsgSeqNum(next int) {
if s == nil {
return
}
select {
case s.messagesQueue <- &BackupMessage{Operation: OperationSetNextTargetMsgSeqNum, SeqNum: next}:
default:
fmt.Println("encountering a large amount of traffic, drop the SetNextTargetMsgSeqNum operation")
}
}
func (s *backupStore) SaveMessage(seqNum int, msg []byte) {
if s == nil {
return
}
select {
case s.messagesQueue <- &BackupMessage{Operation: OperationSaveMessage, SeqNum: seqNum, Msg: msg}:
default:
fmt.Println("encountering a large amount of traffic, drop the SaveMessage operation")
}
}
func (s *backupStore) Reset() {
if s == nil {
return
}
select {
case s.messagesQueue <- &BackupMessage{Operation: OperationReset}:
default:
fmt.Println("encountering a large amount of traffic, drop the Reset operation")
}
}