| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package sctp |
| 5 |
|
| 6 |
import "maps" |
| 7 |
|
| 8 |
type interleavingSettings struct { |
| 9 |
newStreamScheduler InterleavingStreamSchedulerFactory |
| 10 |
wfqWeights map[uint16]uint16 |
| 11 |
} |
| 12 |
|
| 13 |
func setWeightedFairQueueingStreamScheduler(s *interleavingSettings) { |
| 14 |
weights := maps.Clone(s.wfqWeights) |
| 15 |
s.newStreamScheduler = func() InterleavingStreamScheduler { |
| 16 |
return newWeightedFairQueueingPendingQueuePolicy(weights) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
func cloneInterleavingSettings(s *interleavingSettings) *interleavingSettings { |
| 21 |
if s == nil { |
| 22 |
return &interleavingSettings{} |
| 23 |
} |
| 24 |
|
| 25 |
clone := &interleavingSettings{ |
| 26 |
newStreamScheduler: s.newStreamScheduler, |
| 27 |
} |
| 28 |
if len(s.wfqWeights) > 0 { |
| 29 |
clone.wfqWeights = make(map[uint16]uint16, len(s.wfqWeights)) |
| 30 |
maps.Copy(clone.wfqWeights, s.wfqWeights) |
| 31 |
} |
| 32 |
|
| 33 |
return clone |
| 34 |
} |
| 35 |
|
| 36 |
// StreamSchedulerChunk is the scheduler-visible view of a DATA or I-DATA chunk. |
| 37 |
// Stream reset chunks are control markers queued with data to preserve ordering. |
| 38 |
type StreamSchedulerChunk interface { |
| 39 |
StreamIdentifier() uint16 |
| 40 |
UserDataLen() int |
| 41 |
IsStreamReset() bool |
| 42 |
|
| 43 |
chunkPayloadData() *chunkPayloadData |
| 44 |
} |
| 45 |
|
| 46 |
// InterleavingStreamScheduler schedules I-DATA chunks across streams when RFC 8260 |
| 47 |
// user message interleaving is negotiated. |
| 48 |
type InterleavingStreamScheduler interface { |
| 49 |
// Reset clears scheduler state before use and when the scheduler is detached. |
| 50 |
Reset() |
| 51 |
Push(StreamSchedulerChunk) |
| 52 |
Peek() StreamSchedulerChunk |
| 53 |
Pop(StreamSchedulerChunk) error |
| 54 |
} |
| 55 |
|
| 56 |
// InterleavingStreamSchedulerFactory creates an empty interleaving stream scheduler. |
| 57 |
type InterleavingStreamSchedulerFactory func() InterleavingStreamScheduler |
| 58 |
|
| 59 |
// AssociationInterleavingOption configures RFC 8260 user message interleaving. |
| 60 |
type AssociationInterleavingOption func(*interleavingSettings) error |
| 61 |
|
| 62 |
// WithInterleavingStreamSchedulerFactory selects a custom stream scheduler factory |
| 63 |
// used when user message interleaving is negotiated. |
| 64 |
func WithInterleavingStreamSchedulerFactory( |
| 65 |
newScheduler InterleavingStreamSchedulerFactory, |
| 66 |
) AssociationInterleavingOption { |
| 67 |
return func(s *interleavingSettings) error { |
| 68 |
if newScheduler == nil { |
| 69 |
return errNilStreamScheduler |
| 70 |
} |
| 71 |
s.newStreamScheduler = newScheduler |
| 72 |
|
| 73 |
return nil |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// WithInterleavingWeightedFairQueueingScheduler selects the built-in RFC 8260 |
| 78 |
// weighted fair queueing stream scheduler. |
| 79 |
func WithInterleavingWeightedFairQueueingScheduler() AssociationInterleavingOption { |
| 80 |
return func(s *interleavingSettings) error { |
| 81 |
setWeightedFairQueueingStreamScheduler(s) |
| 82 |
|
| 83 |
return nil |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// WithInterleavingWeightedFairQueueingWeight sets the WFQ weight for a stream. |
| 88 |
// Larger weights receive proportionally more scheduling service when multiple |
| 89 |
// streams are backlogged. |
| 90 |
func WithInterleavingWeightedFairQueueingWeight(streamID uint16, weight uint16) AssociationInterleavingOption { |
| 91 |
return func(s *interleavingSettings) error { |
| 92 |
if weight == 0 { |
| 93 |
return errInvalidStreamSchedulerWeight |
| 94 |
} |
| 95 |
if s.wfqWeights == nil { |
| 96 |
s.wfqWeights = map[uint16]uint16{} |
| 97 |
} |
| 98 |
s.wfqWeights[streamID] = weight |
| 99 |
setWeightedFairQueueingStreamScheduler(s) |
| 100 |
|
| 101 |
return nil |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// WithInterleavingRoundRobinScheduler selects the built-in round-robin stream scheduler. |
| 106 |
func WithInterleavingRoundRobinScheduler() AssociationInterleavingOption { |
| 107 |
return func(s *interleavingSettings) error { |
| 108 |
s.newStreamScheduler = func() InterleavingStreamScheduler { |
| 109 |
return newRoundRobinPendingQueuePolicy() |
| 110 |
} |
| 111 |
|
| 112 |
return nil |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
// WithInterleavingOptions configures RFC 8260 user message interleaving options. |
| 117 |
// The default interleaving stream scheduler is weighted fair queueing. |
| 118 |
func WithInterleavingOptions(opts ...AssociationInterleavingOption) AssociationOption { |
| 119 |
return sharedOption(func(c *Config) error { |
| 120 |
cfg := cloneInterleavingSettings(c.interleaving) |
| 121 |
for _, opt := range opts { |
| 122 |
if opt == nil { |
| 123 |
continue |
| 124 |
} |
| 125 |
|
| 126 |
if err := opt(cfg); err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
c.interleaving = cfg |
| 132 |
|
| 133 |
return nil |
| 134 |
}) |
| 135 |
} |