summaryrefslogtreecommitdiffstats
path: root/src/peer.c
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-11-10 18:26:37 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-11-10 18:26:37 +0100
commit9bd967affaafb10e8262253bc746dac234f39443 (patch)
tree248c56773a1b71ee82f95545b23c86724487d2ef /src/peer.c
parent181deb932074de613e33f955978af579e52e1feb (diff)
downloadfastd-9bd967affaafb10e8262253bc746dac234f39443.tar
fastd-9bd967affaafb10e8262253bc746dac234f39443.zip
Use heap-based priority queue to schedule handshakes instead of a linked list
Diffstat (limited to 'src/peer.c')
-rw-r--r--src/peer.c21
1 files changed, 6 insertions, 15 deletions
diff --git a/src/peer.c b/src/peer.c
index 67ce50d..87df22d 100644
--- a/src/peer.c
+++ b/src/peer.c
@@ -265,17 +265,8 @@ void fastd_peer_reset_socket(fastd_peer_t *peer) {
void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay) {
fastd_peer_unschedule_handshake(peer);
- peer->next_handshake = ctx.now + delay;
-
- fastd_dlist_head_t *list;
- for (list = &ctx.handshake_queue; list->next; list = list->next) {
- fastd_peer_t *entry = container_of(list->next, fastd_peer_t, handshake_entry);
-
- if (entry->next_handshake > peer->next_handshake)
- break;
- }
-
- fastd_dlist_insert(list, &peer->handshake_entry);
+ peer->handshake_entry.value = ctx.now + delay;
+ fastd_pqueue_insert(&ctx.handshake_queue, &peer->handshake_entry);
}
/** Checks if the peer group \e group1 lies in \e group2 */
@@ -829,13 +820,13 @@ static void send_handshake(fastd_peer_t *peer, fastd_remote_t *next_remote) {
/** Sends a handshake to one peer, if a scheduled handshake is due */
void fastd_peer_handle_handshake_queue(void) {
- if (!ctx.handshake_queue.next)
+ if (!ctx.handshake_queue)
return;
-
- fastd_peer_t *peer = container_of(ctx.handshake_queue.next, fastd_peer_t, handshake_entry);
- if (!fastd_timed_out(peer->next_handshake))
+ if (!fastd_timed_out(ctx.handshake_queue->value))
return;
+ fastd_peer_t *peer = container_of(ctx.handshake_queue, fastd_peer_t, handshake_entry);
+
fastd_peer_schedule_handshake_default(peer);
if (!fastd_peer_may_connect(peer)) {