Goroutine unit manager
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. # gum
  2. Go Unit Manager is a simple Goroutine unit manager for GoLang.
  3. Features:
  4. - Scheduling of multiple goroutines.
  5. - Shutdown on `os.Signal` events.
  6. - Gracefull shutdown of units
  7. ## Overview
  8. A unit is a type that implements `WorkUnit` interface. The `Spawn()` method
  9. of each registered unit is spawned in its own goroutine.
  10. The `Manager` handles communication and synchronized shutdown procedure.
  11. ## Usage
  12. 1. Create a unit manager
  13. 2. Implement the `WorkUnit` on your goroutines
  14. 3. Add units to the manager
  15. 4. Run the manager and wait on its `Quit` channel
  16. ```golang
  17. import (
  18. "os"
  19. "log"
  20. "time"
  21. gum "git.sp4ke.com/sp4ke/gum.git"
  22. )
  23. type Worker struct{}
  24. // Example loop, will be spwaned inside a goroutine
  25. func (w *Worker) Spawn(um UnitManager) {
  26. ticker := time.NewTicker(time.Second)
  27. // Worker's loop
  28. for {
  29. select {
  30. case <-ticker.C:
  31. log.Println("tick")
  32. // Read from channel if this worker unit should stop
  33. case <-um.ShouldStop():
  34. // Shutdown work for current unit
  35. w.Shutdown()
  36. // Notify manager that this unit is done.
  37. um.Done()
  38. }
  39. }
  40. }
  41. func (w *Worker) Shutdown() {
  42. // Do shutdown procedure for worker
  43. return
  44. }
  45. func NewWorker() *Worker {
  46. return &Worker{}
  47. }
  48. func main() {
  49. // Create a unit manager
  50. manager := gum.NewManager()
  51. // Shutdown all units on SIGINT
  52. manager.ShutdownOn(os.Interrupt)
  53. // NewWorker returns a type implementing WorkUnit interface unit :=
  54. worker := NewWorker()
  55. worker2 := NewWorker()
  56. // Register the unit with the manager
  57. manager.AddUnit(worker)
  58. manager.AddUnit(worker2)
  59. // Run the manager
  60. go manager.Run()
  61. // Wait for all units to shutdown gracefully through their `Shutdown` method
  62. <-manager.Quit
  63. }
  64. ```
  65. ## Issues and Comments
  66. This repo is a mirror. For any question or issues use the repo hosted at
  67. [https://git.sp4ke.com/sp4ke/gum.git](https://git.sp4ke.com/sp4ke/gum.git)