I have a Kafka Consumer with poll time mentioned like
kafkaConsumer.poll(polltimeinmilliseconds);
I would like to update the poll timer dynamically. Right now I set that to a static variable , and the poll time updates.
The problem is , the consumer waits for the old timer to complete. i.e if the old timer was 5 minutes, and if I update the timer to 10 ( dynamimcally ), it duly waits for the first 5 minutes before updating to 10 minutes interval.
How do I reset it immediately. i.e the timer should reset and set to 10 minutes immediately?
You can abort a long poll using the wakeup method.
Wakeup the consumer. This method is thread-safe and is useful in particular to abort a long poll. The thread which is blocking in an operation will throw WakeupException. If no thread is blocking in a method which can throw WakeupException, the next call to such a method will raise it instead.
Related
I wanted to set timeout for my sqlite connection to deal with database is locked error. I looked at the documentation for:
int sqlite3_busy_timeout(sqlite3*, int ms);
int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
Based on the documentation for sqlite3_busy_handler it looks like I only need to configure one of sqlite3_busy_handler or sqlite3_busy_timeout.
There can only be a single busy handler defined for each database connection. Setting a new busy handler clears any previously set handler. Note that calling sqlite3_busy_timeout() or evaluating PRAGMA busy_timeout=N will change the busy handler and thus clear any previously set busy handler.
Based on this, I have 3 questions:
If I configure sqlite3_busy_timeout, then how many retry attempt will it make? The documentation is not super clear about this. It just says will sleep multiple times.
The handler will sleep multiple times until at least "ms" milliseconds of sleeping have accumulated. After at least "ms" milliseconds of sleeping, the handler returns 0 which causes sqlite3_step() to return SQLITE_BUSY.
When can I set the value for sqlite3_busy_timeout? I have a wrapper similar to SQLite.swift/Connection.swift. Is it okay if I call sqlite3_busy_timeout after connect succeeds? The wrapper I linked to has 2 public configurable properties busyTimeout and busyHandler which configure sqlite3_busy_timeout and sqlite3_busy_handler respectively. So it seems like the timeout or busy handler can be set at any time and takes effect for the next attempt to access the database for a connection.
Will the same configuration of sqlite3_busy_timeout/sqlite3_busy_handler get used for all database accesses using a connection?
ROS2 timer adds elements to the callback queue. In a single threaded (C++) node, when a timer callback execution time exceeds the timer period, the spinner would be flooded with timer callbacks. This will cause a degradation in node responsivity to other (subscriber) callbacks.
Is there a way to make a ROS node to hold only one timer's callback instance in a queue at a time?
Hi I want to toggle LED with timing as follows
100ms ON1, 250ms Off1
1250ms ON2, 1500ms off2
and this cycle gets repeated (Both ON1 off1 and ON2 off2 pair repeats)
For this I have planned to utilize hardware timer with elapsing timings as 100,250,1250 and 1500 and this will repeat.
I am pretty new to the embedded field,
My questions are as follows
How to trigger this using a hardware timer? (How to enable and alternate the timings dynamically?)
How to set a call back function that toggles LED based on the timer elapse ?
Note : This is not a standalone code but an application will
Be running .From OS the callback will be triggered in the background so that other normal application is not affected during this process
Use the OS's software timer service. Create four software timers with the specified periods. The timers should be configured to count once and then stop each time they are started (i.e., they should be "one-shot", not "auto-reloading" nor "continuous" or whatever terminology your OS uses). Each software timer will have a unique callback function that you specify when you create the software timer.
From main, or wherever is appropriate, start the first timer once to get things started. Then start the second timer from the first timer's callback function. And start the third timer from the second timer's callback function. And so on until the last timer's callback function restarts the first timer and the circle of timers repeats.
Use timer interrupt for it.
You have the ready example here:
https://www.diymat.co.uk/arm-blinking-led-driver/
It does what you need and a bit more as well :)
I am using 2.17.6 version of camel AWSSQS component ( consumer) to consume messages from a FIFO queue.
When the exchange processing takes longer than visibility timeout, camel is spinning up a new thread with the same message ( but a new exchange). This is happening because I am using long polling, and looks like this component is built to have asynchronous behavior by default.
In my case, I want to move on to the next message only when the current message gets deleted after processing. ( as my requirement is FIFO)
If I increase the visibility timeout to a high value(ex: 15 mins), that solves the issue. but I do have cases where I will go for infinite retry, and in those cases I don't want to cross the set high Visibility timeout and have multiple threads starting thus changing the order of execution.
Please suggest, if there is a way not to receive the next message in the queue until I am done processing the current one.
Thanks,
Sowjanya.
In my program, I use a single thread pool to dispatch all my task, like timer task, non-blocking socket I/O, etc. A task is actually an callback function, which will be executed when specific event received.
The architecture is :
The main thread calls epoll() to harvest the I/O event, then dispatch the I/O callback to the thread pool.
The main thread also handle timer timeout, and dispatch the timeout callback to the thread pool
In an I/O callback, one timer task may be cancelled, depending on I/O processing result.
During one I/O callback is running, the coresponding socket is not monitored for further identical event.
Durning one timer callback is running, that timer task will temporarily removed from the timer task queue.
Here is the problem:
During thread A in the pool is running a timer callback T.
Thread B in the pool may be running another callback(registered for an socket I/O read event), after processing request received, thread B decide to delete the timer task T, but that timer task T is being executed by thread A right now.
I can add an lock for the timer task, but where should I place the lock? I can't place the lock object in the timer task structure, because when I decide to free the task object, I must have already acquired the lock, free and held lock, may lead to undefined behaviours:
pthread_mutex_lock(T->mutex);
free(T);
/*without a pthread_mutex_unlock(T->mutex);*/
And what happened if another thread is blocked on :
pthread_mutex_lock(T->mutex);
Without these problem being addressed, I can't continue my work.Please HELP me out!
Should I use separate thread pool for task of different types in my single process? Or just use single thread?
Any suggestion is appreciated!
You can use a global table of timers protected by its own mutex. The table does not actually need to be global but can belong to some collection, such as whatever owns all the things you are doing I/O on.
Then use this logic:
To create a timer:
Lock the global table.
Add the timer to the global table in state "pending".
Unlock the global table.
Schedule the timer with the thread pool.
To fire a timer:
Lock the global table.
Check the timer's state. If it's not "pending", delete the timer, unlock the table, and stop.
Change the timer's state to "firing".
Unlock the global table.
Perform the timer operation.
Lock the global table.
Remove the timer from the table.
Unlock the global table.
To cancel a timer:
Lock the global table.
Find the timer. If it's state is "pending", change it to "cancelled". Do not delete it.
Unlock the global table.
Can't you refernce count the tasks? When thread A in the pool is running a timer callback T you mark it (e.g. using interlocked increment). When it finishes you decrement the usage. It cannot be freed until the usage is zero.