2

I have a blocking queue where multiple writers are writing. I want to put a transaction control mechanism where not more than, say, 50 writers(or near) can write per second. Is there a way I can achieve it?

Edit 1: There is a similar requirement to be done with SocketChannel write method. That is to allow n writers to write per second. The value of n varies. I need not worry about that.

Another Edit: I'm thinking of using a Cyclic Barrier which I will release & reset every second. Am I going the right direction?

Abhash Upadhyaya
  • 717
  • 14
  • 34
  • What did you try so far? – npinti Nov 23 '15 at 07:29
  • @npinti, I could not think of anything to restrict the flow per second. Although, if I had an option to limit the flow at any time then I could have use semaphore. Any pointers to make it work for this time frame thing? – Abhash Upadhyaya Nov 23 '15 at 07:34
  • 1
    Just override the add method to sleep for 20ms. – user207421 Nov 23 '15 at 07:48
  • @EJP, what about cyclic barrier? Updated my question. – Abhash Upadhyaya Nov 23 '15 at 08:15
  • What about my suggestion? I don't see why I should be called upon to debate every *other* suggestion. – user207421 Nov 23 '15 at 09:10
  • @EJP, there was nothing wrong with your suggestion. It covers one part (for queue). There was another part(SocketChannel) for which I was expecting a supporting argument. – Abhash Upadhyaya Nov 23 '15 at 09:49
  • I don't see why I should be called upon to respond to edits that have appeared since my comment. If you are *now* asking me to respond about the SocketChannel part, which is not what you said in your first comment addressed to me, if I had a suggestion to offer I would have offered it. – user207421 Nov 23 '15 at 12:40
  • @EJP, I'm new to SO and didn't know that commenters are not notified of any change to the question. I'm not arguing. I'm thankful that you took time to respond with that overriding add method comment. It's just that I was expecting some POC or suggestions. – Abhash Upadhyaya Nov 23 '15 at 12:44

2 Answers2

0

I do not think thaT 1 semaphore for 50 writers is enough.

What you could do, would be to, for instance have a pool of tickets and a mechanism which issues them. The size of the pool would allow you to stipulate the maximum amount of writers and the mechanism which issues them would allow you to control the flow.

Essentially, the flow would be something like so:

  1. A writer requests a ticket. You can use a semaphore to vlock access for other threads while the wrjter is given a ticket.
  2. Once a writer has a ticket, it writes to the buffer.
  3. The writer returns the ticket (again, you could use a semaphore to control access to this section so that at any one point in time a writer is either requesting or returning s ticket).
npinti
  • 51,780
  • 5
  • 72
  • 96
0

Finally I mixed two solution to achieve what I wanted. I used bounded semaphores to limit the writers at any point of time along with timed cyclic barrier which helped me define the total writes/second.

Abhash Upadhyaya
  • 717
  • 14
  • 34