-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57c1fdc
commit ec6d932
Showing
20 changed files
with
235 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package poolui | ||
|
||
import ( | ||
"context" | ||
"github.com/hephbuild/heph/specs" | ||
"github.com/hephbuild/heph/worker2" | ||
"sync" | ||
) | ||
|
||
func NewApprover() *Approver { | ||
return &Approver{ | ||
queue: make(chan ApproveRequest), | ||
} | ||
} | ||
|
||
type ApproveRequest struct { | ||
Target specs.Target | ||
Respond func(bool) | ||
} | ||
|
||
type Approver struct { | ||
m sync.Mutex | ||
connected bool | ||
queue chan ApproveRequest | ||
} | ||
|
||
func (a *Approver) SetConnected(connected bool) { | ||
a.m.Lock() | ||
defer a.m.Unlock() | ||
|
||
a.connected = connected | ||
|
||
if !connected { | ||
for { | ||
select { | ||
case req := <-a.queue: | ||
req.Respond(false) | ||
default: | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (a *Approver) Approve(ctx context.Context, t specs.Target) bool { | ||
a.m.Lock() | ||
|
||
if !a.connected { | ||
a.m.Unlock() | ||
return false | ||
} | ||
a.m.Unlock() | ||
|
||
valueCh := make(chan bool) | ||
defer close(valueCh) | ||
|
||
err := worker2.WaitChanSend(ctx, a.queue, ApproveRequest{ | ||
Target: t, | ||
Respond: func(approved bool) { | ||
valueCh <- approved | ||
}, | ||
}) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
res, err := worker2.WaitChanReceive(ctx, valueCh) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return res | ||
} | ||
|
||
func (a *Approver) Next() ApproveRequest { | ||
return <-a.queue | ||
} |
Oops, something went wrong.