Guest User

Untitled

a guest
Jun 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. // Start starts monitoring for competition entity for resource
  2. func (k *GateKeeper) Start(resource interface{}, waitTime time.Duration) {
  3. k.mux.Lock()
  4. defer k.mux.Unlock()
  5. if k.cancel != nil {
  6. k.cancel()
  7. }
  8. k.ctx, k.cancel = context.WithCancel(context.Background())
  9. mux := &GreedyMutex{
  10. TimeToWait: waitTime,
  11. queue: k.queue,
  12. }
  13. wg := new(sync.WaitGroup)
  14. wg.Add(1)
  15. go func() {
  16. wg.Done()
  17. mux.Compete(k.ctx, resource)
  18. }()
  19. wg.Wait()
  20. fmt.Println("successfully start monitoring for resource")
  21. }
  22.  
  23. // Stop stops monitoring resource
  24. func (k *GateKeeper) Stop() {
  25. if k.cancel != nil {
  26. k.cancel()
  27. }
  28. }
  29.  
  30. // Register register for accessing resource
  31. func (k *GateKeeper) Register(entity PrioritizedEntity) {
  32. k.queue <- entity
  33. }
Add Comment
Please, Sign In to add comment