mutex.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package cert
  2. import (
  3. "sync"
  4. "github.com/0xJacky/Nginx-UI/internal/event"
  5. )
  6. var (
  7. // mutex is used to control access to certificate operations
  8. mutex sync.Mutex
  9. // isProcessing indicates whether a certificate operation is in progress
  10. isProcessing bool
  11. // processingMutex protects the isProcessing flag
  12. processingMutex sync.RWMutex
  13. )
  14. // publishProcessingStatus publishes the processing status to the event bus
  15. func publishProcessingStatus(processing bool) {
  16. event.Publish(event.Event{
  17. Type: event.EventTypeAutoCertProcessing,
  18. Data: processing,
  19. })
  20. }
  21. // lock acquires the certificate mutex
  22. func lock() {
  23. mutex.Lock()
  24. setProcessingStatus(true)
  25. }
  26. // unlock releases the certificate mutex
  27. func unlock() {
  28. setProcessingStatus(false)
  29. mutex.Unlock()
  30. }
  31. // IsProcessing returns whether a certificate operation is currently in progress
  32. func IsProcessing() bool {
  33. processingMutex.RLock()
  34. defer processingMutex.RUnlock()
  35. return isProcessing
  36. }
  37. // setProcessingStatus updates the processing status and publishes the change
  38. func setProcessingStatus(status bool) {
  39. processingMutex.Lock()
  40. if isProcessing != status {
  41. isProcessing = status
  42. publishProcessingStatus(status)
  43. }
  44. processingMutex.Unlock()
  45. }