Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pion/turn
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: inhandnet/turn
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 2 contributors

Commits on Mar 11, 2024

  1. feat: calculate allocation usage

    xuym committed Mar 11, 2024
    Copy the full SHA
    e4b8b52 View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    caccfcc View commit details

Commits on Mar 12, 2024

  1. Merge pull request #1 from inhandnet/feat/cal-allocation-usage

    Feat/cal allocation usage
    xuym-inhand authored Mar 12, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    b235950 View commit details
Showing with 28 additions and 2 deletions.
  1. +6 −1 internal/allocation/allocation.go
  2. +11 −0 internal/allocation/allocation_manager.go
  3. +1 −1 internal/server/turn.go
  4. +10 −0 server.go
7 changes: 6 additions & 1 deletion internal/allocation/allocation.go
Original file line number Diff line number Diff line change
@@ -28,15 +28,16 @@ type Allocation struct {
Protocol Protocol
TurnSocket net.PacketConn
RelaySocket net.PacketConn
Usage int
fiveTuple *FiveTuple
permissionsLock sync.RWMutex
permissions map[string]*Permission
channelBindingsLock sync.RWMutex
channelBindings []*ChannelBind
lifetimeTimer *time.Timer
closed chan interface{}
log logging.LeveledLogger

log logging.LeveledLogger
// Some clients (Firefox or others using resiprocate's nICE lib) may retry allocation
// with same 5 tuple when received 413, for compatible with these clients,
// cache for response lost and client retry to implement 'stateless stack approach'
@@ -55,6 +56,10 @@ func NewAllocation(turnSocket net.PacketConn, fiveTuple *FiveTuple, log logging.
}
}

func (a *Allocation) AddUsage(usage int) {
a.Usage += usage
}

// GetPermission gets the Permission from the allocation
func (a *Allocation) GetPermission(addr net.Addr) *Permission {
a.permissionsLock.RLock()
11 changes: 11 additions & 0 deletions internal/allocation/allocation_manager.go
Original file line number Diff line number Diff line change
@@ -72,6 +72,17 @@ func (m *Manager) AllocationCount() int {
return len(m.allocations)
}

func (m *Manager) AllocationUsage() map[string]int {
m.lock.RLock()
defer m.lock.RUnlock()
usage := make(map[string]int)
for k, a := range m.allocations {
usage[k] = a.Usage
}

return usage
}

// Close closes the manager and closes all allocations it manages
func (m *Manager) Close() error {
m.lock.Lock()
2 changes: 1 addition & 1 deletion internal/server/turn.go
Original file line number Diff line number Diff line change
@@ -381,6 +381,6 @@ func handleChannelData(r Request, c *proto.ChannelData) error {
} else if l != len(c.Data) {
return fmt.Errorf("%w %d != %d (expected)", errShortWrite, l, len(c.Data))
}

a.AddUsage(len(r.Buff))
return nil
}
10 changes: 10 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -114,6 +114,16 @@ func (s *Server) AllocationCount() int {
return allocs
}

func (s *Server) AllocationUsage() map[string]int {
usageMap := make(map[string]int)
for _, am := range s.allocationManagers {
for k, u := range am.AllocationUsage() {
usageMap[k] = u
}
}
return usageMap
}

// Close stops the TURN Server. It cleans up any associated state and closes all connections it is managing
func (s *Server) Close() error {
var errors []error