Skip to content

Commit

Permalink
Correct caching of disk images
Browse files Browse the repository at this point in the history
The cachecontrol library can't cache large images [1], which we
work around here by using our own serializer to allow msgpage
to load big files.

It's quite likely this caching is the wrong way to go, and
missing some important details, but it is a useful way to
speed up the experimentation.

[1] psf/cachecontrol#200
  • Loading branch information
cdent committed Jan 21, 2019
1 parent 519d656 commit f0fa70b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,39 @@

import cachecontrol
from cachecontrol.caches import file_cache
from cachecontrol import serialize
import etcd3
import libvirt
import msgpack
import psutil
import requests
import yaml

from ecomp import clients
from ecomp import conf


# deal with size limitations in CacheControl
class MySerializer(serialize.Serializer):

def _loads_v4(self, request, data):
try:
cached = msgpack.loads(
data, encoding="utf-8", max_bin_len=2147483647)
except ValueError:
return

return self.prepare_response(request, cached)


KEY = '/hosts'
SLEEP = 1
COMPUTE_UUID = str(uuid.uuid4())
CLIENT = None
CACHED_SESSION = cachecontrol.CacheControl(
requests.Session(), cache=file_cache.FileCache('.web_cache'))
requests.Session(),
cache=file_cache.FileCache('.web_cache'),
serializer=MySerializer())


# default config
Expand Down Expand Up @@ -196,6 +214,7 @@ def _copy_image(source, instance, size):
os.unlink(source_file)
except FileNotFoundError:
pass
_print('Fetching image from %s' % source)
source_refresh = CACHED_SESSION.get(source, stream=True)
with open(source_file, 'wb') as sf:
shutil.copyfileobj(source_refresh.raw, sf)
Expand Down

0 comments on commit f0fa70b

Please sign in to comment.