Fix trafficontrol Manager
This commit is contained in:
@@ -33,13 +33,15 @@ type ConnectionEvent struct {
|
|||||||
ClosedAt time.Time
|
ClosedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const closedConnectionsLimit = 1000
|
||||||
|
|
||||||
type Manager struct {
|
type Manager struct {
|
||||||
uploadTotal atomic.Int64
|
uploadTotal atomic.Int64
|
||||||
downloadTotal atomic.Int64
|
downloadTotal atomic.Int64
|
||||||
|
|
||||||
connections compatible.Map[uuid.UUID, Tracker]
|
connections compatible.Map[uuid.UUID, Tracker]
|
||||||
closedConnectionsAccess sync.Mutex
|
closedConnectionsAccess sync.Mutex
|
||||||
closedConnections list.List[*TrackerMetadata]
|
closedConnections list.List[TrackerMetadata]
|
||||||
memory uint64
|
memory uint64
|
||||||
|
|
||||||
eventSubscriber *observable.Subscriber[ConnectionEvent]
|
eventSubscriber *observable.Subscriber[ConnectionEvent]
|
||||||
@@ -69,19 +71,21 @@ func (m *Manager) Leave(c Tracker) {
|
|||||||
metadata := c.Metadata()
|
metadata := c.Metadata()
|
||||||
_, loaded := m.connections.LoadAndDelete(metadata.ID)
|
_, loaded := m.connections.LoadAndDelete(metadata.ID)
|
||||||
if loaded {
|
if loaded {
|
||||||
metadata.ClosedAt = time.Now()
|
closedAt := time.Now()
|
||||||
|
metadata.ClosedAt = closedAt
|
||||||
|
metadataCopy := *metadata
|
||||||
m.closedConnectionsAccess.Lock()
|
m.closedConnectionsAccess.Lock()
|
||||||
if m.closedConnections.Len() >= 1000 {
|
if m.closedConnections.Len() >= closedConnectionsLimit {
|
||||||
m.closedConnections.PopFront()
|
m.closedConnections.PopFront()
|
||||||
}
|
}
|
||||||
m.closedConnections.PushBack(metadata)
|
m.closedConnections.PushBack(metadataCopy)
|
||||||
m.closedConnectionsAccess.Unlock()
|
m.closedConnectionsAccess.Unlock()
|
||||||
if m.eventSubscriber != nil {
|
if m.eventSubscriber != nil {
|
||||||
m.eventSubscriber.Emit(ConnectionEvent{
|
m.eventSubscriber.Emit(ConnectionEvent{
|
||||||
Type: ConnectionEventClosed,
|
Type: ConnectionEventClosed,
|
||||||
ID: metadata.ID,
|
ID: metadata.ID,
|
||||||
Metadata: metadata,
|
Metadata: &metadataCopy,
|
||||||
ClosedAt: metadata.ClosedAt,
|
ClosedAt: closedAt,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,8 +118,16 @@ func (m *Manager) Connections() []*TrackerMetadata {
|
|||||||
|
|
||||||
func (m *Manager) ClosedConnections() []*TrackerMetadata {
|
func (m *Manager) ClosedConnections() []*TrackerMetadata {
|
||||||
m.closedConnectionsAccess.Lock()
|
m.closedConnectionsAccess.Lock()
|
||||||
defer m.closedConnectionsAccess.Unlock()
|
values := m.closedConnections.Array()
|
||||||
return m.closedConnections.Array()
|
m.closedConnectionsAccess.Unlock()
|
||||||
|
if len(values) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
connections := make([]*TrackerMetadata, len(values))
|
||||||
|
for i := range values {
|
||||||
|
connections[i] = &values[i]
|
||||||
|
}
|
||||||
|
return connections
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) Connection(id uuid.UUID) Tracker {
|
func (m *Manager) Connection(id uuid.UUID) Tracker {
|
||||||
|
|||||||
Reference in New Issue
Block a user