Skip to content
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

Improvements to IPConnection #26

Closed
wants to merge 1 commit into from
Closed

Conversation

OCopping
Copy link
Contributor

No description provided.

Copy link

codecov bot commented Mar 11, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 20 lines in your changes missing coverage. Please review.

Project coverage is 45.03%. Comparing base (2d9657f) to head (b7f6601).
Report is 64 commits behind head on main.

Files with missing lines Patch % Lines
src/fastcs/connections/ip_connection.py 50.00% 20 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #26      +/-   ##
==========================================
+ Coverage   43.96%   45.03%   +1.06%     
==========================================
  Files          18       18              
  Lines         655      675      +20     
==========================================
+ Hits          288      304      +16     
- Misses        367      371       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@OCopping OCopping marked this pull request as ready for review March 12, 2024 09:21
@OCopping
Copy link
Contributor Author

OCopping commented Mar 12, 2024

If you two are happy with the decorator approach to try and clean up the functions inside Connection objects, I might try and move them into a separate script to then be imported. I would have to make them more generalised though (e.g. instead of looking at self._reader and self,_writer in _ensure_connected, I could set a new self._connected variable in the object that could be used in every new Connection object too)

I have tested similar decorators in pythonSoftIOCs I have written recently and they worked nicely.

async with self.lock:
await func(*args, **kwargs)

return cast(_AsyncFuncType, with_lock)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I would prefer to have to do

with self.lock:
    ...

than this honestly. It is more conventional and it allows minimising the time the lock is held without having to split it out into a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The old implementation wrapped all the contents of the functions in the context manager though. I can see why this would be nice for if only part of the function was in the CM, but I feel like the decorator approach is cleaner if that isn't the case.


@property
def lock(self) -> asyncio.Lock:
return self._lock
Copy link
Contributor

@GDYendell GDYendell Mar 14, 2024

Choose a reason for hiding this comment

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

This should be removed if _with_lock is removed. I am wary that this suggests that it can be generally used outside the class, and it should not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Originally I didn't have this property, I only created it as the linter complained. The intention is that this would only be used in the decorator so wrap the function in the context manager, not use outside the class.

else:
await func(*args, **kwargs)

return cast(_AsyncFuncType, check_connected)
Copy link
Contributor

@GDYendell GDYendell Mar 14, 2024

Choose a reason for hiding this comment

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

In this case I would prefer to make a wrapper around StreamWriter and StreamReader (StreamConnection?) where the connection is either open, or an exception is raised. This would simplify the typing and avoid having to do isinstance and is not None everywhere. So, IPConnection would make a StreamConnection, the connection is guaranteed to be open as soon as the object is instantiated and then it can call send and recv without having to check every time if it is valid.

I feel like this shouldn't be necessary. I expect am misunderstanding something here, but the asyncio API for this seems clunky.

Thoughts?

Copy link
Contributor Author

@OCopping OCopping Mar 14, 2024

Choose a reason for hiding this comment

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

I did realise this probably does the same thing as SerialConnection does in #24, except that uses the socket module. I feel like that would be the better way to approach this.
It is worth noting that currently is written to be synchronous.

@GDYendell
Copy link
Contributor

I think this is superseded by the refactor for pyright in #61

@GDYendell GDYendell closed this Oct 14, 2024
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.

2 participants