package federation import ( "context" "sync" "time" ) // What a friend is held to while it reads: a few streams at a time, and a number of bytes a second shared by all // of them. The numbers are the admin's (see Settings). const ( minKiBPerSec = 64 maxKiBPerSec = 100 << 10 maxStreamsCap = 20 // A burst of this much time at the full rate is let through at once, so a small file is not slowed down. pacerBurst = 250 * time.Millisecond ) // pacer spaces out bytes: each Wait says when the allowance for those bytes is used up and sleeps until then. type pacer struct { mu sync.Mutex kib int next time.Time perByte time.Duration } func newPacer(kib int) *pacer { return &pacer{kib: kib, perByte: time.Second / time.Duration(kib*1024)} } func (p *pacer) wait(ctx context.Context, n int, now func() time.Time, sleep func(context.Context, time.Duration) error) error { p.mu.Lock() t := now() if p.next.Before(t) { p.next = t } p.next = p.next.Add(time.Duration(n) * p.perByte) delay := p.next.Sub(t) - pacerBurst p.mu.Unlock() if delay <= 0 { return nil } return sleep(ctx, delay) } // Stream is a place among the streams a friend may have open. Close it when the stream ends. type Stream struct { s *Service friend int64 slot bool // a place is taken, to give back on Close pace *pacer // nil: no limit once sync.Once } // Wait is called before sending n bytes: it sleeps as long as the friend's allowance asks. func (st *Stream) Wait(ctx context.Context, n int) error { if st.pace == nil { return nil } return st.pace.wait(ctx, n, st.s.now, st.s.sleep) } // Close gives the place back. It may be called more than once. func (st *Stream) Close() { st.once.Do(func() { if !st.slot { return } st.s.mu.Lock() defer st.s.mu.Unlock() if st.s.streams[st.friend] > 0 { st.s.streams[st.friend]-- } }) } // StartStream takes a place for a friend, or says ErrTooManyStreams. The limits are read every time, so a change // of the admin applies to the next stream. func (s *Service) StartStream(ctx context.Context, friendID int64) (*Stream, error) { return s.start(ctx, friendID, true) } // Pace is a Stream that takes no place among the streams, only a share of the bytes: for what is small and comes // many at a time, like covers. func (s *Service) Pace(ctx context.Context, friendID int64) (*Stream, error) { return s.start(ctx, friendID, false) } func (s *Service) start(ctx context.Context, friendID int64, slot bool) (*Stream, error) { set, err := s.load(ctx) if err != nil { return nil, err } s.mu.Lock() defer s.mu.Unlock() if slot { if s.streams[friendID] >= set.MaxStreams { return nil, ErrTooManyStreams } s.streams[friendID]++ } st := &Stream{s: s, friend: friendID, slot: slot} if set.MaxKiBPerSec > 0 { p := s.pacers[friendID] if p == nil || p.kib != set.MaxKiBPerSec { p = newPacer(set.MaxKiBPerSec) s.pacers[friendID] = p } st.pace = p } return st, nil } func sleepFor(ctx context.Context, d time.Duration) error { t := time.NewTimer(d) defer t.Stop() select { case <-t.C: return nil case <-ctx.Done(): return ctx.Err() } }