service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package suture
  2. /*
  3. Service is the interface that describes a service to a Supervisor.
  4. Serve Method
  5. The Serve method is called by a Supervisor to start the service.
  6. The service should execute within the goroutine that this is
  7. called in. If this function either returns or panics, the Supervisor
  8. will call it again.
  9. A Serve method SHOULD do as much cleanup of the state as possible,
  10. to prevent any corruption in the previous state from crashing the
  11. service again.
  12. Stop Method
  13. This method is used by the supervisor to stop the service. Calling this
  14. directly on a Service given to a Supervisor will simply result in the
  15. Service being restarted; use the Supervisor's .Remove(ServiceToken) method
  16. to stop a service. A supervisor will call .Stop() only once. Thus, it may
  17. be as destructive as it likes to get the service to stop.
  18. Once Stop has been called on a Service, the Service SHOULD NOT be
  19. reused in any other supervisor! Because of the impossibility of
  20. guaranteeing that the service has actually stopped in Go, you can't
  21. prove that you won't be starting two goroutines using the exact
  22. same memory to store state, causing completely unpredictable behavior.
  23. Stop should not return until the service has actually stopped.
  24. "Stopped" here is defined as "the service will stop servicing any
  25. further requests in the future". For instance, a common implementation
  26. is to receive a message on a dedicated "stop" channel and immediately
  27. returning. Once the stop command has been processed, the service is
  28. stopped.
  29. Another common Stop implementation is to forcibly close an open socket
  30. or other resource, which will cause detectable errors to manifest in the
  31. service code. Bear in mind that to perfectly correctly use this
  32. approach requires a bit more work to handle the chance of a Stop
  33. command coming in before the resource has been created.
  34. If a service does not Stop within the supervisor's timeout duration, a log
  35. entry will be made with a descriptive string to that effect. This does
  36. not guarantee that the service is hung; it may still get around to being
  37. properly stopped in the future. Until the service is fully stopped,
  38. both the service and the spawned goroutine trying to stop it will be
  39. "leaked".
  40. Stringer Interface
  41. When a Service is added to a Supervisor, the Supervisor will create a
  42. string representation of that service used for logging.
  43. If you implement the fmt.Stringer interface, that will be used.
  44. If you do not implement the fmt.Stringer interface, a default
  45. fmt.Sprintf("%#v") will be used.
  46. Optional Interface
  47. Services may optionally implement IsCompletable, which allows a service
  48. to indicate to a supervisor that it does not need to be restarted if
  49. it has terminated.
  50. */
  51. type Service interface {
  52. Serve()
  53. Stop()
  54. }
  55. /*
  56. IsCompletable is an optionally-implementable interface that allows a service
  57. to report to a supervisor that it does not need to be restarted because it
  58. has terminated normally. When a Service is going to be restarted, the
  59. supervisor will check for this method, and if Complete returns true, the
  60. service is removed from the supervisor instead of restarted.
  61. This is only executed when the service is not running because it has
  62. terminated, and has not yet been restarted.
  63. */
  64. type IsCompletable interface {
  65. Complete() bool
  66. }