Skip to content

Commit 0a542cc

Browse files
authored
fix : request selector (#233)
* fixed code * removed peer string * fixed lint * fixed lint * merged update request with existing for loop
1 parent 5d26b85 commit 0a542cc

4 files changed

Lines changed: 13 additions & 19 deletions

File tree

crawler/crawl/crawl.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ func (c *crawler) selectPendingAndExecute(ctx context.Context) {
132132
return
133133
}
134134
for _, req := range reqs {
135+
// update the pr, so it won't be picked again in 24 hours
136+
// We have to update the LastUpdated field here and cannot rety on the worker to update it
137+
// That is because the same request will be picked again when it is in worker.
138+
req.LastUpdated = time.Now().Unix()
139+
err = c.peerStore.Update(ctx, req)
140+
if err != nil {
141+
log.Error("error updating request", log.Ctx{"err": err})
142+
continue
143+
}
135144
select {
136145
case <-ctx.Done():
137146
log.Error("update selector stopped", log.Ctx{"err": ctx.Err()})
@@ -183,7 +192,6 @@ func (c *crawler) updatePeerInfo(ctx context.Context, peer *models.Peer) {
183192
}
184193
return
185194
}
186-
peer.LastUpdated = time.Now().Unix()
187195
err := c.peerStore.Update(ctx, peer)
188196
if err != nil {
189197
log.Error("failed on updating peerstore", log.Ctx{"err": err})

models/peer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
LodestarClient: {"lodestar", "js-libp2p"},
5151
NimbusClient: {"nimbus"},
5252
TrinityClient: {"trinity"},
53-
GrandineClient: {"grandine", "rust"},
53+
GrandineClient: {"grandine", "rust-libp2p"},
5454
}
5555
}
5656

store/peerstore/mongo/mongo.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import (
88
"context"
99
"encoding/hex"
1010
"errors"
11-
"eth2-crawler/graph/model"
12-
"eth2-crawler/store/peerstore"
1311
"fmt"
1412
"time"
1513

14+
"eth2-crawler/graph/model"
1615
"eth2-crawler/models"
17-
16+
"eth2-crawler/store/peerstore"
1817
"eth2-crawler/utils/config"
1918

2019
"github.com/libp2p/go-libp2p-core/peer"
@@ -30,18 +29,6 @@ type mongoStore struct {
3029
timeout time.Duration
3130
}
3231

33-
func (s *mongoStore) Upsert(ctx context.Context, peer *models.Peer) error {
34-
_, err := s.View(ctx, peer.ID)
35-
if err != nil {
36-
if errors.Is(err, peerstore.ErrPeerNotFound) {
37-
return s.Create(ctx, peer)
38-
}
39-
return err
40-
}
41-
42-
return s.Update(ctx, peer)
43-
}
44-
4532
func (s *mongoStore) Create(ctx context.Context, peer *models.Peer) error {
4633
_, err := s.View(ctx, peer.ID)
4734
if err != nil {
@@ -58,7 +45,7 @@ func (s *mongoStore) Update(ctx context.Context, peer *models.Peer) error {
5845
filter := bson.D{
5946
{Key: "_id", Value: peer.ID},
6047
}
61-
_, err := s.coll.UpdateOne(ctx, filter, bson.D{{Key: "$set", Value: peer}})
48+
_, err := s.coll.ReplaceOne(ctx, filter, peer)
6249
if err != nil {
6350
return err
6451
}

store/peerstore/store.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
type Provider interface {
1919
Create(ctx context.Context, peer *models.Peer) error
2020
Update(ctx context.Context, peer *models.Peer) error
21-
Upsert(ctx context.Context, peer *models.Peer) error
2221
View(ctx context.Context, peerID peer.ID) (*models.Peer, error)
2322
Delete(ctx context.Context, peer *models.Peer) error
2423
// Todo: accept filter and find options to get limited information

0 commit comments

Comments
 (0)