samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.71k stars 67 forks source link

feat: let pass our own chan to ShutdownOnSignals #56

Closed GreyXor closed 2 months ago

GreyXor commented 6 months ago

Passing your own channel as an argument to these methods allows you to have control over the communication channel used to receive signals. That is very useful for my custom signal handling logic.

codecov-commenter commented 6 months ago

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (c635315) 86.09% compared to head (7a5c686) 85.99%.

Files Patch % Lines
root_scope.go 0.00% 6 Missing :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## v2-🚀 #56 +/- ## ========================================== - Coverage 86.09% 85.99% -0.10% ========================================== Files 20 20 Lines 1783 1785 +2 ========================================== Hits 1535 1535 - Misses 218 220 +2 Partials 30 30 ``` | [Flag](https://app.codecov.io/gh/samber/do/pull/56/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/samber/do/pull/56/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe) | `85.99% <0.00%> (-0.10%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Berthe#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

samber commented 5 months ago

Can you develop your use case please ?

Why not just call injector.Shutdown() when you need a custom shutdown logic?

GreyXor commented 5 months ago

I give the channel to my API so I can shutdown my server from it. Yes for now I manage to do it by calling Injector.Shutdown() it's working as expected.

This solution just allows me to not write the signal waiting, which just saves me a few lines of code.

samber commented 5 months ago

We recently removed some Shutdown**** helpers for simplicity. I'm not sure we want to support more cases here.

Also, I think sending a os.Signal into this channel is unsafe, since the Shutdown helper might listen to different signals.

IMO, it's better to wait with:

ch := make(chan struct{}{})

go func() {
    <-ch
    injector.Shutdown()
}()

I will keep this PR open for further discussion.

GreyXor commented 5 months ago
ch := make(chan struct{}{})

go func() {
    <-ch
    injector.Shutdown()
}()

Yes, that's how i'm doing it so far, and it's working well. We can keep it like that