Skip to content

Commit

Permalink
Adapt to discussion in bytecodealliance/wasmtime-dotnet#90
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwille committed Feb 1, 2022
1 parent d490f51 commit ad1f6e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 43 deletions.
53 changes: 12 additions & 41 deletions spikes/HigherLevelApisSpike/IPolicyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@ namespace HigherLevelApisSpike;
public class Policy : IDisposable
{
private bool disposedValue;

internal Module? _module;
internal string _policyName;
private readonly IOpaSerializer _serde;

public OpaPolicy Opa { get; }

public Policy(OpaPolicy opaPolicy, Module module, string policyName, IOpaSerializer serde)
public Policy(OpaPolicy opaPolicy, IOpaSerializer serde)
{
Opa = opaPolicy;

_module = module;
_policyName = policyName;

_serde = serde;
}

Expand All @@ -40,16 +32,8 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)

// When disposing, we are not telling the PolicyFactory
Opa.Dispose();
if (null != _module) _module.Dispose();
_module = null;
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}
Expand Down Expand Up @@ -94,19 +78,19 @@ public string Serialize(object obj)
}

// Size of module cache is determined by implementation
public interface IModuleCache
public interface IOpaModuleCache
{
public Module? GetAndRemove(string policyName);
public Module? Get(string policyName);
public void Add(string policyName, Module module);
}

public class NoOpModuleCache : IModuleCache
public class NoOpModuleCache : IOpaModuleCache
{
public void Add(string policyName, Module module)
{
}

public Module? GetAndRemove(string policyName)
public Module? Get(string policyName)
{
return null;
}
Expand All @@ -116,47 +100,34 @@ public class PolicyFactory
{
private readonly IPolicyStore _store;
private readonly IOpaSerializer _serde;
private readonly IModuleCache _moduleCache;
private readonly IOpaModuleCache _moduleCache;

public PolicyFactory(IPolicyStore store, IModuleCache moduleCache, IOpaSerializer serde)
public PolicyFactory(IPolicyStore store, IOpaModuleCache moduleCache, IOpaSerializer serde)
{
_store = store;
_serde = serde;
_moduleCache = moduleCache;
}

// Idea from ArrayPool
public async Task<Policy> RentAsync(string policyName)
public async Task<Policy> GetAsync(string policyName)
{
using var opaRuntime = new OpaRuntime();

Module? module = null;
if (null == (module = _moduleCache.GetAndRemove(policyName)))
if (null == (module = _moduleCache.Get(policyName)))
{
// Non-cache case
var (wasmBytes, err) = await _store.LoadPolicyAsync(policyName);

// disposing is the duty of the consumer
module = opaRuntime.Load(policyName, wasmBytes);

_moduleCache.Add(policyName, module);
}

// disposing is the duty of the consumer
var opaPolicy = new OpaPolicy(opaRuntime, module);

return new Policy(opaPolicy, module, policyName, _serde);
}

// intentionally void because the cache if any is going to be on the local machine!
public void Return(Policy p)
{
var moduleToReturn = p._module;
if (null == moduleToReturn) return;

p._module = null;

var nameofPolicy = p._policyName;
_moduleCache.Add(nameofPolicy, moduleToReturn);

p.Dispose();
return new Policy(opaPolicy, _serde);
}
}
3 changes: 1 addition & 2 deletions spikes/HigherLevelApisSpike/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
new NoOpModuleCache(),
new DefaultOpaSerializer());

var policy = await factory.RentAsync("mysamplepolicy");
var policy = await factory.GetAsync("mysamplepolicy");

// if you want to be fancy use the string version on the root object
// policy.Opa.SetData("");
policy.SetData(new { blah = "blups" });

policy.Evaluate(new { myName = "" });

factory.Return(policy); // do not use policy after here, but return is optional re:IDisposable

0 comments on commit ad1f6e6

Please # to comment.