Skip to content

Commit

Permalink
channel: Store hashed scheme urls in context
Browse files Browse the repository at this point in the history
  • Loading branch information
shramov committed Nov 16, 2023
1 parent e9fa798 commit 2db9979
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
30 changes: 23 additions & 7 deletions python/test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tll.channel as C
from tll.config import Config
from tll.error import TLLError
from tll.scheme import Scheme
from tll.stat import Method, Unit
from tll.test_util import Accum, ports
from tll.processor import Loop
Expand Down Expand Up @@ -35,17 +36,32 @@ def test_defaults():
del c
del defaults['mem.size']

def test_context_scheme():
c = ctx.Channel('null://;name=null;scheme=yamls://{}')
def test_context_scheme(context):
c = context.Channel('null://;name=null;scheme=yamls://{}')
c.open()
s1 = ctx.scheme_load('yamls://{}')
s2 = ctx.scheme_load('channel://null')
c1 = ctx.Channel('null://;scheme=channel://null')
s1 = context.scheme_load('yamls://{}')
s2 = context.scheme_load('channel://null')
c1 = context.Channel('null://;scheme=channel://null')
c1.open()
assert c.scheme != None
assert c1.scheme != None
with pytest.raises(TLLError): ctx.scheme_load('channel://unknown')
with pytest.raises(TLLError): ctx.scheme_load('zzz://scheme')
with pytest.raises(TLLError): context.scheme_load('channel://unknown')
with pytest.raises(TLLError): context.scheme_load('zzz://scheme')

def test_context_scheme_hash(context):
SCHEME = 'yamls://[{name: Data}]'
s0 = Scheme(SCHEME)
assert [m.name for m in s0.messages] == ['Data']
try:
sha = s0.dump('sha256')
except:
pytest.skip('SHA256 not available for scheme')
return
with pytest.raises(TLLError): context.scheme_load(sha)
s1 = context.scheme_load(SCHEME)
s2 = context.scheme_load(sha)
assert [m.name for m in s1.messages] == ['Data']
assert [m.name for m in s2.messages] == ['Data']

def test_stat():
ctx = C.Context()
Expand Down
29 changes: 21 additions & 8 deletions src/channel/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,36 @@ struct tll_channel_context_t : public tll::util::refbase_t<tll_channel_context_t
return tll_scheme_ref(tll_channel_scheme(c, 0));
}

if (!cache)
auto hashproto = (url.substr(0, 6 + 3) == "sha256://");

if (!cache && !hashproto)
return Scheme::load(url);

{
std::shared_lock<std::shared_mutex> lock(scheme_cache_lock);
auto it = scheme_cache.find(url);
if (it != scheme_cache.end())
return tll_scheme_ref(it->second.get());
return it->second->ref();
}

auto s = Scheme::load(url);
if (!s) return nullptr;
if (hashproto)
return _log.fail(nullptr, "Hashed scheme '{}' not found in the cache", url);

auto result = Scheme::load(url);
if (!result) return nullptr;

std::unique_lock<std::shared_mutex> lock(scheme_cache_lock);
if (!scheme_cache.insert({std::string(url), scheme::SchemePtr {s}}).second)
return s;
return tll_scheme_ref(s);
{
std::unique_lock<std::shared_mutex> lock(scheme_cache_lock);
if (!scheme_cache.insert({std::string(url), scheme::SchemePtr {result->ref()}}).second)
return result;
auto hash = tll_scheme_dump(result, "sha256");
if (hash) {
_log.debug("Register scheme hash '{}'", hash);
scheme_cache.insert({std::string(hash), scheme::SchemePtr {result->ref()}});
free(hash);
}
}
return result;
}
};

Expand Down

0 comments on commit 2db9979

Please sign in to comment.