Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add cancellation token optional argument support #208

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions NetCasbin/Abstractions/Persist/IAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Casbin.Model;

namespace Casbin.Persist
{
public interface IAdapter
{
void LoadPolicy(IModel model);
public void LoadPolicy(IModel model, CancellationToken cancellationToken = default);

Task LoadPolicyAsync(IModel model);
public Task LoadPolicyAsync(IModel model, CancellationToken cancellationToken = default);

void SavePolicy(IModel model);
public void SavePolicy(IModel model, CancellationToken cancellationToken = default);

Task SavePolicyAsync(IModel model);
public Task SavePolicyAsync(IModel model, CancellationToken cancellationToken = default);

void AddPolicy(string section, string policyType, IEnumerable<string> rule);
public void AddPolicy(string section, string policyType, IEnumerable<string> rule, CancellationToken cancellationToken = default);

Task AddPolicyAsync(string section, string policyType, IEnumerable<string> rule);
public Task AddPolicyAsync(string section, string policyType, IEnumerable<string> rule, CancellationToken cancellationToken = default);

void AddPolicies(string section, string policyType, IEnumerable<IEnumerable<string>> rules);
public void AddPolicies(string section, string policyType, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default);

Task AddPoliciesAsync(string section, string policyType, IEnumerable<IEnumerable<string>> rules);
public Task AddPoliciesAsync(string section, string policyType, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default);

void RemovePolicy(string section, string policyType, IEnumerable<string> rule);
public void RemovePolicy(string section, string policyType, IEnumerable<string> rule, CancellationToken cancellationToken = default);

Task RemovePolicyAsync(string section, string policyType, IEnumerable<string> rule);
public Task RemovePolicyAsync(string section, string policyType, IEnumerable<string> rule, CancellationToken cancellationToken = default);

void RemovePolicies(string section, string policyType, IEnumerable<IEnumerable<string>> rules);
public void RemovePolicies(string section, string policyType, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default);

Task RemovePoliciesAsync(string section, string policyType, IEnumerable<IEnumerable<string>> rules);
public Task RemovePoliciesAsync(string section, string policyType, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default);

void RemoveFilteredPolicy(string section, string policyType, int fieldIndex, params string[] fieldValues);
public void RemoveFilteredPolicy(string section, string policyType, int fieldIndex, params string[] fieldValues);

Task RemoveFilteredPolicyAsync(string section, string policyType, int fieldIndex, params string[] fieldValues);
public Task RemoveFilteredPolicyAsync(string section, string policyType, int fieldIndex, params string[] fieldValues);
}
}
25 changes: 13 additions & 12 deletions NetCasbin/Adapter/File/FileAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.Persist;
Expand Down Expand Up @@ -34,7 +35,7 @@ public FileAdapter(Stream inputStream)
}
}

public void LoadPolicy(IModel model)
public void LoadPolicy(IModel model, CancellationToken cancellationToken = default)
{
if (!string.IsNullOrWhiteSpace(filePath))
{
Expand All @@ -51,7 +52,7 @@ public void LoadPolicy(IModel model)
}
}

public async Task LoadPolicyAsync(IModel model)
public async Task LoadPolicyAsync(IModel model, CancellationToken cancellationToken = default)
{
if (!string.IsNullOrWhiteSpace(filePath))
{
Expand All @@ -68,7 +69,7 @@ public async Task LoadPolicyAsync(IModel model)
}
}

