mirror of https://gerrit.osmocom.org/libosmocore
write_queue: Enable updating max_length field
Dequeue and free any excess messages, in case the new queue length is shorter than the old. Related: OS#5774 Change-Id: Ibfe51a2faf29f8ae160a9c330c9af0d09b5a9002
This commit is contained in:
parent
d5c6651ae0
commit
ae07127f73
|
@ -50,6 +50,7 @@ void osmo_wqueue_init(struct osmo_wqueue *queue, int max_length);
|
|||
void osmo_wqueue_clear(struct osmo_wqueue *queue);
|
||||
int osmo_wqueue_enqueue(struct osmo_wqueue *queue, struct msgb *data);
|
||||
int osmo_wqueue_enqueue_quiet(struct osmo_wqueue *queue, struct msgb *data);
|
||||
size_t osmo_wqueue_set_maxlen(struct osmo_wqueue *queue, unsigned int len);
|
||||
int osmo_wqueue_bfd_cb(struct osmo_fd *fd, unsigned int what);
|
||||
|
||||
/*! @} */
|
||||
|
|
|
@ -565,6 +565,7 @@ osmo_wqueue_bfd_cb;
|
|||
osmo_wqueue_clear;
|
||||
osmo_wqueue_enqueue;
|
||||
osmo_wqueue_enqueue_quiet;
|
||||
osmo_wqueue_set_maxlen;
|
||||
osmo_wqueue_init;
|
||||
rate_ctr_add;
|
||||
rate_ctr_difference;
|
||||
|
|
|
@ -147,4 +147,24 @@ void osmo_wqueue_clear(struct osmo_wqueue *queue)
|
|||
queue->bfd.when &= ~OSMO_FD_WRITE;
|
||||
}
|
||||
|
||||
/* Update write queue length & drop excess messages.
|
||||
* \param[in] queue linked list header of message queue
|
||||
* \param[in] len new max. wqueue length
|
||||
* \returns Number of messages dropped.
|
||||
*
|
||||
* Messages beyond the new maximum message queue size will be dropped.
|
||||
*/
|
||||
size_t osmo_wqueue_set_maxlen(struct osmo_wqueue *queue, unsigned int len)
|
||||
{
|
||||
size_t dropped_msgs = 0;
|
||||
struct msgb *msg;
|
||||
queue->max_length = len;
|
||||
while (queue->current_length > queue->max_length) {
|
||||
msg = msgb_dequeue_count(&queue->msg_queue, &queue->current_length);
|
||||
msgb_free(msg);
|
||||
dropped_msgs++;
|
||||
}
|
||||
return dropped_msgs;
|
||||
}
|
||||
|
||||
/*! @} */
|
||||
|
|
|
@ -1,3 +1,14 @@
|
|||
/*
|
||||
* (C) 2023 by sysmocom - s.f.m.c. GmbH.
|
||||
* Authors: Holger Hans Peter Freyther
|
||||
* Alexander Rehbein
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
#include <osmocom/core/logging.h>
|
||||
#include <osmocom/core/utils.h>
|
||||
#include <osmocom/core/write_queue.h>
|
||||
|
@ -15,6 +26,7 @@ static void test_wqueue_limit(void)
|
|||
struct msgb *msg;
|
||||
struct osmo_wqueue wqueue;
|
||||
int rc;
|
||||
size_t dropped_msgs;
|
||||
|
||||
osmo_wqueue_init(&wqueue, 0);
|
||||
OSMO_ASSERT(wqueue.max_length == 0);
|
||||
|
@ -63,6 +75,46 @@ static void test_wqueue_limit(void)
|
|||
OSMO_ASSERT(wqueue.current_length == 2);
|
||||
msgb_free(msg);
|
||||
osmo_wqueue_clear(&wqueue);
|
||||
|
||||
/* Update limit */
|
||||
OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 5) == 0);
|
||||
OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 1) == 0);
|
||||
OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 4) == 0);
|
||||
|
||||
/* Add three, update limit to 1 */
|
||||
OSMO_ASSERT(wqueue.max_length == 4);
|
||||
msg = msgb_alloc(4096, "msg6");
|
||||
rc = osmo_wqueue_enqueue(&wqueue, msg);
|
||||
OSMO_ASSERT(rc == 0);
|
||||
OSMO_ASSERT(wqueue.current_length == 1);
|
||||
msg = msgb_alloc(4096, "msg7");
|
||||
rc = osmo_wqueue_enqueue(&wqueue, msg);
|
||||
OSMO_ASSERT(rc == 0);
|
||||
OSMO_ASSERT(wqueue.current_length == 2);
|
||||
msg = msgb_alloc(4096, "msg8");
|
||||
rc = osmo_wqueue_enqueue(&wqueue, msg);
|
||||
OSMO_ASSERT(wqueue.current_length == 3);
|
||||
dropped_msgs = osmo_wqueue_set_maxlen(&wqueue, 1);
|
||||
OSMO_ASSERT(dropped_msgs == 2);
|
||||
osmo_wqueue_clear(&wqueue);
|
||||
|
||||
/* Add three, reduce limit to 3 from 6 */
|
||||
OSMO_ASSERT(osmo_wqueue_set_maxlen(&wqueue, 6) == 0);
|
||||
OSMO_ASSERT(wqueue.max_length == 6);
|
||||
msg = msgb_alloc(4096, "msg9");
|
||||
rc = osmo_wqueue_enqueue(&wqueue, msg);
|
||||
OSMO_ASSERT(rc == 0);
|
||||
OSMO_ASSERT(wqueue.current_length == 1);
|
||||
msg = msgb_alloc(4096, "msg10");
|
||||
rc = osmo_wqueue_enqueue(&wqueue, msg);
|
||||
OSMO_ASSERT(rc == 0);
|
||||
OSMO_ASSERT(wqueue.current_length == 2);
|
||||
msg = msgb_alloc(4096, "msg11");
|
||||
rc = osmo_wqueue_enqueue(&wqueue, msg);
|
||||
OSMO_ASSERT(wqueue.current_length == 3);
|
||||
dropped_msgs = osmo_wqueue_set_maxlen(&wqueue, 3);
|
||||
OSMO_ASSERT(dropped_msgs == 0);
|
||||
osmo_wqueue_clear(&wqueue);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
|
Loading…
Reference in New Issue