Download this file
| 1 | package activator |
| 2 | |
| 3 | // PrepareScheduledToken orders token preparation with this host's local fence. |
| 4 | // An admitted callback finishes before its fence is installed. Other hosts do |
| 5 | // not wait for its IO. The callback must not reenter preparation or fencing for |
| 6 | // the same host. This establishes neither wake nor durable maintenance authority. |
| 7 | func (a *Service) PrepareScheduledToken(host string, prepare func() (string, error)) (string, error) { |
| 8 | if a == nil || prepare == nil { |
| 9 | return "", ErrFenced |
| 10 | } |
| 11 | for { |
| 12 | a.mu.Lock() |
| 13 | if a.fences[host] != 0 { |
| 14 | a.mu.Unlock() |
| 15 | return "", ErrFenced |
| 16 | } |
| 17 | if pending := a.tokenPreparations[host]; pending != nil { |
| 18 | a.mu.Unlock() |
| 19 | <-pending |
| 20 | continue |
| 21 | } |
| 22 | if a.tokenPreparations == nil { |
| 23 | a.tokenPreparations = make(map[string]chan struct{}) |
| 24 | } |
| 25 | done := make(chan struct{}) |
| 26 | a.tokenPreparations[host] = done |
| 27 | a.mu.Unlock() |
| 28 | defer func() { |
| 29 | a.mu.Lock() |
| 30 | delete(a.tokenPreparations, host) |
| 31 | close(done) |
| 32 | a.mu.Unlock() |
| 33 | }() |
| 34 | return prepare() |
| 35 | } |
| 36 | } |
| 37 | |