forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_submission_list.go
123 lines (100 loc) · 2.66 KB
/
open_submission_list.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
package txsub
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-errors/errors"
"github.com/stellar/go/support/log"
)
// NewDefaultSubmissionList returns a list that manages open submissions purely
// in memory.
func NewDefaultSubmissionList() OpenSubmissionList {
return &submissionList{
submissions: map[string]*openSubmission{},
log: log.DefaultLogger.WithField("service", "txsub.submissionList"),
}
}
// openSubmission tracks a slice of channels that should be emitted to when we
// know the result for the transactions with the provided hash
type openSubmission struct {
Hash string
SubmittedAt time.Time
Listeners []Listener
}
type submissionList struct {
sync.Mutex
submissions map[string]*openSubmission // hash => `*openSubmission`
log *log.Entry
}
func (s *submissionList) Add(ctx context.Context, hash string, l Listener) error {
s.Lock()
defer s.Unlock()
if cap(l) == 0 {
panic("Unbuffered listener cannot be added to OpenSubmissionList")
}
if len(hash) != 64 {
return errors.New("Unexpected transaction hash length: must be 64 hex characters")
}
os, ok := s.submissions[hash]
if !ok {
os = &openSubmission{
Hash: hash,
SubmittedAt: time.Now(),
Listeners: []Listener{},
}
s.submissions[hash] = os
s.log.WithField("hash", hash).Info("Created a new submission for a transaction")
} else {
s.log.WithField("hash", hash).Info("Adding listener to existing submission")
}
os.Listeners = append(os.Listeners, l)
return nil
}
func (s *submissionList) Finish(ctx context.Context, hash string, r Result) error {
s.Lock()
defer s.Unlock()
os, ok := s.submissions[hash]
if !ok {
return nil
}
s.log.WithFields(log.F{
"hash": hash,
"listeners": len(os.Listeners),
"result": fmt.Sprintf("%+v", r),
}).Info("Sending submission result to listeners")
for _, l := range os.Listeners {
l <- r
close(l)
}
delete(s.submissions, hash)
return nil
}
func (s *submissionList) Clean(ctx context.Context, maxAge time.Duration) (int, error) {
s.Lock()
defer s.Unlock()
for _, os := range s.submissions {
if time.Since(os.SubmittedAt) > maxAge {
s.log.WithFields(log.F{
"hash": os.Hash,
"listeners": len(os.Listeners),
}).Warn("Cleared submission due to timeout")
r := Result{Err: ErrTimeout}
delete(s.submissions, os.Hash)
for _, l := range os.Listeners {
l <- r
close(l)
}
}
}
return len(s.submissions), nil
}
func (s *submissionList) Pending(ctx context.Context) []string {
s.Lock()
defer s.Unlock()
results := make([]string, 0, len(s.submissions))
for hash := range s.submissions {
results = append(results, hash)
}
return results
}