public void SavePolicy(IModel model)
public void SavePolicy(IModel model, CancellationToken cancellationToken = default)
{
if (_byteArrayInputStream != null && _readOnly)
{
Expand All @@ -84,7 +85,7 @@ public void SavePolicy(IModel model)
SavePolicyFile(string.Join("\n", policy));
}

public async Task SavePolicyAsync(IModel model)
public async Task SavePolicyAsync(IModel model, CancellationToken cancellationToken = default)
{
if (_byteArrayInputStream != null && _readOnly)
{
Expand Down Expand Up @@ -158,14 +159,14 @@ private async Task SavePolicyFileAsync(string text)
}
}

public void AddPolicies(string sec, string ptype, IEnumerable<IEnumerable<string>> rules) => throw new NotImplementedException();
public Task AddPoliciesAsync(string sec, string ptype, IEnumerable<IEnumerable<string>> rules) => throw new NotImplementedException();
public void RemovePolicies(string sec, string ptype, IEnumerable<IEnumerable<string>> rules) => throw new NotImplementedException();
public Task RemovePoliciesAsync(string sec, string ptype, IEnumerable<IEnumerable<string>> rules) => throw new NotImplementedException();
public void AddPolicy(string sec, string ptype, IEnumerable<string> rule) => throw new NotImplementedException();
public Task AddPolicyAsync(string sec, string ptype, IEnumerable<string> rule) => throw new NotImplementedException();
public void RemovePolicy(string sec, string ptype, IEnumerable<string> rule) => throw new NotImplementedException();
public Task RemovePolicyAsync(string sec, string ptype, IEnumerable<string> rule) => throw new NotImplementedException();
public void AddPolicies(string sec, string ptype, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public Task AddPoliciesAsync(string sec, string ptype, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public void RemovePolicies(string sec, string ptype, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public Task RemovePoliciesAsync(string sec, string ptype, IEnumerable<IEnumerable<string>> rules, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public void AddPolicy(string sec, string ptype, IEnumerable<string> rule, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public Task AddPolicyAsync(string sec, string ptype, IEnumerable<string> rule, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public void RemovePolicy(string sec, string ptype, IEnumerable<string> rule, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public Task RemovePolicyAsync(string sec, string ptype, IEnumerable<string> rule, CancellationToken cancellationToken = default) => throw new NotImplementedException();
public void RemoveFilteredPolicy(string sec, string ptype, int fieldIndex, params string[] fieldValues) => throw new NotImplementedException();
public Task RemoveFilteredPolicyAsync(string sec, string ptype, int fieldIndex, params string[] fieldValues) => throw new NotImplementedException();
}
Expand Down
16 changes: 9 additions & 7 deletions NetCasbin/Model/DefaultPolicyManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Casbin.Persist;

Expand All @@ -18,6 +19,7 @@ public DefaultPolicyManager(IPolicy policy, IAdapter adapter = null)
}

public virtual bool IsSynchronized => false;
public CancellationToken CancellationToken { get; set; }
public bool AutoSave { get; set; }
public IAdapter Adapter { get; set; }
public bool HasAdapter => Adapter is null;
Expand Down Expand Up @@ -167,7 +169,7 @@ public bool AddPolicy(string section, string policyType, IEnumerable<string> rul

try
{
Adapter.AddPolicy(section, policyType, ruleArray);
Adapter.AddPolicy(section, policyType, ruleArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down Expand Up @@ -200,7 +202,7 @@ public bool AddPolicies(string section, string policyType, IEnumerable<IEnumerab

try
{
Adapter.AddPolicies(section, policyType, rulesArray);
Adapter.AddPolicies(section, policyType, rulesArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down Expand Up @@ -233,7 +235,7 @@ public bool RemovePolicy(string section, string policyType, IEnumerable<string>

try
{
Adapter.RemovePolicy(section, policyType, ruleArray);
Adapter.RemovePolicy(section, policyType, ruleArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down Expand Up @@ -266,7 +268,7 @@ public bool RemovePolicies(string section, string policyType, IEnumerable<IEnume

try
{
Adapter.RemovePolicies(section, policyType, rulesArray);
Adapter.RemovePolicies(section, policyType, rulesArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down Expand Up @@ -363,7 +365,7 @@ public async Task<bool> AddPoliciesAsync(string section, string policyType, IEnu

try
{
await Adapter.AddPoliciesAsync(section, policyType, rulesArray);
await Adapter.AddPoliciesAsync(section, policyType, rulesArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down Expand Up @@ -396,7 +398,7 @@ public async Task<bool> RemovePolicyAsync(string section, string policyType, IEn

try
{
await Adapter.RemovePolicyAsync(section, policyType, ruleArray);
await Adapter.RemovePolicyAsync(section, policyType, ruleArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down Expand Up @@ -429,7 +431,7 @@ public async Task<bool> RemovePoliciesAsync(string section, string policyType, I

try
{
await Adapter.RemovePoliciesAsync(section, policyType, rulesArray);
await Adapter.RemovePoliciesAsync(section, policyType, rulesArray, CancellationToken);
}
catch (NotImplementedException)
{
Expand Down