-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ruby): add offline initialization and Configuration API (#39)
* feat(ruby): add offline initialization and Configuration API * chore(ruby): bump version
- Loading branch information
Showing
13 changed files
with
352 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
eppo-server-sdk (3.1.1) | ||
eppo-server-sdk (3.2.0) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::sync::Arc; | ||
|
||
use magnus::{function, method, prelude::*, scan_args::get_kwargs, Error, RHash, RString, Ruby}; | ||
|
||
use eppo_core::{ufc::UniversalFlagConfig, Configuration as CoreConfiguration}; | ||
|
||
use crate::{gc_lock::GcLock, SDK_METADATA}; | ||
|
||
pub(crate) fn init(ruby: &Ruby) -> Result<(), Error> { | ||
let eppo_client = ruby.define_module("EppoClient")?; | ||
|
||
let configuration = eppo_client.define_class("Configuration", magnus::class::object())?; | ||
configuration.define_singleton_method("new", function!(Configuration::new, 1))?; | ||
configuration.define_method( | ||
"flags_configuration", | ||
method!(Configuration::flags_configuration, 0), | ||
)?; | ||
configuration.define_method( | ||
"bandits_configuration", | ||
method!(Configuration::bandits_configuration, 0), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
#[magnus::wrap(class = "EppoClient::Configuration", free_immediately)] | ||
pub struct Configuration { | ||
inner: Arc<CoreConfiguration>, | ||
} | ||
|
||
impl Configuration { | ||
fn new(ruby: &Ruby, kw: RHash) -> Result<Configuration, Error> { | ||
let args = get_kwargs(kw, &["flags_configuration"], &["bandits_configuration"])?; | ||
let (flags_configuration,): (RString,) = args.required; | ||
let (bandits_configuration,): (Option<Option<RString>>,) = args.optional; | ||
let rest: RHash = args.splat; | ||
if !rest.is_empty() { | ||
return Err(Error::new( | ||
ruby.exception_arg_error(), | ||
format!("unexpected keyword arguments: {:?}", rest), | ||
)); | ||
} | ||
|
||
let inner = { | ||
let _gc_lock = GcLock::new(ruby); | ||
|
||
Arc::new(CoreConfiguration::from_server_response( | ||
UniversalFlagConfig::from_json( | ||
SDK_METADATA, | ||
unsafe { | ||
// SAFETY: we have disabled GC, so the memory can't be modified concurrently. | ||
flags_configuration.as_slice() | ||
} | ||
.to_vec(), | ||
) | ||
.map_err(|err| { | ||
Error::new( | ||
ruby.exception_arg_error(), | ||
format!("failed to parse flags_configuration: {err:?}"), | ||
) | ||
})?, | ||
bandits_configuration | ||
.flatten() | ||
.map(|bandits| { | ||
serde_json::from_slice(unsafe { | ||
// SAFETY: we have disabled GC, so the memory can't be modified concurrently. | ||
bandits.as_slice() | ||
}) | ||
}) | ||
.transpose() | ||
.map_err(|err| { | ||
Error::new( | ||
ruby.exception_arg_error(), | ||
format!("failed to parse bandits_configuration: {err:?}"), | ||
) | ||
})?, | ||
)) | ||
}; | ||
|
||
Ok(Configuration { inner }) | ||
} | ||
|
||
fn flags_configuration(ruby: &Ruby, rb_self: &Self) -> Result<RString, Error> { | ||
Ok(ruby.str_from_slice(rb_self.inner.flags.to_json())) | ||
} | ||
|
||
fn bandits_configuration(ruby: &Ruby, rb_self: &Self) -> Result<Option<RString>, Error> { | ||
let Some(bandits) = &rb_self.inner.bandits else { | ||
return Ok(None) | ||
}; | ||
let vec = serde_json::to_vec(bandits).map_err(|err| { | ||
// this should never happen | ||
Error::new( | ||
ruby.exception_runtime_error(), | ||
format!("failed to serialize bandits configuration: {err:?}"), | ||
) | ||
})?; | ||
Ok(Some(ruby.str_from_slice(&vec))) | ||
} | ||
} | ||
|
||
impl From<Arc<CoreConfiguration>> for Configuration { | ||
fn from(inner: Arc<CoreConfiguration>) -> Configuration { | ||
Configuration { inner } | ||
} | ||
} | ||
|
||
impl From<Configuration> for Arc<CoreConfiguration> { | ||
fn from(value: Configuration) -> Arc<CoreConfiguration> { | ||
value.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use magnus::Ruby; | ||
|
||
pub struct GcLock<'a> { | ||
ruby: &'a Ruby, | ||
/// Holds `true` if GC was already disabled before acquiring the lock (so it doesn't need to be | ||
/// re-enabled). | ||
gc_was_disabled: bool, | ||
} | ||
|
||
impl<'a> GcLock<'a> { | ||
pub fn new(ruby: &'a Ruby) -> GcLock<'a> { | ||
GcLock { | ||
ruby, | ||
gc_was_disabled: ruby.gc_disable(), | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Drop for GcLock<'a> { | ||
fn drop(&mut self) { | ||
if !self.gc_was_disabled { | ||
self.ruby.gc_enable(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.