Do I need a queue for a socket pair pattern?

184 views Asked by At

I'm writing a Tornado server that allows for communication between sockets in a kind of pairing pattern. This looks something like

def write_to_paired(self, message):
    if (paired == online):
        paired.write_message(message)

class typeASocket:
    def on_message(message):
        write_to_paired(message)

class typeBSocket:
    def on_message(message):
        write_to_paired(message)

These connections will only ever be 1:1. Do I need a queue to make this pattern scalable, or would that only be necessary in the N:1 producer-consumer case? For example write_to_paired would become

def write_to_paired(self, message):
    if (paired == online):
        self.queue.push(message)
        self.ping(paired) # Tell the paired socket that there's a message available
1

There are 1 answers

0
Ben Darnell On BEST ANSWER

This depends on how exactly write_message and on_message are defined. For many protocols implemented on top of the Tornado IOStream including Tornado's WebSocketHandler, a queue would not be necessary for a 1:1 pairing because IOStream.write has an internal FIFO buffer and so individual writes are atomic. However, if write_message internally performs multiple separate writes, you would need a queue to ensure that one doesn't start before the previous one has finished.

One reason why you may want to use a queue even when it's not required would be for flow control. IOStream's internal buffering only gives you crude control over the buffering behavior so a bounded queue might be better for exerting backpressure on your traffic sources.