Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Initial Version #2

Merged
merged 1 commit into from
Apr 25, 2018
Merged

Initial Version #2

merged 1 commit into from
Apr 25, 2018

Conversation

jrconlin
Copy link
Member

@jrconlin jrconlin commented Apr 7, 2018

This is a rust implementation of PushBox and is a work in progress.

src/db/models.rs Outdated
.filter(pushboxv1::device_id.eq(device_id.clone())),
).execute(conn)
.context(HandlerErrorKind::DBError)
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never fail?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may, but IIUC, the context() function captures the error and handles it as a HandleErrorKind::DBError https://boats.gitlab.io/failure/doc/failure/trait.Fail.html#method.context Although, I admit that it's through the failure crate, so there's some voodoo going on there.

Copy link
Member

@pjenvey pjenvey Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right you'll convert the diesel Error into a failure here but you still ultimately end up with a Result that you don't want to unwrap() (panic).

This function already returns a HandlerError so instead propagate it out via try!: .context(HandlerErrorKind::DBError)?;

src/db/models.rs Outdated
.filter(pushboxv1::service.eq(service.clone())),
).execute(conn)
.context(HandlerErrorKind::DBError)
.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bare unwrap?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. The context should provide additional handling information.

src/server.rs Outdated
.unwrap_or(-1);
if decoded > -1 {
match key.to_lowercase().as_str() {
"index" => opt.index = Some(decoded.clone()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: don't need to call .clone() on these primitives here (they are Copy types)

src/server.rs Outdated

#[derive(Debug)]
pub struct Options {
pub index: Option<i64>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: these and default_ttl could really be u64s

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, except that diesel maps BigInt to i64 http://docs.diesel.rs/src/diesel/sql_types/mod.rs#102, and changing to u64 triggers a compiler issue:

the trait `diesel::Expression` is not implemented for `u64`

so i64 it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meh, going to hack around it. Not using u64s bugs me too.

src/server.rs Outdated
}
Ok(Json(json!({
"last": is_last,
"index": max_index.clone(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

src/server.rs Outdated
let messages = match DatabaseManager::read_records(&conn, &user_id, &device_id, &service, 0, 0)
{
Ok(val) => val,
Err(e) => return Err(e),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this match statement and the one in DatabaseManager::new_record should be killed for try! (let messages = ...)?;)

src/db/models.rs Outdated
.expect("Could not create transaction");
insert_into(pushboxv1::table)
.values((
pushboxv1::user_id.eq(user_id.to_string()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none of the to_string or cloned calls are needed for these diesel queries, it's ok w/ &strs

src/server.rs Outdated
&data.data,
calc_ttl(data.ttl),
);
if response.is_err() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here and in delete, you can replace these 3 lines by using the ? operator on the previous statement

src/auth.rs Outdated
// There is no other way to get the rocket.config() from inside a request
// handler.
let config = request.guard::<State<RustboxConfig>>().unwrap();
let fxa_host = config.fxa_host.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be referenced (= &config.fxa_host) vs clone()'d

src/server.rs Outdated
&data.data,
calc_ttl(data.ttl),
);
if response.is_err() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same w/ these, just use ? on previous statement

src/auth.rs Outdated
))
}
};
if raw_resp.status().is_success() == false {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!raw_resp.status().is_success()

src/auth.rs Outdated
fn test_valid() {
let mut test_data = Table::new();
let mut fxa_response = Table::new();
fxa_response.insert("user".to_owned(), "test".to_owned().into());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can replace .to_owned.into() with just .into() for all these values

src/server.rs Outdated
}
Ok(Json(json!({
"last": is_last,
"index": max_index.clone(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't "index" returned here the last index in the current batch we are returning to the client? So it knows where it left off. max_index here is the last index of all messages, which isn't right.

src/db/models.rs Outdated
service: &String,
) -> HandlerResult<bool> {
// boxed deletes are "coming soon"
// see https://github.com/diesel-rs/diesel/pull/1534
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they're here! 1.2.0 was just released w/ them

@bbangert
Copy link
Member

The commits should probably be squashed at this point, and verified.

@bbangert
Copy link
Member

No .travis.yml for a basic build test?

Copy link
Member

@pjenvey pjenvey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks about ready to merge to mozilla-services/pushbox master (minus circleci and a deployment setup) let alone rustbox master

token: String,
config: &ServerConfig,
logger: &RBLogger,
) -> request::Outcome<Self, HandlerError> {
Copy link
Member

@pjenvey pjenvey Apr 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that this is separate from a request guard, it can return Result instead of Outcome (which can let the error handling code become easier)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not so sure. Both of these are used by from_request which wants an Outcome response. I'm sure there's some cleanup that could be done there, but having both of these return Outcome I think still makes sense.

src/db/models.rs Outdated
let t_manager = conn.transaction_manager();
t_manager
.begin_transaction(conn)
.expect("Could not create transaction");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error should never happen, but maybe there's a once in a million db/network hiccup: safer to use .context(HandlerErrorKind::DBError)? instead

src/db/models.rs Outdated
};
t_manager
.commit_transaction(conn)
.expect("Could not close transaction");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as begin_transaction

This is a rust implementation of PushBox and is a work in progress.
@jrconlin
Copy link
Member Author

No, travis will be coming shortly, once I iron out the docker stuff.

@jrconlin jrconlin merged commit bfe5a4b into master Apr 25, 2018
@bbangert bbangert deleted the wip branch April 25, 2018 21:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants