mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 08:54:02 +03:00
FILE / ScuroNeko/mtg
mtglib/internal/doppel/scout_conn_collected.go
Исходный файл и его история в репозитории.
1. Add sync.Mutex to ScoutConnCollected to eliminate data race between Add()/MarkWrite() in readLoop and learn() iterating results. Introduce Snapshot() for safe read access. 2. Increase bloom filter test size from 500 to 100000 to prevent false negatives from random eviction in the stable bloom filter. 3. Use Require().NoError() in TestHTTPSRequest to prevent nil-pointer panic on resp.Body.Close() when the request fails. Fixes #425
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package doppel
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
ScoutConnCollectedPreallocSize = 100
|
|
)
|
|
|
|
type ScoutConnResult struct {
|
|
timestamp time.Time
|
|
recordType byte
|
|
payloadLen int
|
|
}
|
|
|
|
type ScoutConnCollected struct {
|
|
mu sync.Mutex
|
|
data []ScoutConnResult
|
|
writeIndex int // index at which client first wrote post-handshake data; -1 if not set
|
|
}
|
|
|
|
func (s *ScoutConnCollected) Add(record byte, payloadLen int) {
|
|
s.mu.Lock()
|
|
s.data = append(s.data, ScoutConnResult{
|
|
timestamp: time.Now(),
|
|
recordType: record,
|
|
payloadLen: payloadLen,
|
|
})
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// MarkWrite records the current data length as the handshake boundary.
|
|
func (s *ScoutConnCollected) MarkWrite() {
|
|
s.mu.Lock()
|
|
if s.writeIndex < 0 {
|
|
s.writeIndex = len(s.data)
|
|
}
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
// Snapshot returns a copy of the collected data and the write index.
|
|
func (s *ScoutConnCollected) Snapshot() ([]ScoutConnResult, int) {
|
|
s.mu.Lock()
|
|
snapshot := make([]ScoutConnResult, len(s.data))
|
|
copy(snapshot, s.data)
|
|
writeIndex := s.writeIndex
|
|
s.mu.Unlock()
|
|
|
|
return snapshot, writeIndex
|
|
}
|
|
|
|
func NewScoutConnCollected() *ScoutConnCollected {
|
|
return &ScoutConnCollected{
|
|
data: make([]ScoutConnResult, 0, ScoutConnCollectedPreallocSize),
|
|
writeIndex: -1,
|
|
}
|
|
}
|