34 lines
456 B
Go
34 lines
456 B
Go
// backend\tasks_exclusive.go
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
var exclusiveTaskSem = make(chan struct{}, 1)
|
|
|
|
func init() {
|
|
exclusiveTaskSem <- struct{}{}
|
|
}
|
|
|
|
func acquireExclusiveTask(ctx context.Context) error {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-exclusiveTaskSem:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func releaseExclusiveTask() {
|
|
select {
|
|
case exclusiveTaskSem <- struct{}{}:
|
|
default:
|
|
}
|
|
}
|