implement message delivery struct details

This commit is contained in:
vyzo 2020-03-09 13:07:37 +02:00
parent 7d862e2ee6
commit 38fbc7fa47
1 changed files with 45 additions and 3 deletions

View File

@ -148,7 +148,11 @@ type peerScore struct {
} }
type messageDeliveries struct { type messageDeliveries struct {
// TODO records map[string]*deliveryRecord
// queue for cleaning up old delivery records
head *deliveryEntry
tail *deliveryEntry
} }
type deliveryRecord struct { type deliveryRecord struct {
@ -157,6 +161,12 @@ type deliveryRecord struct {
peers map[peer.ID]struct{} peers map[peer.ID]struct{}
} }
type deliveryEntry struct {
id string
expire time.Time
next *deliveryEntry
}
// delivery record status // delivery record status
const ( const (
delivery_unknown = iota // we don't know (yet) if the message is valid delivery_unknown = iota // we don't know (yet) if the message is valid
@ -497,8 +507,40 @@ func (ps *peerScore) DuplicateMessage(msg *Message) {
// message delivery records // message delivery records
func (d *messageDeliveries) getRecord(id string) *deliveryRecord { func (d *messageDeliveries) getRecord(id string) *deliveryRecord {
// TODO rec, ok := d.records[id]
return nil if ok {
return rec
}
rec = &deliveryRecord{peers: make(map[peer.ID]struct{})}
d.records[id] = rec
entry := &deliveryEntry{id: id, expire: time.Now().Add(TimeCacheDuration)}
if d.tail != nil {
d.tail.next = entry
d.tail = entry
} else {
d.head = entry
d.tail = entry
}
return rec
}
func (d *messageDeliveries) gc() {
if d.head == nil {
return
}
now := time.Now()
for d.head != nil && now.After(d.head.expire) {
delete(d.records, d.head.id)
d.head = d.head.next
}
if d.head == nil {
d.tail = nil
}
} }
// utilities // utilities