fix fuse reconnect

This commit is contained in:
ed 2019-09-19 19:49:25 +00:00
parent ea8543ebb0
commit 01a8409d69

View file

@ -41,6 +41,10 @@ def log(msg):
print(msg[4:], end="") print(msg[4:], end="")
def get_tid():
return threading.current_thread().ident
class CacheNode(object): class CacheNode(object):
def __init__(self, name, data): def __init__(self, name, data):
self.name = name self.name = name
@ -71,11 +75,10 @@ class Gateway(object):
def quotep(self, path): def quotep(self, path):
# TODO: mojibake support # TODO: mojibake support
path = path.encode("utf-8", "ignore") path = path.encode("utf-8", "ignore")
path = path.replace(b" ", b"+") return quote(path, safe="/")
return quote(path)
def getconn(self, tid=None): def getconn(self, tid=None):
tid = tid or threading.current_thread().ident tid = tid or get_tid()
try: try:
return self.conns[tid] return self.conns[tid]
except: except:
@ -87,7 +90,7 @@ class Gateway(object):
return conn return conn
def closeconn(self, tid=None): def closeconn(self, tid=None):
tid = tid or threading.current_thread().ident tid = tid or get_tid()
try: try:
self.conns[tid].close() self.conns[tid].close()
del self.conns[tid] del self.conns[tid]
@ -95,26 +98,28 @@ class Gateway(object):
pass pass
def sendreq(self, *args, **kwargs): def sendreq(self, *args, **kwargs):
tid = threading.current_thread().ident tid = get_tid()
try: try:
c = self.getconn(tid) c = self.getconn(tid)
c.request(*list(args), **kwargs) c.request(*list(args), **kwargs)
return c.getresponse()
except: except:
self.closeconn(tid) self.closeconn(tid)
c = self.getconn(tid) c = self.getconn(tid)
c.request(*list(args), **kwargs) c.request(*list(args), **kwargs)
return c.getresponse()
return c
def listdir(self, path): def listdir(self, path):
web_path = "/" + "/".join([self.web_root, path]) web_path = "/" + "/".join([self.web_root, path])
c = self.sendreq("GET", self.quotep(web_path)) r = self.sendreq("GET", self.quotep(web_path))
r = c.getresponse()
if r.status != 200: if r.status != 200:
self.closeconn() self.closeconn()
raise Exception("http error {}".format(r.status)) raise Exception(
"http error {} reading dir {} in {:x}".format(
r.status, web_path, get_tid()
)
)
return self.parse_html(r) return self.parse_html(r)
@ -122,12 +127,14 @@ class Gateway(object):
web_path = "/" + "/".join([self.web_root, path]) web_path = "/" + "/".join([self.web_root, path])
hdr_range = "bytes={}-{}".format(ofs1, ofs2) hdr_range = "bytes={}-{}".format(ofs1, ofs2)
c = self.sendreq("GET", self.quotep(web_path), headers={"Range": hdr_range}) r = self.sendreq("GET", self.quotep(web_path), headers={"Range": hdr_range})
r = c.getresponse()
if r.status != http.client.PARTIAL_CONTENT: if r.status != http.client.PARTIAL_CONTENT:
self.closeconn() self.closeconn()
raise Exception("http error {}".format(r.status)) raise Exception(
"http error {} reading file {} range {} in {:x}".format(
r.status, web_path, hdr_range, get_tid()
)
)
return r.read() return r.read()