@@ -200,6 +200,52 @@ keeper := shutdownKeeper.NewKeeper(shutdownKeeper.KeeperOpts{
200200})
201201```
202202
203+ ### Chained HoldToken
204+ The chained HoldToken feature allows you to create hierarchical HoldToken structures with dependencies. Child tokens will wait for parent tokens to be released before executing their shutdown logic. This mechanism ensures the orderly shutdown of multiple dependent modules in complex scenarios.
205+
206+ ``` go
207+ package main
208+
209+ import (
210+ " context"
211+ " fmt"
212+ " time"
213+ " os"
214+ " syscall"
215+
216+ " github.com/hsldymq/shutdownKeeper/v2"
217+ )
218+
219+ func main () {
220+ keeper := shutdownKeeper.NewKeeper (shutdownKeeper.KeeperOpts {
221+ Signals: []os.Signal {syscall.SIGINT , syscall.SIGTERM },
222+ })
223+
224+ // Create tokens for two modules, where module A should be released before module B
225+ moduleAToken := keeper.AllocHoldToken ()
226+ moduleBToken := moduleAToken.AllocChainedToken ()
227+
228+ // Module A shutdown logic
229+ moduleAToken.DoOnShutdown (func (ctx context.Context ) {
230+ fmt.Printf (" Module A starting shutdown..." )
231+ // Simulate some cleanup work
232+ time.Sleep (2 * time.Second )
233+ fmt.Println (" Module A shutdown completed" )
234+ })
235+
236+ // Module B shutdown logic (will wait for module A to shutdown first)
237+ moduleBToken.DoOnShutdown (func (ctx context.Context ) {
238+ fmt.Printf (" Module B starting shutdown..." )
239+ time.Sleep (1 * time.Second )
240+ fmt.Println (" Module B shutdown completed" )
241+ })
242+
243+ fmt.Println (" Application started, press Ctrl+C to test ordered shutdown" )
244+ keeper.Wait ()
245+ fmt.Println (" Application gracefully shut down in order" )
246+ }
247+ ```
248+
203249## Frequently Asked Questions
204250
205251### Q: Why not use context.WithCancel directly?
@@ -210,6 +256,6 @@ A: Context can only pass cancellation signals but cannot ensure that all gorouti
210256
211257A: Set the ` MaxHoldTime ` parameter, and it will force exit after timeout. You can also set your own timeout logic within each module.
212258
213- ### Q: Can Tokens be dynamically allocated at runtime?
259+ ### Q: Can HoldTokens be dynamically allocated at runtime?
214260
215261A: Yes! You can call ` AllocHoldToken() ` at any time, and Keeper will track all allocated tokens.
0 commit comments