|
| 1 | +// SPDX-FileCopyrightText: 2025 The Pion community <https://pion.ly> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// Package pacing implements a pacing interceptor. |
| 5 | +package pacing |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "log/slog" |
| 10 | + "maps" |
| 11 | + "sync" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/pion/interceptor" |
| 15 | + "github.com/pion/logging" |
| 16 | + "github.com/pion/rtp" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + errPacerClosed = errors.New("pacer closed") |
| 21 | + errPacerOverflow = errors.New("pacer queue overflow") |
| 22 | +) |
| 23 | + |
| 24 | +type pacerFactory func(initialRate, burst int) pacer |
| 25 | + |
| 26 | +type pacer interface { |
| 27 | + SetRate(rate, burst int) |
| 28 | + Budget(time.Time) float64 |
| 29 | + AllowN(time.Time, int) bool |
| 30 | +} |
| 31 | + |
| 32 | +// Option is a configuration option for pacing interceptors. |
| 33 | +type Option func(*Interceptor) error |
| 34 | + |
| 35 | +// InitialRate configures the initial pacing rate for interceptors created by |
| 36 | +// the interceptor factory. |
| 37 | +func InitialRate(rate int) Option { |
| 38 | + return func(i *Interceptor) error { |
| 39 | + i.initialRate = rate |
| 40 | + |
| 41 | + return nil |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// Interval configures the pacing interval for interceptors created by the |
| 46 | +// interceptor factory. |
| 47 | +func Interval(interval time.Duration) Option { |
| 48 | + return func(i *Interceptor) error { |
| 49 | + i.interval = interval |
| 50 | + |
| 51 | + return nil |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func setPacerFactory(f pacerFactory) Option { |
| 56 | + return func(i *Interceptor) error { |
| 57 | + i.pacerFactory = f |
| 58 | + |
| 59 | + return nil |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// InterceptorFactory is a factory for pacing interceptors. It also keeps a map |
| 64 | +// of interceptors created in the past by ID. |
| 65 | +type InterceptorFactory struct { |
| 66 | + lock sync.Mutex |
| 67 | + opts []Option |
| 68 | + interceptors map[string]*Interceptor |
| 69 | +} |
| 70 | + |
| 71 | +// NewInterceptor returns a new InterceptorFactory. |
| 72 | +func NewInterceptor(opts ...Option) *InterceptorFactory { |
| 73 | + return &InterceptorFactory{ |
| 74 | + lock: sync.Mutex{}, |
| 75 | + opts: opts, |
| 76 | + interceptors: map[string]*Interceptor{}, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// SetRate updates the pacing rate of the pacing interceptor with the given ID. |
| 81 | +func (f *InterceptorFactory) SetRate(id string, r int) { |
| 82 | + f.lock.Lock() |
| 83 | + defer f.lock.Unlock() |
| 84 | + |
| 85 | + i, ok := f.interceptors[id] |
| 86 | + if !ok { |
| 87 | + return |
| 88 | + } |
| 89 | + i.setRate(r) |
| 90 | +} |
| 91 | + |
| 92 | +// NewInterceptor creates a new pacing interceptor. |
| 93 | +func (f *InterceptorFactory) NewInterceptor(id string) (interceptor.Interceptor, error) { |
| 94 | + f.lock.Lock() |
| 95 | + defer f.lock.Unlock() |
| 96 | + |
| 97 | + interceptor := &Interceptor{ |
| 98 | + NoOp: interceptor.NoOp{}, |
| 99 | + log: logging.NewDefaultLoggerFactory().NewLogger("pacer_interceptor"), |
| 100 | + initialRate: 1_000_000, |
| 101 | + interval: 5 * time.Millisecond, |
| 102 | + queueSize: 1_000_000, |
| 103 | + pacerFactory: func(initialRate, burst int) pacer { |
| 104 | + return newRateLimitPacer(initialRate, burst) |
| 105 | + }, |
| 106 | + limit: nil, |
| 107 | + queue: nil, |
| 108 | + closed: make(chan struct{}), |
| 109 | + wg: sync.WaitGroup{}, |
| 110 | + } |
| 111 | + for _, opt := range f.opts { |
| 112 | + if err := opt(interceptor); err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + } |
| 116 | + interceptor.limit = interceptor.pacerFactory( |
| 117 | + interceptor.initialRate, |
| 118 | + burst(interceptor.initialRate, interceptor.interval), |
| 119 | + ) |
| 120 | + interceptor.queue = make(chan packet, interceptor.queueSize) |
| 121 | + |
| 122 | + f.interceptors[id] = interceptor |
| 123 | + |
| 124 | + interceptor.wg.Add(1) |
| 125 | + go func() { |
| 126 | + defer interceptor.wg.Done() |
| 127 | + interceptor.loop() |
| 128 | + }() |
| 129 | + |
| 130 | + return interceptor, nil |
| 131 | +} |
| 132 | + |
| 133 | +// Interceptor implements packet pacing using a token bucket filter and sends |
| 134 | +// packets at a fixed interval. |
| 135 | +type Interceptor struct { |
| 136 | + interceptor.NoOp |
| 137 | + log logging.LeveledLogger |
| 138 | + |
| 139 | + // config |
| 140 | + initialRate int |
| 141 | + interval time.Duration |
| 142 | + queueSize int |
| 143 | + pacerFactory pacerFactory |
| 144 | + |
| 145 | + // limiter and queue |
| 146 | + limit pacer |
| 147 | + queue chan packet |
| 148 | + |
| 149 | + // shutdown |
| 150 | + closed chan struct{} |
| 151 | + wg sync.WaitGroup |
| 152 | +} |
| 153 | + |
| 154 | +// burst calculates the minimal burst size required to reach the given rate and |
| 155 | +// pacing interval. |
| 156 | +func burst(rate int, interval time.Duration) int { |
| 157 | + if interval == 0 { |
| 158 | + interval = time.Millisecond |
| 159 | + } |
| 160 | + f := float64(time.Second.Milliseconds() / interval.Milliseconds()) |
| 161 | + |
| 162 | + return 8 * int(float64(rate)/f) |
| 163 | +} |
| 164 | + |
| 165 | +// setRate updates the pacing rate and burst of the rate limiter. |
| 166 | +func (i *Interceptor) setRate(r int) { |
| 167 | + i.limit.SetRate(r, burst(r, i.interval)) |
| 168 | +} |
| 169 | + |
| 170 | +// BindLocalStream implements interceptor.Interceptor. |
| 171 | +func (i *Interceptor) BindLocalStream( |
| 172 | + info *interceptor.StreamInfo, |
| 173 | + writer interceptor.RTPWriter, |
| 174 | +) interceptor.RTPWriter { |
| 175 | + return interceptor.RTPWriterFunc(func( |
| 176 | + header *rtp.Header, |
| 177 | + payload []byte, |
| 178 | + attributes interceptor.Attributes, |
| 179 | + ) (int, error) { |
| 180 | + hdr := header.Clone() |
| 181 | + pay := make([]byte, len(payload)) |
| 182 | + copy(pay, payload) |
| 183 | + attr := maps.Clone(attributes) |
| 184 | + select { |
| 185 | + case i.queue <- packet{ |
| 186 | + writer: writer, |
| 187 | + header: &hdr, |
| 188 | + payload: pay, |
| 189 | + attributes: attr, |
| 190 | + }: |
| 191 | + case <-i.closed: |
| 192 | + return 0, errPacerClosed |
| 193 | + default: |
| 194 | + return 0, errPacerOverflow |
| 195 | + } |
| 196 | + |
| 197 | + return header.MarshalSize() + len(payload), nil |
| 198 | + }) |
| 199 | +} |
| 200 | + |
| 201 | +// Close implements interceptor.Interceptor. |
| 202 | +func (i *Interceptor) Close() error { |
| 203 | + defer i.wg.Done() |
| 204 | + |
| 205 | + return nil |
| 206 | +} |
| 207 | + |
| 208 | +func (i *Interceptor) loop() { |
| 209 | + ticker := time.NewTicker(i.interval) |
| 210 | + queue := make([]packet, 0) |
| 211 | + for { |
| 212 | + select { |
| 213 | + case now := <-ticker.C: |
| 214 | + for len(queue) > 0 && i.limit.Budget(now) > 8*float64(queue[0].len()) { |
| 215 | + i.limit.AllowN(now, 8*queue[0].len()) |
| 216 | + var next packet |
| 217 | + next, queue = queue[0], queue[1:] |
| 218 | + if _, err := next.writer.Write(next.header, next.payload, next.attributes); err != nil { |
| 219 | + slog.Warn("error on writing RTP packet", "error", err) |
| 220 | + } |
| 221 | + } |
| 222 | + case pkt := <-i.queue: |
| 223 | + queue = append(queue, pkt) |
| 224 | + case <-i.closed: |
| 225 | + return |
| 226 | + } |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +type packet struct { |
| 231 | + writer interceptor.RTPWriter |
| 232 | + header *rtp.Header |
| 233 | + payload []byte |
| 234 | + attributes interceptor.Attributes |
| 235 | +} |
| 236 | + |
| 237 | +func (p *packet) len() int { |
| 238 | + return p.header.MarshalSize() + len(p.payload) |
| 239 | +} |
0 commit comments