Skip to content

Commit

Permalink
refactor: Prep for Wasm (#175)
Browse files Browse the repository at this point in the history
* chore(wip): refactor for wasm

* refactor: extract all ffi specific code from flipt-evaluation

* chore: add flipt-evaluation to release-please

* chore: rm flipt wasm from cargo toml

* chore: try to fix CI

* chore: rename var

* chore: rm wasm lib for now

* chore: move batch down to eval lib

* chore: fmt

* chore: common visibility

* chore: try to setup codecov (#176)

* chore: apply fixes on main
  • Loading branch information
markphelps authored Mar 25, 2024
1 parent d87e9ac commit 34f779b
Show file tree
Hide file tree
Showing 15 changed files with 934 additions and 959 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/test-engine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,27 @@ jobs:
with:
command: clippy
args: -- -D warnings

coverage:
name: Coverage
runs-on: ubuntu-latest
env:
CARGO_TERM_COLOR: always
steps:
- uses: actions/checkout@v4

- name: Install Rust
run: rustup update stable

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Generate code coverage
run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: lcov.info
fail_ci_if_error: true
3 changes: 2 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"flipt-engine-ffi": "0.1.6"
"flipt-engine-ffi": "0.1.5",
"flipt-evaluation": "0.0.3"
}
4 changes: 4 additions & 0 deletions flipt-engine-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ publish = false
libc = "0.2.150"
serde = { version = "1.0.147", features = ["derive"] }
serde_json = { version = "1.0.89", features = ["raw_value"] }
reqwest = { version = "0.11.22", features = ["json", "blocking"] }
tokio = { version = "1.33.0", features = ["full"] }
futures = "0.3"
openssl = { version = "0.10", features = ["vendored"] }

[dependencies.flipt-evaluation]
path = "../flipt-evaluation"
Expand Down
8 changes: 5 additions & 3 deletions flipt-engine-ffi/examples/evaluation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// cargo run --example evaluation

use fliptengine::{self};
use fliptevaluation::parser::{Authentication, HTTPParserBuilder};
use fliptevaluation::{EvaluationRequest, Evaluator};
use fliptengine::{
evaluator::Evaluator,
parser::http::{Authentication, HTTPParserBuilder},
};
use fliptevaluation::EvaluationRequest;
use std::collections::HashMap;

fn main() {
Expand Down
107 changes: 107 additions & 0 deletions flipt-engine-ffi/src/evaluator/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use std::sync::{Arc, RwLock};

use fliptevaluation::{
batch_evalution, boolean_evaluation,
error::Error,
models::{flipt, source::Document},
parser::Parser,
store::{Snapshot, Store},
variant_evaluation, BatchEvaluationResponse, BooleanEvaluationResponse, EvaluationRequest,
VariantEvaluationResponse,
};

pub struct Evaluator<P, S>
where
P: Parser + Send,
S: Store + Send,
{
namespace: String,
parser: P,
store: S,
mtx: Arc<RwLock<i32>>,
}

impl<P> Evaluator<P, Snapshot>
where
P: Parser + Send,
{
pub fn new_snapshot_evaluator(namespace: &str, parser: P) -> Result<Self, Error> {
let doc = parser.parse(namespace)?;
let snap = Snapshot::build(namespace, doc)?;
Ok(Evaluator::new(namespace, parser, snap))
}

pub fn replace_snapshot(&mut self) {
let doc = match self.parser.parse(&self.namespace) {
Ok(d) => d,
Err(_) => {
// TODO: log::error!("error parsing document: {}"", e);
Document::default()
}
};

match Snapshot::build(&self.namespace, doc) {
Ok(s) => {
self.replace_store(s);
}
Err(_) => {
// TODO: log::error!("error building snapshot: {}", e);
}
};
}
}

impl<P, S> Evaluator<P, S>
where
P: Parser + Send,
S: Store + Send,
{
pub fn new(namespace: &str, parser: P, store: S) -> Self {
Self {
namespace: namespace.to_string(),
parser,
store,
mtx: Arc::new(RwLock::new(0)),
}
}

pub fn replace_store(&mut self, store: S) {
let _w_lock = self.mtx.write().unwrap();
self.store = store;
}

pub fn list_flags(&self) -> Result<Vec<flipt::Flag>, Error> {
let _r_lock = self.mtx.read().unwrap();
match self.store.list_flags(&self.namespace) {
Some(f) => Ok(f),
None => Err(Error::Unknown(format!(
"failed to get flags for {}",
self.namespace,
))),
}
}

pub fn variant(
&self,
evaluation_request: &EvaluationRequest,
) -> Result<VariantEvaluationResponse, Error> {
let _r_lock = self.mtx.read().unwrap();
variant_evaluation(&self.store, &self.namespace, evaluation_request)
}

pub fn boolean(
&self,
evaluation_request: &EvaluationRequest,
) -> Result<BooleanEvaluationResponse, Error> {
let _r_lock = self.mtx.read().unwrap();
boolean_evaluation(&self.store, &self.namespace, evaluation_request)
}

pub fn batch(
&self,
requests: Vec<EvaluationRequest>,
) -> Result<BatchEvaluationResponse, Error> {
let _r_lock = self.mtx.read().unwrap();
batch_evalution(&self.store, &self.namespace, requests)
}
}
11 changes: 8 additions & 3 deletions flipt-engine-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
pub mod evaluator;
pub mod parser;

use evaluator::Evaluator;
use parser::http::{Authentication, HTTPParser, HTTPParserBuilder};

use fliptevaluation::error::Error;
use fliptevaluation::models::flipt;
use fliptevaluation::parser::{Authentication, HTTPParser, HTTPParserBuilder};
use fliptevaluation::store::Snapshot;
use fliptevaluation::{
BatchEvaluationResponse, BooleanEvaluationResponse, EvaluationRequest, Evaluator,
BatchEvaluationResponse, BooleanEvaluationResponse, EvaluationRequest,
VariantEvaluationResponse,
};
use libc::c_void;
Expand Down Expand Up @@ -197,7 +202,7 @@ pub unsafe extern "C" fn initialize_engine(
};

let parser = parser_builder.build();
let evaluator = Evaluator::new_snapshot_evaluator(namespace.to_string(), parser).unwrap();
let evaluator = Evaluator::new_snapshot_evaluator(namespace, parser).unwrap();

Box::into_raw(Box::new(Engine::new(evaluator, engine_opts))) as *mut c_void
}
Expand Down
Loading

0 comments on commit 34f779b

Please sign in to comment.