calibredb: Add a timeout option to control the timeout when connecting to the calibre server

This commit is contained in:
Kovid Goyal 2021-02-01 08:31:33 +05:30
parent ae2582659c
commit 07aabb488b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -84,6 +84,13 @@ def get_parser(usage):
' for your shell.').format( ' for your shell.').format(
'<stdin>', '<f:C:/path/to/file>' if iswindows else '<f:/path/to/file>') '<stdin>', '<f:C:/path/to/file>' if iswindows else '<f:/path/to/file>')
) )
go.add_option(
'--timeout',
type=float,
default=120,
help=_('The timeout, in seconds when connecting to a calibre library over the network. The default is'
' two minutes.')
)
return parser return parser
@ -122,6 +129,7 @@ class DBCtx(object):
def __init__(self, opts): def __init__(self, opts):
self.library_path = opts.library_path or prefs['library_path'] self.library_path = opts.library_path or prefs['library_path']
self.timeout = opts.timeout
self.url = None self.url = None
if self.library_path is None: if self.library_path is None:
raise SystemExit( raise SystemExit(
@ -194,7 +202,7 @@ class DBCtx(object):
rq = Request(url, data=msgpack_dumps(args), rq = Request(url, data=msgpack_dumps(args),
headers={'Accept': MSGPACK_MIME, 'Content-Type': MSGPACK_MIME}) headers={'Accept': MSGPACK_MIME, 'Content-Type': MSGPACK_MIME})
try: try:
res = self.br.open_novisit(rq) res = self.br.open_novisit(rq, timeout=self.timeout)
ans = msgpack_loads(res.read()) ans = msgpack_loads(res.read())
except HTTPError as err: except HTTPError as err:
self.interpret_http_error(err) self.interpret_http_error(err)
@ -209,7 +217,7 @@ class DBCtx(object):
from mechanize import HTTPError from mechanize import HTTPError
url = self.url + '/ajax/library-info' url = self.url + '/ajax/library-info'
try: try:
res = self.br.open_novisit(url) res = self.br.open_novisit(url, timeout=self.timeout)
ans = json.loads(res.read()) ans = json.loads(res.read())
except HTTPError as err: except HTTPError as err:
self.interpret_http_error(err) self.interpret_http_error(err)