-
Notifications
You must be signed in to change notification settings - Fork 87
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
Refactor Guardian.DB.Sweeper #130
Conversation
@sixty_minutes 60 * 60 * 1000 | ||
|
||
def start_link(opts) do | ||
interval = Keyword.get(opts, :interval, @sixty_minutes) |
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.
looks like it's : sweep_interval
in the config, @doomspork
config :guardian, Guardian.DB,
repo: MyApp.Repo, # Add your repository module
schema_name: "guardian_tokens", # default
token_types: ["refresh_token"], # store all token types if not set
sweep_interval: 60 # default: 60 minutes
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.
oh I got it now, so it's not configurable anymore?
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.
It is configurable @alexfilatov but as I mentioned in this point:
- Stop accessing configuration directly from the library code. It's generally a better practice for the application to have its own configuration which it passes in rather than libraries doing that themselves.
I think we should be moving away from our libraries accessing application configuration themselves directly. Applications should be able to pass in their configurations.
I'm open to not doing this, I think it's just a better design practice.
children = [
...
{Guardian.DB.Sweeper, interval: configured_sweep_interval()}
...
]
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 love this part for sure
I disagree with the promise of no real benefits. SweeperServer is impure, it deals with Erlang message passing and gen-server stuff. While Sweeper is pure since it is just the implementation of the Sweeper. Functional Core with Imperative Interface. That being said, you are the boss. I just would try to be clear on what is wrong as of today based on the objective issues since it wasn't been a problem so far before we start removing things just because it is deemed unimportant based on your skills set or your perspective, which it isn't hard to understand where you are coming from for sure. I totally understand your reasoning. |
Overall, don't mind the changes, I can maintaining them either way, if you feel strong about it, you have my 1+ |
It's not that at all. I'm open to ideas and suggestions.
I'm not sure "wasn't been a problem so far" is ever good reason to avoid changes, would we ever improve code with that mentality? If your concern is having to test the GenServers I can update the test to use the GenServer callbacks directly and avoid that. I actually want to test that this stuff works accordingly so these tests are intentional but I'm happy to remove them. Simplifying the code to be easier to understand makes things better for everyone, especially us maintainers. A code base that's difficult to grok keeps contributors away. I'm okay to not incorporating this change but I think it's misguided to assume that just because we can have "pure" functions to test that there isn't a better solution. As-is responsibility is spread throughout modules making understanding the code base far more complicated/ |
Works for me either way to me, I leave this one to your judgment, I trust your judgments. |
Once #129 is merged I will update this further 👍 |
bff7f9d
to
a0bad53
Compare
@yordis I updated the tests and made use of Mox for 2 reasons:
|
scheduled work. | ||
""" | ||
def purge do | ||
GenServer.cast(__MODULE__, :sweep) |
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 were also changed to cast
from call
since we don't need or care about a reply.
@@ -0,0 +1,70 @@ | |||
defmodule Guardian.DB.Sweeper do |
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.
For backwards compatibility we could add a Guardian.DB.Token.SweeperServer
alias but I'd like to encourage folks to move away from that module.
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.
@doomspork wouldn't be better to use SweeperServer
following some GenServer
lingos?
It is a stateful thing (hint the server part of it) that you send messages to it 🤔 💭
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 don't often seen things called "Server" in Elixir/Erlang space, do you? Looking at a few of my applications there's nothing in the application tree with that suffix 🤔
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.
It depends if you have the Server
being the message passing, while the thing without Server
(as it was before), was the implementation of the thing.
Dave has an amazing course about it, https://codestool.coding-gnome.com/courses/elixir-for-programmers
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 appreciate Dave's pattern but as much as he would like to see this catch on I can't say I see this having gained any adoption. I prefer to keep code simple and easy to grok for the human reader over trying to be "academically right".
I'd like to leave this.
Moved the Sweeper functionality out from inside the Token namespace/folder. Also simplified the code by combining the two separate but unnecessary modules. Updated and added test coverage.
a0bad53
to
5fb60f2
Compare
5fb60f2
to
cdebe9a
Compare
I'd like to slowly start improving the code quality and test coverage across the Ueberauth org projects. Since we've been focusing on Guardian.DB recently I thought I'd start by proposing some changes here. For this PR I looked to tackle the Sweeper functionality first:
Sweeper
andSweeperServer
but their roles are not clearly separated (Sweeper
manages theSweeperServer
's state for instance). I combined these together as there's no real benefit to keeping them separate and this cleans up some of their code.purge/0
andreset_timer/0
.Guardian.DB.Token.SweeperServer
toGuardian.DB.Sweeper
, I'm not entirely sure why this was ever nested under Token but it seems rather unnecessary. This is a breaking change.I may look at adding some addition test coverage for each of the GenServer callbacks just to cover our bases.
This PR is a place to start this conversation, hence the draft. 🎉