Files
livekit/pkg/service/turnquota.go
T
Raja SubramanianandClaude Opus 4.8 223587e140 Add per-participant concurrent TURN allocation quota (#4744)
* Add per-participant concurrent TURN allocation quota

The embedded TURN server authenticated each Allocate request but placed
no cap on how many relay allocations a single participant credential
could hold. One participant could reuse its credential across many client
5-tuples and open one relay socket/port per request, exhausting the
shared relay-port range for everyone else.

Add a configurable per-participant limit (turn.per_user_relay_allocation_limit,
default 4) wired to Pion's QuotaHandler, keyed by the participant ID from
HandleAuth. Slots are reserved before allocation and released when the
allocation ends, under a single lock, so concurrent Allocate bursts cannot
race past the limit; reservations are keyed by source address so retransmits
are idempotent. Over-quota requests receive 486 (Allocation Quota Reached).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Reclaim unconfirmed TURN allocation reservations

Allow reserved a quota slot before Pion built the relay, but the slot was
only released on the allocation-deleted event. An Allocate that passed the
quota check and then failed to create a relay (e.g. relay-port range
exhausted) emits no event, so the reservation leaked: after enough failures
a participant could lock itself out with 486, and the tracking map grew
without bound.

Reservations now start pending and are confirmed on allocation-created; each
pending reservation carries a reclaim timer that frees the slot after a TTL,
so a failed attempt cannot hold a slot forever while concurrent-burst safety
is preserved.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* Make TURN reservation reclaim identity-aware

The reclaim timer captured only userID+key. Because Timer.Stop cannot cancel
a callback that has already fired and is waiting on the lock, a stale timer
could delete a replacement reservation created for the same userID+key after
the original was released, leaving a live allocation untracked and letting the
participant exceed its cap.

reclaimPending now captures the slot pointer and only removes the entry when
the map still holds that exact slot, so a stale timer is a no-op.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-08-11 23:12:07 +05:30

188 lines
6.1 KiB
Go

// Copyright 2026 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"net"
"sync"
"time"
"github.com/pion/turn/v5"
"github.com/livekit/protocol/logger"
)
// defaultTURNReservationTTL bounds how long a reserved-but-unconfirmed
// allocation slot may live. Pion calls the QuotaHandler and then synchronously
// creates the allocation, so a healthy reservation is confirmed within
// microseconds; this generous window only matters if allocation creation fails
// after the quota check (see turnAllocationQuota).
const defaultTURNReservationTTL = 30 * time.Second
// turnAllocationQuota enforces a per-user cap on concurrent relay allocations on
// the embedded TURN server. Without it, a single authenticated participant can
// reuse its credential across many client 5-tuples and open one relay socket/port
// per request, exhausting the shared relay-port range for every other participant.
//
// The quota is keyed by the user ID returned from TURNAuthHandler.HandleAuth,
// which is the stable participant ID embedded in the signed TURN username. A
// participant cannot forge a different ID without the API secret, so this bounds
// the total relay footprint of any one participant.
//
// Slots are reserved in Allow (which Pion calls before creating an allocation)
// and released in OnDeleted, all under a single lock, so a burst of concurrent
// Allocate requests cannot race past the limit. Reservations are keyed by the
// client source address so that Allocate retransmissions from the same 5-tuple
// are idempotent rather than double-counted.
//
// A reservation starts out pending and is confirmed in OnCreated. Pion emits no
// event when an Allocate passes the quota check but then fails to create a relay
// (e.g. the relay-port range is exhausted), so each pending reservation carries a
// reclaim timer that frees the slot after reservationTTL. This keeps a failed
// attempt from occupying a slot forever, which would otherwise let a participant
// lock itself out and grow the tracking map without bound.
type turnAllocationQuota struct {
limit int
reservationTTL time.Duration
mu sync.Mutex
users map[string]map[string]*turnAllocationSlot // userID -> source-address key -> slot
}
type turnAllocationSlot struct {
confirmed bool
timer *time.Timer
}
func newTURNAllocationQuota(limit int) *turnAllocationQuota {
return &turnAllocationQuota{
limit: limit,
reservationTTL: defaultTURNReservationTTL,
users: make(map[string]map[string]*turnAllocationSlot),
}
}
func srcAddrKey(srcAddr net.Addr) string {
if srcAddr == nil {
return ""
}
// include the network so an ip:port reused across UDP and TCP relays is not
// collapsed into a single slot
return srcAddr.Network() + "|" + srcAddr.String()
}
// Allow implements turn.QuotaHandler. It returns true if the allocation may
// proceed, reserving a slot for the user, and false (Pion replies 486 Allocation
// Quota Reached) once the user is at its limit.
func (q *turnAllocationQuota) Allow(userID, _ string, srcAddr net.Addr) bool {
key := srcAddrKey(srcAddr)
q.mu.Lock()
defer q.mu.Unlock()
slots := q.users[userID]
if _, ok := slots[key]; ok {
// already reserved for this 5-tuple (retransmit / create race) - idempotent
return true
}
if len(slots) >= q.limit {
logger.Infow("TURN allocation quota reached",
"participantID", userID,
"limit", q.limit,
)
return false
}
if slots == nil {
slots = make(map[string]*turnAllocationSlot)
q.users[userID] = slots
}
// reserve a pending slot; reclaim it if the allocation is never created so a
// failed attempt (Pion emits no created/deleted event) cannot hold it forever
slot := &turnAllocationSlot{}
slot.timer = time.AfterFunc(q.reservationTTL, func() {
q.reclaimPending(userID, key, slot)
})
slots[key] = slot
return true
}
// OnCreated confirms a reservation once the relay allocation actually exists,
// cancelling its reclaim timer so a live allocation is never evicted early.
func (q *turnAllocationQuota) OnCreated(srcAddr, _ net.Addr, _, userID, _ string, _ net.Addr, _ int) {
key := srcAddrKey(srcAddr)
q.mu.Lock()
defer q.mu.Unlock()
slot := q.users[userID][key]
if slot == nil {
return
}
slot.confirmed = true
if slot.timer != nil {
slot.timer.Stop()
slot.timer = nil
}
}
// OnDeleted releases the slot reserved in Allow so the user regains capacity once
// an allocation ends.
func (q *turnAllocationQuota) OnDeleted(srcAddr, _ net.Addr, _, userID, _ string) {
key := srcAddrKey(srcAddr)
q.mu.Lock()
defer q.mu.Unlock()
q.removeLocked(userID, key)
}
// reclaimPending drops a reservation that was never confirmed, freeing a slot
// left behind by an Allocate that passed the quota but failed to create a relay.
// It is identity-aware: a timer that fires after its slot was already released
// must not evict a replacement reservation created for the same userID+key.
func (q *turnAllocationQuota) reclaimPending(userID, key string, slot *turnAllocationSlot) {
q.mu.Lock()
defer q.mu.Unlock()
if q.users[userID][key] != slot || slot.confirmed {
return
}
q.removeLocked(userID, key)
}
func (q *turnAllocationQuota) removeLocked(userID, key string) {
slots := q.users[userID]
slot := slots[key]
if slot == nil {
return
}
if slot.timer != nil {
slot.timer.Stop()
}
delete(slots, key)
if len(slots) == 0 {
delete(q.users, userID)
}
}
// eventHandler returns the Pion EventHandler wired to confirm and release quota
// slots as allocations are created and torn down.
func (q *turnAllocationQuota) eventHandler() turn.EventHandler {
return turn.EventHandler{
OnAllocationCreated: q.OnCreated,
OnAllocationDeleted: q.OnDeleted,
}
}