oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "sync"
6 "time"
7
8 "amadan.net/oficina/equipo/internal/wire"
9)
10
11// warmEvery is how often opening Base may warm one person's Assistant:
12// reading its snapshot or opening its stream asks at most this often.
13const warmEvery = 5 * time.Minute
14
15// warmGate remembers when each person's Assistant was last asked to
16// warm, in this process. Another process (an overlapping deploy) may
17// ask once more; a warm-up of a harness already up does nothing.
18type warmGate struct {
19 mu sync.Mutex
20 last map[string]time.Time
21}
22
23// WarmAssistant asks the person's primary runner to warm their
24// Assistant now (warm command, protocol 24): they opened Base, so the
25// first message should find the harness up. At most once per warmEvery
26// per person, and only to a primary online at 24 or later; nothing for
27// an older or offline one (an offline one warms on its own sync). The
28// runner decides whether the person used their Assistant lately.
29func (s *Store) WarmAssistant(ctx context.Context, person string) error {
30 now := s.now()
31 s.warm.mu.Lock()
32 if last, ok := s.warm.last[person]; ok && now.Sub(last) < warmEvery {
33 s.warm.mu.Unlock()
34 return nil
35 }
36 if s.warm.last == nil {
37 s.warm.last = map[string]time.Time{}
38 }
39 s.warm.last[person] = now
40 s.warm.mu.Unlock()
41 err := s.tx(ctx, func(t *txn) error {
42 online, protocol, err := t.primaryState(person)
43 if err != nil || !online || protocol < wire.ProtocolWarm {
44 return err
45 }
46 return t.command(person, wire.FrameWarm, wire.WarmCommand{Person: person})
47 })
48 if err != nil {
49 // Asked again on the next opening.
50 s.warm.mu.Lock()
51 delete(s.warm.last, person)
52 s.warm.mu.Unlock()
53 }
54 return err
55}
56