Skip to content

Commit

Permalink
chore: Implement quota handler stub
Browse files Browse the repository at this point in the history
  • Loading branch information
rg0now committed Dec 10, 2024
1 parent 835c1b2 commit 378f31e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
29 changes: 29 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,35 @@ func (s *Stunner) NewStatusHandler() object.StatusHandler {
return func() stnrv1.Status { return s.Status() }
}

// Quota handler
type QuotaHandler interface {
QuotaHandler() turn.QuotaHandler
}

var quotaHandlerConstructor = newQuotaHandlerStub

// NewUserQuotaHandler creates a quota handler that defaults to a stub.
func (s *Stunner) NewQuotaHandler() QuotaHandler {
return quotaHandlerConstructor(s)
}

type quotaHandlerStub struct {
quotaHandler turn.QuotaHandler
}

func (q *quotaHandlerStub) QuotaHandler() turn.QuotaHandler {
return q.quotaHandler
}

func newQuotaHandlerStub(_ *Stunner) QuotaHandler {
return &quotaHandlerStub{
quotaHandler: func(_, _ string, _ net.Addr) (ok bool) {
return true
},
}
}

// Event handlers
var eventHandlerConstructor = newEventHandlerStub

// NewEventHandler creates a set of callbcks for tracking the lifecycle of TURN allocations.
Expand Down
3 changes: 2 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (s *Stunner) StartServer(l *object.Listener) error {
t, err := turn.NewServer(turn.ServerConfig{
Realm: s.GetRealm(),
AuthHandler: s.NewAuthHandler(),
EventHandlers: s.NewEventHandler(),
EventHandlers: s.eventHandlers,
QuotaHandler: s.quotaHandler,
PacketConnConfigs: pConns,
ListenerConfigs: lConns,
LoggerFactory: logger.NewRateLimitedLoggerFactory(s.logger, LogRateLimit, LogBurst),
Expand Down
5 changes: 5 additions & 0 deletions stunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/pion/logging"
"github.com/pion/transport/v3"
"github.com/pion/transport/v3/stdnet"
"github.com/pion/turn/v4"

"github.com/l7mp/stunner/internal/manager"
"github.com/l7mp/stunner/internal/object"
Expand All @@ -32,6 +33,8 @@ type Stunner struct {
telemetry *telemetry.Telemetry
logger logger.LoggerFactory
log logging.LeveledLogger
eventHandlers turn.EventHandlers
quotaHandler turn.QuotaHandler
net transport.Net
ready, shutdown bool
}
Expand Down Expand Up @@ -101,6 +104,8 @@ func NewStunner(options Options) *Stunner {
object.NewListenerFactory(vnet, s.NewRealmHandler(), logger), logger)
s.clusterManager = manager.NewManager("cluster-manager",
object.NewClusterFactory(r, logger), logger)
s.eventHandlers = s.NewEventHandler()
s.quotaHandler = s.NewQuotaHandler().QuotaHandler()

telemetryCallbacks := telemetry.Callbacks{
GetAllocationCount: func() int64 { return s.GetActiveConnections() },
Expand Down

0 comments on commit 378f31e

Please sign in to comment.