Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 16b2219

Browse files
committed
Add timeout and retry to UDP version of protocol
1 parent 63ae96e commit 16b2219

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

Demo/rpc/rpc.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,23 +246,34 @@ def start_call(self, proc):
246246
p.pack_callheader(xid, self.prog, self.vers, proc, cred, verf)
247247

248248
def do_call(self, *rest):
249+
from select import select
249250
if len(rest) == 0:
250251
bufsize = 8192
251252
elif len(rest) > 1:
252253
raise TypeError, 'too many args'
253254
else:
254255
bufsize = rest[0] + 512
255256
call = self.packer.get_buf()
257+
timeout = 1
258+
count = 5
256259
self.sock.send(call)
257-
# XXX What about time-out and retry?
258-
reply = self.sock.recv(bufsize)
259-
u = self.unpacker
260-
u.reset(reply)
261-
xid, verf = u.unpack_replyheader()
262-
if xid <> self.lastxid:
263-
# XXX Should assume it's an old reply
264-
raise RuntimeError, 'wrong xid in reply ' + `xid` + \
265-
' instead of ' + `self.lastxid`
260+
while 1:
261+
r, w, x = select([self.sock], [], [], timeout)
262+
if self.sock not in r:
263+
count = count - 1
264+
if count < 0: raise RuntimeError, 'timeout'
265+
if timeout < 25: timeout = timeout *2
266+
print 'RESEND', timeout, count
267+
self.sock.send(call)
268+
continue
269+
reply = self.sock.recv(bufsize)
270+
u = self.unpacker
271+
u.reset(reply)
272+
xid, verf = u.unpack_replyheader()
273+
if xid <> self.lastxid:
274+
print 'BAD xid'
275+
continue
276+
break
266277

267278
def end_call(self):
268279
self.unpacker.done()

0 commit comments

Comments
 (0)