-
Notifications
You must be signed in to change notification settings - Fork 282
feat: Adds the background worker for fire-and-forget ads-client #7597
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
base: main
Are you sure you want to change the base?
Changes from all commits
34415fc
791f0a6
ef7a00a
b187cd4
0e4c7d5
59fa831
93f7d7d
231d4cd
2e932a1
df0fcac
517f7c8
141f1d5
46b0abe
a179fc0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
| */ | ||
|
|
||
| #[cfg(feature = "stateful")] | ||
| use crate::ads_store::AdsStore; | ||
| use crate::ads_store::{AdsStore, PlacementId, StorableAd}; | ||
| use crate::common::bytesize::ByteSize; | ||
| use crate::http_cache::{CachePolicy, HttpCache}; | ||
| use crate::mars::ad_request::{AdPlacementRequest, AdRequestFlags}; | ||
| use crate::mars::ad_response::{AdImage, AdResponse, AdResponseValue, AdSpoc, AdTile}; | ||
| #[cfg(feature = "stateful")] | ||
| use crate::mars::error::FetchAdsError; | ||
| use crate::mars::error::{RecordClickError, RecordImpressionError, ReportAdError}; | ||
| use crate::mars::{MARSClient, ReportReason}; | ||
| #[cfg(feature = "stateful")] | ||
|
|
@@ -111,6 +113,71 @@ where | |
| self.client.clear_cache() | ||
| } | ||
|
|
||
| #[cfg(feature = "stateful")] | ||
| pub fn store_ads( | ||
| &mut self, | ||
| ads: HashMap<PlacementId, StorableAd>, | ||
| ) -> Result<(), FetchAdsError> { | ||
| let ads_store = self.ads_store.lock(); | ||
| if let Some(ads_store) = ads_store.as_ref() { | ||
| ads_store.store_ads(ads)?; | ||
| Ok(()) | ||
| } else { | ||
| Err(FetchAdsError::SqliteShutdown) | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should not use the word "cache" because it's not really a cache, it's more like a store / view / aggregate / projection / query model. (like CQRS pattern)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, this is a vestige from the old version. |
||
|
|
||
| #[cfg(feature = "stateful")] | ||
| pub fn get_stored_ad_images(&self, placement_id: &PlacementId) -> Option<AdImage> { | ||
| let ads_store = self.ads_store.lock(); | ||
| if let Some(ads_store) = ads_store.as_ref() { | ||
| match ads_store.lookup(placement_id) { | ||
| Ok(ad) => ad.and_then(|ad| ad.into_image()), | ||
| Err(_) => { | ||
| // TODO: Telemetry should return an error here (eg: some internal sqlite error) | ||
| None | ||
| } | ||
| } | ||
| } else { | ||
| // TODO: Telemetry should be added here for the database being shut down. | ||
| None | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "stateful")] | ||
| pub fn get_stored_ad_spocs(&self, placement_id: &PlacementId) -> Option<Vec<AdSpoc>> { | ||
| let ads_store = self.ads_store.lock(); | ||
| if let Some(ads_store) = ads_store.as_ref() { | ||
| match ads_store.lookup(placement_id) { | ||
| Ok(ad) => ad.and_then(|ad| ad.into_spocs()), | ||
| Err(_) => { | ||
| // TODO: Telemetry should return an error here (eg: some internal sqlite error) | ||
| None | ||
| } | ||
| } | ||
| } else { | ||
| // TODO: Telemetry should be added here for the database being shut down. | ||
| None | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "stateful")] | ||
| pub fn get_stored_ad_tile(&self, placement_id: &PlacementId) -> Option<AdTile> { | ||
| let ads_store = self.ads_store.lock(); | ||
| if let Some(ads_store) = ads_store.as_ref() { | ||
| match ads_store.lookup(placement_id) { | ||
| Ok(ad) => ad.and_then(|ad| ad.into_tile()), | ||
| Err(_) => { | ||
| // TODO: Telemetry should return an error here (eg: some internal sqlite error) | ||
| None | ||
| } | ||
| } | ||
| } else { | ||
| // TODO: Telemetry should be added here for the database being shut down. | ||
| None | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this, I feel it would probably be better not to use pub fn query_spoc() -> Option<Spoc>
pub fn query_tile() -> Option<Spoc>
etc.Then on the store side, have a generic parameter
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, my initial plan for it was to do that on the next layer. eg: I'll replace this with what my idea was. |
||
|
|
||
| pub fn get_context_id(&self) -> context_id::ApiResult<String> { | ||
| self.context_id_component.request(DEFAULT_ROTATION_DAYS) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its possible to also rewrite the sql queries to batch insert these, but not for this PR.