-
Notifications
You must be signed in to change notification settings - Fork 22
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
Add 5s timeout for market operations #305
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -368,6 +368,28 @@ async def accept_payment_for_agreement(agreement_id: str, *, partial: bool = Fal | |||||||
) | ||||||||
|
||||||||
async def find_offers() -> None: | ||||||||
async def reject_proposal(proposal, reason): | ||||||||
try: | ||||||||
await proposal.reject(reason=reason) | ||||||||
emit(events.ProposalRejected(prop_id=proposal.id, reason=reason)) | ||||||||
except Exception: | ||||||||
emit( | ||||||||
events.ProposalFailed( | ||||||||
prop_id=proposal.id, exc_info=sys.exc_info() # type: ignore | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
async def respond_to_proposal(proposal, builder): | ||||||||
try: | ||||||||
await proposal.respond(builder.properties, builder.constraints) | ||||||||
emit(events.ProposalResponded(prop_id=proposal.id)) | ||||||||
except Exception: | ||||||||
emit( | ||||||||
events.ProposalFailed( | ||||||||
prop_id=proposal.id, exc_info=sys.exc_info() # type: ignore | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
nonlocal offers_collected, proposals_confirmed | ||||||||
try: | ||||||||
subscription = await builder.subscribe(market_api) | ||||||||
|
@@ -394,12 +416,10 @@ async def find_offers() -> None: | |||||||
score, | ||||||||
) | ||||||||
except InvalidPropertiesError as err: | ||||||||
emit(events.ProposalRejected(prop_id=proposal.id, reason=str(err))) | ||||||||
await reject_proposal(proposal, "Malformed offer") | ||||||||
continue | ||||||||
if score < SCORE_NEUTRAL: | ||||||||
with contextlib.suppress(Exception): | ||||||||
await proposal.reject(reason="Score too low") | ||||||||
emit(events.ProposalRejected(prop_id=proposal.id, reason="Score too low")) | ||||||||
await reject_proposal(proposal, "Score too low") | ||||||||
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. This is not required, because there is no code after all
Suggested change
|
||||||||
elif not proposal.is_draft: | ||||||||
try: | ||||||||
common_platforms = self._get_common_payment_platforms(proposal) | ||||||||
|
@@ -409,31 +429,21 @@ async def find_offers() -> None: | |||||||
) | ||||||||
else: | ||||||||
# reject proposal if there are no common payment platforms | ||||||||
with contextlib.suppress(Exception): | ||||||||
await proposal.reject(reason="No common payment platform") | ||||||||
emit( | ||||||||
events.ProposalRejected( | ||||||||
prop_id=proposal.id, reason="No common payment platforms" | ||||||||
) | ||||||||
) | ||||||||
await reject_proposal(proposal, "No common payment platform") | ||||||||
continue | ||||||||
timeout = proposal.props.get(DEBIT_NOTE_ACCEPTANCE_TIMEOUT_PROP) | ||||||||
if timeout: | ||||||||
if timeout < DEBIT_NOTE_MIN_TIMEOUT: | ||||||||
with contextlib.suppress(Exception): | ||||||||
await proposal.reject(reason="Debit note timeout too low") | ||||||||
emit( | ||||||||
events.ProposalRejected( | ||||||||
prop_id=proposal.id, | ||||||||
reason="Debit note acceptance timeout too short", | ||||||||
) | ||||||||
await reject_proposal( | ||||||||
proposal, "Debit note acceptance timeout too short" | ||||||||
) | ||||||||
continue | ||||||||
else: | ||||||||
builder.properties[DEBIT_NOTE_ACCEPTANCE_TIMEOUT_PROP] = timeout | ||||||||
await proposal.respond(builder.properties, builder.constraints) | ||||||||
emit(events.ProposalResponded(prop_id=proposal.id)) | ||||||||
await respond_to_proposal(proposal, builder) | ||||||||
except CancelledError: | ||||||||
raise | ||||||||
except Exception as ex: | ||||||||
except Exception: | ||||||||
emit( | ||||||||
events.ProposalFailed( | ||||||||
prop_id=proposal.id, exc_info=sys.exc_info() # type: ignore | ||||||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
hmm... the only thing I'm concerned about is that again, we're adding inner-scope functions ... whereas maybe we should start pulling that stuff outside ... but I do understand that the milk is spilled by
find_offers
already so it would be artificial to pull these as methods and leavefind_offers
as internal...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.
Sometimes inner functions are just better - object-oriented programming is not the only paradigm, let's not be afraid of them. Actually, I like @azawlocki's solution very much, it's an improvement over the original code and over my PR to
yajsapi
, which AFAIR added another copy of try/call api/catch/emit.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.
These functions are not visible outside of
find_offers()
. If they are not used outside, IMO they shouldn't be moved as they are "helper" functions.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.
@shadeofblue I've just started pulling stuff outside in #306, I'm not sure we want to do that kind of refactoring in a PR to a release branch.