Skip to content

Expand throttle to include string targets#5302

Merged
zachmargolis merged 15 commits intomainfrom
margolis-throttle-by-strings
Aug 18, 2021
Merged

Expand throttle to include string targets#5302
zachmargolis merged 15 commits intomainfrom
margolis-throttle-by-strings

Conversation

@zachmargolis
Copy link
Copy Markdown
Contributor

  • Also combine separate services into methods on the AR model, via
    shared .for method

This was motivated by LG-4942 where we'll want to be able to throttle IDV attempts by SSN, which can be independent from users

I didn't know #5301 would also touch the Throttle model, but I think these changes won't have any problems combining

- Also combine separate services into methods on the AR model, via
  shared .for method
Comment on lines +52 to +65
# @param [User,Integer,String] target User, its ID, or a string identifier
# @param [Symbol] throttle_type
# @return [Throttle]
def self.for(target:, throttle_type:)
throttle = case target
when User
find_or_create_by(user: target, throttle_type: throttle_type)
when Integer
find_or_create_by(user_id: target, throttle_type: throttle_type)
when String
find_or_create_by(target: target, throttle_type: throttle_type)
else
raise "Unknown throttle target class=#{target.class}"
end
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To keep the old style code, I would have had to update our old Throttler::FindOrCreate and all the signatures of those separate service classes....or just move things onto the model where they can be instance methods and keep a lot of context together

That choice pushed me to just do the big refactor

!expired? && maxed?
end

def throttled_else_increment?
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Open to new name suggestions here! It's not a normal predicate method because it has a side effect. Maybe just drop the ? at the end? 😬

Copy link
Copy Markdown
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

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

This feels like a nice improvement over the services 👍

I didn't know #5301 would also touch the Throttle model, but I think these changes won't have any problems combining

Nope, I think I'll have some updates to make, but not fundamentally conflicting.


def throttled?
Throttler::IsThrottled.call(document_capture_session.user_id, :idv_acuant)
return unless document_capture_session.user
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should it return a boolean? Is it necessary at all, given that I can't see that the previous logic would have guarded for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think we can get rid of the guard... 45c436f

Comment on lines +73 to +75
return self if maxed?
update(attempts: attempts + 1, attempted_at: Time.zone.now)
self
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Kinda curious about these returning the instance itself. It seems to have had existed previously, though unclear if we want to change that as part of the refactoring. We already are in the case of throttled_else_increment? which previously returned the throttle instance if it was throttled (now returns true).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah I kept this to match the old behavior, but I have no idea what an expected return value for increment would be... in other languages I'd make this void or return the count... I guess I'll have it return the count

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

zachmargolis and others added 5 commits August 18, 2021 09:33
Co-authored-by: Andrew Duthie <aduth@users.noreply.github.com>
- Also add memoization

Co-authored-by: Andrew Duthie <aduth@users.noreply.github.com>
@zachmargolis zachmargolis merged commit 398c0f8 into main Aug 18, 2021
@zachmargolis zachmargolis deleted the margolis-throttle-by-strings branch August 18, 2021 17:44
def throttled_else_increment
Throttler::IsThrottledElseIncrement.call(user_id, :idv_acuant)
Throttle.for(
target: user_id,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this have been... ?

Suggested change
target: user_id,
user: user_id,

Copy link
Copy Markdown
Contributor Author

@zachmargolis zachmargolis Aug 30, 2021

Choose a reason for hiding this comment

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

🤦 I think it should have been:

Suggested change
target: user_id,
user: current_user,

but yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants