Reputation: 3592
I'm working on a transport mechanism, where I am supposed to ignore OP_READ events on a socket, if the internal queue is already full.
SelectionKey next = it.next();
if (next.isReadable()) {
SocketChannel client = (SocketChannel) next.channel();
if (!innerqueueIsFull()) {
if (client.read(nextRead) == -1) {
break;
}
}
}
After writing a response for one of the pending requests, I want to go back and retry reading the data packets on the channel. How can I retry reading the request?
Upvotes: 0
Views: 75
Reputation: 310957
I'm working on a transport mechanism, where I am supposed to ignore OP_READ events on a socket, if the internal queue is already full.
The correct way to do that is to unregister interest in OP_READ for that channel, or for all channels if there is only one internal queue. Then you won't even get the isReadable()
events on the channel. When the queue clears, re-register OP_READ.
The effect will be to block the affected peers from sending.
How can I retry reading the request?
You don't have to retry anything this way. Just react to isReadable()
in the normal way. As a matter of fact you don't have to retry anything your way either, as you never did the read in the first place, so there is nothing to retry.
NB If client.read()
returns -1 it is utterly bizarre to break
. You should close the channel and continue. There's no reason not to process other events just because one client has disconnected.
Upvotes: 1