Skip to content

Commit

Permalink
The libev example handler can now be given a priority
Browse files Browse the repository at this point in the history
  • Loading branch information
EmielBruijntjes committed Apr 12, 2023
1 parent cf99cc9 commit 40456bb
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions include/amqpcpp/libev.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ class LibEvHandler : public TcpHandler
* @param object The object being watched
* @param fd The filedescriptor being watched
* @param events The events that should be monitored
* @param priority The priority for the watcher
*/
Watcher(struct ev_loop *loop, Watchable *object, int fd, int events) : _loop(loop)
Watcher(struct ev_loop *loop, Watchable *object, int fd, int events, int priority) : _loop(loop)
{
// initialize the libev structure
ev_io_init(&_io, callback, fd, events);

// install a priority
ev_set_priority(&_io, priority);

// store the object in the data "void*"
_io.data = object;
Expand Down Expand Up @@ -300,8 +304,9 @@ class LibEvHandler : public TcpHandler
* @param loop The current event loop
* @param connection The TCP connection
* @param timeout Connect timeout
* @param priority The priority (high priorities are invoked earlier
*/
Wrapper(struct ev_loop *loop, AMQP::TcpConnection *connection, uint16_t timeout = 60) :
Wrapper(struct ev_loop *loop, AMQP::TcpConnection *connection, uint16_t timeout, int priority) :
_connection(connection),
_loop(loop),
_next(0.0),
Expand All @@ -314,6 +319,9 @@ class LibEvHandler : public TcpHandler
// initialize the libev structure, it should expire after the connection timeout
ev_timer_init(&_timer, callback, timeout, 0.0);

// set a priority
ev_set_priority(&_timer, priority);

// start the timer (this is the time that we reserve for setting up the connection)
ev_timer_start(_loop, &_timer);

Expand Down Expand Up @@ -422,7 +430,7 @@ class LibEvHandler : public TcpHandler
Watchable *watchable = this;

// we should monitor a new filedescriptor
_watchers.emplace_back(_loop, watchable, fd, events);
_watchers.emplace_back(_loop, watchable, fd, events, ev_priority(&_timer));
}
}
};
Expand All @@ -438,6 +446,12 @@ class LibEvHandler : public TcpHandler
* @var std::list
*/
std::list<Wrapper> _wrappers;

/**
* The priority that watchers should have (higher prio means libev gives more prio to this eveht)
* @var int
*/
int _priority;

/**
* Lookup a connection-wrapper, when the wrapper is not found, we construct one
Expand All @@ -454,7 +468,7 @@ class LibEvHandler : public TcpHandler
}

// add to the wrappers
_wrappers.emplace_back(_loop, connection);
_wrappers.emplace_back(_loop, connection, 60, _priority);

// done
return _wrappers.back();
Expand Down Expand Up @@ -500,9 +514,10 @@ class LibEvHandler : public TcpHandler
public:
/**
* Constructor
* @param loop The event loop to wrap
* @param loop The event loop to wrap
* @param priority The libev priority (higher priorities are invoked earlier)
*/
LibEvHandler(struct ev_loop *loop) : _loop(loop) {}
LibEvHandler(struct ev_loop *loop, int priority = 0) : _loop(loop), _priority(priority) {}

/**
* Destructor
Expand Down

0 comments on commit 40456bb

Please sign in to comment.