S2 take once: keep the ids a holder took in its state taken
A holder of a kept call takes an ask once by its id: TakeOnce creates <seat>.<ask> in the state taken with the ask's bound as its time to live, and a second ask of the same id within it answers false. Builds: design 47 H2 §775, §820; H-A-338 (6), H-A-339 (4)
This commit is contained in:
@@ -398,3 +398,28 @@ func (s State) delete(key string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TakenState is the state a holder of a kept call keeps the ids of the asks it took in (design 47 H2 §775, §820): a
|
||||
// state of 8 MiB whose keys live the ask's bound and are deleted by their time to live alone, never pruned.
|
||||
const TakenState = "taken"
|
||||
|
||||
// TakenKey is the key an ask taken from seat is kept under in TakenState: `<seat>.<ask>`.
|
||||
func TakenKey(seat, ask string) string { return seat + "." + ask }
|
||||
|
||||
// TakeOnce says whether this holder takes the ask ask of seat now (design 47 H2 §775; ADR 0668 §2): the first time it
|
||||
// is given, it is kept under TakenKey for the ask's bound and TakeOnce answers true; given again within the bound,
|
||||
// false, and the holder acts once. s is the holder's TakenState, declared `key-ttl`. An ask with no id, or an id that is
|
||||
// not one key, is refused: a kept call carries its id.
|
||||
func (s State) TakeOnce(seat, ask string, bound time.Duration) (bool, error) {
|
||||
if err := checkAskID(ask); err != nil {
|
||||
return false, fmt.Errorf("taking an ask of %s: %w", seat, err)
|
||||
}
|
||||
if seat == "" || strings.ContainsAny(seat, ".*> ") {
|
||||
return false, fmt.Errorf("taking the ask %s: %q is not a seat's name", ask, seat)
|
||||
}
|
||||
_, err := s.CreateTTL(TakenKey(seat, ask), map[string]string{"seat": seat, "ask": ask}, max(bound, MinTTL))
|
||||
if errors.Is(err, stdio.ErrChanged) {
|
||||
return false, nil
|
||||
}
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user