summaryrefslogtreecommitdiff
path: root/txnsync/msgorderingheap.go
blob: 69758216bb9b54aa1937778c2daf384c499bd836 (plain)
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
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

package txnsync

import (
	"container/heap"
	"errors"

	"github.com/algorand/go-deadlock"
)

var errHeapEmpty = errors.New("message ordering heap is empty")
var errHeapReachedCapacity = errors.New("message ordering heap reached capacity")
var errSequenceNumberMismatch = errors.New("sequence number mismatch")

const messageOrderingHeapLimit = 128

type messageHeapItem incomingMessage

type messageOrderingHeap struct {
	mu       deadlock.Mutex
	messages []messageHeapItem
}

// Push implements heap.Interface
func (p *messageOrderingHeap) Push(x interface{}) {
	entry := x.(messageHeapItem)
	p.messages = append(p.messages, entry)
}

// Pop implements heap.Interface
func (p *messageOrderingHeap) Pop() interface{} {
	end := len(p.messages) - 1
	res := p.messages[end]
	p.messages[end] = messageHeapItem{}
	p.messages = p.messages[0:end]
	return res
}

// Len implements heap.Interface
func (p *messageOrderingHeap) Len() int {
	return len(p.messages)
}

// Swap implements heap.Interface
func (p *messageOrderingHeap) Swap(i, j int) {
	p.messages[i], p.messages[j] = p.messages[j], p.messages[i]
}

// Less implements heap.Interface
func (p *messageOrderingHeap) Less(i, j int) bool {
	return p.messages[i].sequenceNumber < p.messages[j].sequenceNumber
}

func (p *messageOrderingHeap) enqueue(msg incomingMessage) error {
	p.mu.Lock()
	defer p.mu.Unlock()
	if len(p.messages) >= messageOrderingHeapLimit {
		return errHeapReachedCapacity
	}
	heap.Push(p, messageHeapItem(msg))
	return nil
}

func (p *messageOrderingHeap) popSequence(sequenceNumber uint64) (msg incomingMessage, heapSequenceNumber uint64, err error) {
	p.mu.Lock()
	defer p.mu.Unlock()
	if len(p.messages) == 0 {
		return incomingMessage{}, 0, errHeapEmpty
	}
	if p.messages[0].sequenceNumber != sequenceNumber {
		return incomingMessage{}, p.messages[0].sequenceNumber, errSequenceNumberMismatch
	}
	entry := heap.Pop(p).(messageHeapItem)
	return incomingMessage(entry), sequenceNumber, nil
}

func (p *messageOrderingHeap) pop() (msg incomingMessage, err error) {
	p.mu.Lock()
	defer p.mu.Unlock()
	if len(p.messages) == 0 {
		return incomingMessage{}, errHeapEmpty
	}
	entry := heap.Pop(p).(messageHeapItem)
	return incomingMessage(entry), nil
}