Compare commits

...

4 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 42c6b111c8
Migrate from libsnooze (#8)
* Dub

- Removed `libsnooze` dependency

* Queue

- Removed `libsnooze` imports

* Queue

- Added mutex+condition variable

* Queue

- Removed old `ensure()` call

* Queue

- Switched one thing over to mutex+condvar

* Queue

- Switched to using condition variable
- Added configurable slumber interval
2023-10-01 20:51:08 +02:00
Tristan B. Velloza Kildaire 761ddb2d1e Merge branch 'master' into nextgen 2023-10-01 19:14:57 +02:00
Tristan B. Velloza Kildaire a23017a747 README
- Added badges
2023-10-01 19:14:16 +02:00
Tristan B. Velloza Kildaire 459f4a8709 Pipelines
- Added code coverage
2023-10-01 19:10:57 +02:00
4 changed files with 81 additions and 57 deletions

View File

@ -6,9 +6,9 @@ name: D
on: on:
push: push:
branches: [ "master" ] branches: [ "**" ]
pull_request: pull_request:
branches: [ "master", "nextgen" ] branches: [ "**" ]
permissions: permissions:
contents: read contents: read
@ -22,6 +22,11 @@ jobs:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7 - uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
- name: Install Doveralls (code coverage tool)
run: |
dub fetch doveralls
sudo apt install libcurl4-openssl-dev
- name: 'Build & Test' - name: 'Build & Test'
run: | run: |
# Build the project, with its main file included, without unittests # Build the project, with its main file included, without unittests
@ -29,4 +34,8 @@ jobs:
# Build and run tests, as defined by `unittest` configuration # Build and run tests, as defined by `unittest` configuration
# In this mode, `mainSourceFile` is excluded and `version (unittest)` are included # In this mode, `mainSourceFile` is excluded and `version (unittest)` are included
# See https://dub.pm/package-format-json.html#configurations # See https://dub.pm/package-format-json.html#configurations
dub test --compiler=$DC dub test --compiler=$DC --coverage
- name: Coverage upload
run: |
dub run doveralls -- -t ${{secrets.COVERALLS_REPO_TOKEN}}

View File

@ -3,7 +3,8 @@
tristanable tristanable
=========== ===========
[![D](https://github.com/deavmi/tristanable/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/tristanable/actions/workflows/d.yml) [![D](https://github.com/deavmi/tristanable/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/tristanable/actions/workflows/d.yml) ![DUB](https://img.shields.io/dub/v/tristanable?color=%23c10000ff%20&style=flat-square) ![DUB](https://img.shields.io/dub/dt/tristanable?style=flat-square) ![DUB](https://img.shields.io/dub/l/tristanable?style=flat-square) [![Coverage Status](https://coveralls.io/repos/github/deavmi/tristanable/badge.svg?branch=master)](https://coveralls.io/github/deavmi/tristanable?branch=master)
**Tristanable** is a library for D-based libraries and applications that need a way to receive variable-length messages of different types (via a `Socket`) and place these messages into their own respectively tagged queues indicated by their _"type"_ or `id`. **Tristanable** is a library for D-based libraries and applications that need a way to receive variable-length messages of different types (via a `Socket`) and place these messages into their own respectively tagged queues indicated by their _"type"_ or `id`.

View File

@ -4,8 +4,7 @@
], ],
"copyright": "Copyright © 2023, Tristan B. Kildaire", "copyright": "Copyright © 2023, Tristan B. Kildaire",
"dependencies": { "dependencies": {
"bformat": ">=4.1.1", "bformat": ">=4.1.1"
"libsnooze": ">=1.3.0-beta"
}, },
"description": "Tristanable network message queuing framework", "description": "Tristanable network message queuing framework",
"homepage": "https://deavmi.assigned.network/projects/tristanable", "homepage": "https://deavmi.assigned.network/projects/tristanable",

View File

@ -3,14 +3,12 @@
*/ */
module tristanable.queue; module tristanable.queue;
// TODO: Examine the below import which seemingly fixes stuff for libsnooze
import libsnooze.clib;
import libsnooze;
import core.sync.mutex : Mutex; import core.sync.mutex : Mutex;
import core.sync.condition : Condition;
import core.sync.exception : SyncError;
import std.container.slist : SList; import std.container.slist : SList;
import tristanable.encoding; import tristanable.encoding;
import core.thread : dur; import core.time : Duration, dur;
import tristanable.exceptions; import tristanable.exceptions;
version(unittest) version(unittest)
@ -27,10 +25,15 @@ version(unittest)
public class Queue public class Queue
{ {
/** /**
* The libsnooze event used to sleep/wake * Mutex for the condition variable
* on queue events
*/ */
private Event event; private Mutex mutex;
/**
* The condition variable used to sleep/wake
* on queue of events
*/
private Condition signal;
/** /**
* The queue of messages * The queue of messages
@ -47,6 +50,15 @@ public class Queue
*/ */
private ulong queueID; private ulong queueID;
/**
* If a message is enqueued prior
* to us sleeping then we won't
* wake up and return for it.
*
* Therefore a periodic wakeup
* is required.
*/
private Duration wakeInterval;
/** /**
* Constructs a new Queue and immediately sets up the notification * Constructs a new Queue and immediately sets up the notification
@ -62,14 +74,37 @@ public class Queue
/* Initialize the queue lock */ /* Initialize the queue lock */
this.queueLock = new Mutex(); this.queueLock = new Mutex();
/* Initialize the event */ /* Initialize the condition variable */
this.event = new Event(); this.mutex = new Mutex();
this.signal = new Condition(this.mutex);
/* Set the queue id */ /* Set the queue id */
this.queueID = queueID; this.queueID = queueID;
/* Ensure pipe existence (see https://deavmi.assigned.network/git/deavmi/tristanable/issues/5) */ /* Set the slumber interval */
event.wait(dur!("seconds")(0)); this.wakeInterval = dur!("msecs")(50); // TODO: Decide on value
}
/**
* Returns the current wake interval
* for the queue checker
*
* Returns: the `Duration`
*/
public Duration getWakeInterval()
{
return this.wakeInterval;
}
/**
* Sets the wake up interval
*
* Params:
* interval = the new interval
*/
public void setWakeInterval(Duration interval)
{
this.wakeInterval = interval;
} }
/** /**
@ -111,9 +146,9 @@ public class Queue
try try
{ {
// TODO: Make us wait on the event (optional with a time-out) // TODO: Make us wait on the event (optional with a time-out)
event.notifyAll(); signal.notifyAll();
} }
catch(FatalException snozErr) catch(SyncError snozErr)
{ {
// Throw an exception on a fatal exception // Throw an exception on a fatal exception
throw new TristanableException(ErrorType.ENQUEUE_FAILED); throw new TristanableException(ErrorType.ENQUEUE_FAILED);
@ -151,45 +186,25 @@ public class Queue
/* Block till we dequeue a message successfully */ /* Block till we dequeue a message successfully */
while(dequeuedMessage is null) while(dequeuedMessage is null)
{ {
/** scope(exit)
* Call `wait()` and catch any interrupts
* in which case loop back and call `wait()`
* again
*/
while(true)
{ {
try // Unlock the mutex
{ this.mutex.unlock();
// TODO: Make us wait on the event (optional with a time-out)
event.wait();
}
catch(InterruptedException e)
{
version(unittest)
{
import std.stdio;
writeln("dequeue() had libsnooze wait() get interrupted!");
}
// Retry the wait()
continue;
}
catch(FatalException fatalErr)
{
version(unittest)
{
import std.stdio;
writeln("dequeue() had libsnooze wait() get FATALLY fail! Exception will now throw...");
}
// Throw an exception on a fatal exception
throw new TristanableException(ErrorType.DEQUEUE_FAILED);
}
// On successful wait() wake-up exit this wait()-retry loop
break;
} }
// Lock the mutex
this.mutex.lock();
try
{
this.signal.wait(this.wakeInterval);
}
catch(SyncError e)
{
// Throw an exception on a fatal exception
throw new TristanableException(ErrorType.DEQUEUE_FAILED);
}
/* Lock the item queue */ /* Lock the item queue */
queueLock.lock(); queueLock.lock();