@@ -31,7 +31,7 @@ func (f *Fetch) GetAtxs(ctx context.Context, ids []types.ATXID, opts ...system.G
31
31
return nil
32
32
}
33
33
34
- options := system.GetAtxOpts {}
34
+ var options system.GetAtxOpts
35
35
for _ , opt := range opts {
36
36
opt (& options )
37
37
}
@@ -42,10 +42,17 @@ func (f *Fetch) GetAtxs(ctx context.Context, ids []types.ATXID, opts ...system.G
42
42
zap .Bool ("limiting" , ! options .LimitingOff ),
43
43
)
44
44
hashes := types .ATXIDsToHashes (ids )
45
- if options .LimitingOff {
46
- return f .getHashes (ctx , hashes , datastore .ATXDB , f .validators .atx .HandleMessage )
45
+ handler := f .validators .atx .HandleMessage
46
+ var ghOpts []getHashesOpt
47
+ if ! options .LimitingOff {
48
+ ghOpts = append (ghOpts , withLimiter (f .getAtxsLimiter ))
47
49
}
48
- return f .getHashes (ctx , hashes , datastore .ATXDB , f .validators .atx .HandleMessage , withLimiter (f .getAtxsLimiter ))
50
+ if options .Callback != nil {
51
+ ghOpts = append (ghOpts , withHashCallback (func (hash types.Hash32 , err error ) {
52
+ options .Callback (types .ATXID (hash ), err )
53
+ }))
54
+ }
55
+ return f .getHashes (ctx , hashes , datastore .ATXDB , handler , ghOpts ... )
49
56
}
50
57
51
58
type dataReceiver func (context.Context , types.Hash32 , p2p.Peer , []byte ) error
@@ -58,6 +65,12 @@ func withLimiter(l limiter) getHashesOpt {
58
65
}
59
66
}
60
67
68
+ func withHashCallback (callback func (types.Hash32 , error )) getHashesOpt {
69
+ return func (o * getHashesOpts ) {
70
+ o .callback = callback
71
+ }
72
+ }
73
+
61
74
func (f * Fetch ) getHashes (
62
75
ctx context.Context ,
63
76
hashes []types.Hash32 ,
@@ -66,7 +79,8 @@ func (f *Fetch) getHashes(
66
79
opts ... getHashesOpt ,
67
80
) error {
68
81
options := getHashesOpts {
69
- limiter : noLimit {},
82
+ limiter : noLimit {},
83
+ callback : func (types.Hash32 , error ) {},
70
84
}
71
85
for _ , opt := range opts {
72
86
opt (& options )
@@ -83,18 +97,26 @@ func (f *Fetch) getHashes(
83
97
for i , hash := range hashes {
84
98
if err := options .limiter .Acquire (ctx , 1 ); err != nil {
85
99
pendingMetric .Add (float64 (i - len (hashes )))
86
- return fmt .Errorf ("acquiring slot to get hash: %w" , err )
100
+ err = fmt .Errorf ("acquiring slot to get hash: %w" , err )
101
+ for _ , h := range hashes [i :] {
102
+ options .callback (h , err )
103
+ }
104
+ return err
87
105
}
88
106
p , err := f .getHash (ctx , hash , hint , receiver )
89
107
if err != nil {
90
108
options .limiter .Release (1 )
91
109
pendingMetric .Add (float64 (i - len (hashes )))
110
+ for _ , h := range hashes [i :] {
111
+ options .callback (h , err )
112
+ }
92
113
return err
93
114
}
94
115
if p == nil {
95
116
// data is available locally
96
117
options .limiter .Release (1 )
97
118
pendingMetric .Add (- 1 )
119
+ options .callback (hash , nil )
98
120
continue
99
121
}
100
122
@@ -103,6 +125,7 @@ func (f *Fetch) getHashes(
103
125
case <- ctx .Done ():
104
126
options .limiter .Release (1 )
105
127
pendingMetric .Add (- 1 )
128
+ options .callback (hash , ctx .Err ())
106
129
return ctx .Err ()
107
130
case <- p .completed :
108
131
options .limiter .Release (1 )
@@ -118,6 +141,7 @@ func (f *Fetch) getHashes(
118
141
bfailure .Add (hash , p .err )
119
142
mu .Unlock ()
120
143
}
144
+ options .callback (hash , p .err )
121
145
return nil
122
146
}
123
147
})
0 commit comments