Skip to content

Instantly share code, notes, and snippets.

@eiri
Last active April 19, 2020 21:51
Show Gist options
  • Save eiri/5c166c1fef95e41835fd02df81f83c90 to your computer and use it in GitHub Desktop.
Save eiri/5c166c1fef95e41835fd02df81f83c90 to your computer and use it in GitHub Desktop.
Aegis key cache flow chart
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/raphael/2.2.7/raphael.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-sequence-diagrams/1.0.6/sequence-diagram-min.js"></script>
</head>
<body>
<div id="diagram"></div>
<script>
var request = new XMLHttpRequest();
request.open('GET', 'sequence.txt', false);
request.send();
var sequence = request.responseText;
var diagram = Diagram.parse(sequence);
diagram.drawSVG("diagram", {theme: 'simple'});
</script>
</html>
Title: Aegis key cache flow chart
participant aegis.erl
participant aegis_key_cache.erl
participant ets cache
participant Openers
participant Unwrappers
participant Waiters
participant worker process
participant aegis_key_manager.erl
Note over aegis.erl: Create Db
aegis.erl->aegis_key_cache.erl: call {get_wrapped_key, Db}
aegis_key_cache.erl->worker process: spawn_monitor get_wrapped_key(Db)
aegis_key_cache.erl-->Openers: store {Ref, From}
aegis_key_cache.erl->aegis.erl: noreply
worker process->aegis_key_manager.erl: key_wrap(Db)
aegis_key_manager.erl->worker process: {ok, DbKey, WrappedKey}
worker process->aegis_key_cache.erl: DOWN {key, {ok, DbKey, WrappedKey}}
aegis_key_cache.erl-->ets cache: insert {WrappedKey, DbKey}
aegis_key_cache.erl-->Openers: take(Ref)
Openers-->aegis_key_cache.erl: {Ref, From}
aegis_key_cache.erl->aegis.erl: gen_server:reply(From, WrappedKey)
# Note over aegis.erl: Open Db
# aegis.erl->aegis_key_cache.erl: call {maybe_rewrap_key, Db}
# aegis_key_cache.erl->worker process: spawn_monitor unwrap_key(From, Db)
# aegis_key_cache.erl-->Openers: store {Ref, From}
# aegis_key_cache.erl-->Unwrappers: store {WrappedKey, Ref}
# aegis_key_cache.erl->aegis.erl: noreply
# worker process->aegis_key_manager.erl: key_unwrap(Db)
# aegis_key_manager.erl->worker process: {ok, DbKey, WrappedKey}
# worker process->aegis_key_cache.erl: DOWN {key, {ok, DbKey, WrappedKey}}
# Unwrappers-->aegis_key_cache.erl: take {WrappedKey, Ref} /confirm source/
# aegis_key_cache.erl-->ets cache: insert {WrappedKey, DbKey}
# Openers-->aegis_key_cache.erl: take {Ref, From}
# aegis_key_cache.erl->aegis.erl: gen_server:reply(From, WrappedKey)
Note over aegis.erl: Encrypt key in cache
aegis.erl->aegis_key_cache.erl: call {encrypt, Db,..}
aegis_key_cache.erl-->ets cache: lookup(WrappedKey)
ets cache-->aegis_key_cache.erl: {ok, DbKey}
aegis_key_cache.erl->worker process: spawn do_encrypt(From, DbKey, Db,..)
aegis_key_cache.erl->aegis.erl: noreply
Note over worker process: aegis:encrypt(DbKey,..)
worker process->aegis.erl: gen_server:reply(From, <<1:8...>>)
Note over aegis.erl: Encrypt key not in cache
aegis.erl->aegis_key_cache.erl: call {encrypt, Db,..}
aegis_key_cache.erl-->ets cache: lookup(WrappedKey)
ets cache-->aegis_key_cache.erl: not_found
aegis_key_cache.erl-->Unwrappers: is_key(WrappedKey) /check if unwrap in flight/
Unwrappers-->aegis_key_cache.erl: false
aegis_key_cache.erl->worker process: spawn_monitor unwrap_key(Db)
aegis_key_cache.erl-->Unwrappers: store {WrappedKey, Ref}
aegis_key_cache.erl-->Waiters: append {WrappedKey, {from => From, action=>...}}
aegis_key_cache.erl->aegis.erl: noreply
worker process->aegis_key_manager.erl: key_unwrap(Db)
aegis_key_manager.erl->worker process: {ok, DbKey, WrappedKey}
worker process->aegis_key_cache.erl: DOWN {key, {ok, DbKey, WrappedKey}}
aegis_key_cache.erl-->Unwrappers: take(WrappedKey)
Unwrappers-->aegis_key_cache.erl: {WrappedKey, Ref} /check if Ref match/
aegis_key_cache.erl-->ets cache: insert {WrappedKey, DbKey}
aegis_key_cache.erl-->Waiters: take(WrappedKey)
Waiters-->aegis_key_cache.erl: {WrappedKey, WaitList}
aegis_key_cache.erl->worker process: foreach spawn do_encrypt(From, DbKey, Db,..)
Note over worker process: aegis:encrypt(DbKey,..)
worker process->aegis.erl: gen_server:reply(From, <<1:8...>>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment