Skip to content

Commit f826d60

Browse files
committed
Fix issue in RetrieveObjects when some of the objects is not found.
When some of the objects was not found, RetrieveObjects stopped sending more objects to outCh until all objects have been retrieved. The retrieved objects were accumulated in the priority queue, and therefore the program worked as expected, but the queue could grow very large.
1 parent d889594 commit f826d60

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

utils/client.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import (
1717
"container/heap"
1818
"errors"
1919
"fmt"
20-
"os"
21-
"sync"
22-
2320
vt "github.com/VirusTotal/vt-go"
2421
"github.com/spf13/viper"
22+
"os"
23+
"sync"
2524
)
2625

2726
// APIClient represents a VirusTotal API client.
@@ -79,7 +78,7 @@ func (c *APIClient) RetrieveObjects(endpoint string, args []string, outCh chan *
7978
objCh <- PQueueNode{Priority: order, Data: obj}
8079
} else {
8180
if apiErr, ok := err.(vt.Error); ok && apiErr.Code == "NotFoundError" {
82-
errCh <- err
81+
objCh <- PQueueNode{Priority: order, Data: err}
8382
} else {
8483
fmt.Fprintln(os.Stderr, err)
8584
os.Exit(1)
@@ -103,14 +102,23 @@ func (c *APIClient) RetrieveObjects(endpoint string, args []string, outCh chan *
103102
// it can be sent to outCh and removed from the queue, if not, we keep
104103
// pushing objects into the queue.
105104
if h[0].Priority == order {
106-
outCh <- h[0].Data.(*vt.Object)
105+
if obj, ok := h[0].Data.(*vt.Object); ok {
106+
outCh <- obj
107+
} else {
108+
errCh <- h[0].Data.(error)
109+
}
107110
heap.Pop(&h)
108111
order++
109112
}
110113
}
111114
// Send to outCh any object remaining in the queue
112115
for h.Len() > 0 {
113-
outCh <- heap.Pop(&h).(PQueueNode).Data.(*vt.Object)
116+
item := heap.Pop(&h).(PQueueNode).Data
117+
if obj, ok := item.(*vt.Object); ok {
118+
outCh <- obj
119+
} else {
120+
errCh <- item.(error)
121+
}
114122
}
115123
outWg.Done()
116124
}()

0 commit comments

Comments
 (0)