From 7ae31d0ec96ef3d5cfdfffdcd7f2e480a59b5408 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Mon, 13 Jan 2025 15:11:44 +0100 Subject: [PATCH 1/2] Make Registry.Build take extra interceptors Make Registry.Build take an optional list of extra interceptors that are added before the ones specified by the registry. --- registry.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/registry.go b/registry.go index e36ef6bf..e9451961 100644 --- a/registry.go +++ b/registry.go @@ -14,12 +14,17 @@ func (r *Registry) Add(f Factory) { } // Build constructs a single Interceptor from a InterceptorRegistry -func (r *Registry) Build(id string) (Interceptor, error) { +// The extra interceptors are added to the chain before the ones specified +// by the registry. +func (r *Registry) Build(id string, extra... Interceptor) (Interceptor, error) { if len(r.factories) == 0 { return &NoOp{}, nil } interceptors := []Interceptor{} + + interceptors = append(interceptors, extra...) + for _, f := range r.factories { i, err := f.NewInterceptor(id) if err != nil { From 888fadd4e4cbaac42663b392f6f9e9b8d50da67e Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Mon, 13 Jan 2025 16:18:41 +0100 Subject: [PATCH 2/2] Implement cc.NewSingleInterceptor Creates a single interceptor without going through a factory. --- pkg/cc/interceptor.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/pkg/cc/interceptor.go b/pkg/cc/interceptor.go index 252ab29f..5d8da82d 100644 --- a/pkg/cc/interceptor.go +++ b/pkg/cc/interceptor.go @@ -61,11 +61,25 @@ func (f *InterceptorFactory) OnNewPeerConnection(cb NewPeerConnectionCallback) { } // NewInterceptor returns a new CC interceptor +// Don't call this, call [NewSingleInterceptor] instead. func (f *InterceptorFactory) NewInterceptor(id string) (interceptor.Interceptor, error) { bwe, err := f.bweFactory() if err != nil { return nil, err } + i, err := NewSingleInterceptor(bwe, f.opts...) + if err != nil { + return nil, err + } + + if f.addPeerConnection != nil { + f.addPeerConnection(id, i.estimator) + } + return i, nil +} + +// NewSingleInterceptor returns a new CC interceptor +func NewSingleInterceptor(bwe BandwidthEstimator, options ...Option) (*Interceptor, error) { i := &Interceptor{ NoOp: interceptor.NoOp{}, estimator: bwe, @@ -73,15 +87,11 @@ func (f *InterceptorFactory) NewInterceptor(id string) (interceptor.Interceptor, close: make(chan struct{}), } - for _, opt := range f.opts { + for _, opt := range options { if err := opt(i); err != nil { return nil, err } } - - if f.addPeerConnection != nil { - f.addPeerConnection(id, i.estimator) - } return i, nil }