make header access synchronous for now
This commit is contained in:
parent
f09c18539b
commit
f109aa1f73
1 changed files with 19 additions and 23 deletions
|
@ -1,5 +1,4 @@
|
||||||
import os
|
import os
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from typing import Optional, Iterator, Tuple
|
from typing import Optional, Iterator, Tuple
|
||||||
|
@ -35,7 +34,6 @@ class BaseHeaders:
|
||||||
self.io = BytesIO()
|
self.io = BytesIO()
|
||||||
self.path = path
|
self.path = path
|
||||||
self._size: Optional[int] = None
|
self._size: Optional[int] = None
|
||||||
self._header_connect_lock = asyncio.Lock()
|
|
||||||
|
|
||||||
async def open(self):
|
async def open(self):
|
||||||
if self.path != ':memory:':
|
if self.path != ':memory:':
|
||||||
|
@ -98,12 +96,10 @@ class BaseHeaders:
|
||||||
async def connect(self, start: int, headers: bytes) -> int:
|
async def connect(self, start: int, headers: bytes) -> int:
|
||||||
added = 0
|
added = 0
|
||||||
bail = False
|
bail = False
|
||||||
loop = asyncio.get_running_loop()
|
|
||||||
async with self._header_connect_lock:
|
|
||||||
for height, chunk in self._iterate_chunks(start, headers):
|
for height, chunk in self._iterate_chunks(start, headers):
|
||||||
try:
|
try:
|
||||||
# validate_chunk() is CPU bound and reads previous chunks from file system
|
# validate_chunk() is CPU bound and reads previous chunks from file system
|
||||||
await loop.run_in_executor(None, self.validate_chunk, height, chunk)
|
self.validate_chunk(height, chunk)
|
||||||
except InvalidHeader as e:
|
except InvalidHeader as e:
|
||||||
bail = True
|
bail = True
|
||||||
chunk = chunk[:(height-e.height)*self.header_size]
|
chunk = chunk[:(height-e.height)*self.header_size]
|
||||||
|
@ -114,7 +110,7 @@ class BaseHeaders:
|
||||||
self.io.truncate()
|
self.io.truncate()
|
||||||
# .seek()/.write()/.truncate() might also .flush() when needed
|
# .seek()/.write()/.truncate() might also .flush() when needed
|
||||||
# the goal here is mainly to ensure we're definitely flush()'ing
|
# the goal here is mainly to ensure we're definitely flush()'ing
|
||||||
await loop.run_in_executor(None, self.io.flush)
|
self.io.flush()
|
||||||
self._size = None
|
self._size = None
|
||||||
added += written
|
added += written
|
||||||
if bail:
|
if bail:
|
||||||
|
|
Loading…
Reference in a new issue