-
Notifications
You must be signed in to change notification settings - Fork 74
add timeout handler #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package probing | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"time" | ||
) | ||
|
||
type seqMap struct { | ||
trackerUUIDs []uuid.UUID | ||
head *elem | ||
tail *elem | ||
seqMap map[uuid.UUID]map[int]*elem | ||
} | ||
type elem struct { | ||
uuid uuid.UUID | ||
seq int | ||
time time.Time | ||
prev *elem | ||
next *elem | ||
} | ||
|
||
func newSeqMap(u uuid.UUID) seqMap { | ||
s := seqMap{ | ||
head: &elem{}, | ||
tail: &elem{}, | ||
seqMap: map[uuid.UUID]map[int]*elem{}, | ||
} | ||
s.trackerUUIDs = append(s.trackerUUIDs, u) | ||
s.seqMap[u] = make(map[int]*elem) | ||
s.head.next = s.tail | ||
s.tail.prev = s.head | ||
return s | ||
} | ||
|
||
func (s seqMap) newSeqMap(u uuid.UUID) { | ||
s.trackerUUIDs = append(s.trackerUUIDs, u) | ||
s.seqMap[u] = make(map[int]*elem) | ||
s.head.next = s.tail | ||
s.tail.prev = s.head | ||
} | ||
|
||
func (s seqMap) putElem(uuid uuid.UUID, seq int, now time.Time) { | ||
e := &elem{ | ||
uuid: uuid, | ||
seq: seq, | ||
time: now, | ||
prev: s.tail.prev, | ||
next: s.tail, | ||
} | ||
s.tail.prev.next = e | ||
s.tail.prev = e | ||
s.seqMap[uuid][seq] = e | ||
} | ||
func (s seqMap) getElem(uuid uuid.UUID, seq int) (*elem, bool) { | ||
e, ok := s.seqMap[uuid][seq] | ||
return e, ok | ||
} | ||
func (s seqMap) removeElem(e *elem) { | ||
e.prev.next = e.next | ||
e.next.prev = e.prev | ||
if m, ok := s.seqMap[e.uuid]; ok { | ||
delete(m, e.seq) | ||
} | ||
} | ||
func (s seqMap) peekFirst() *elem { | ||
if s.head.next == s.tail { | ||
return nil | ||
} | ||
return s.head.next | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package probing | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestSeqMap(t *testing.T) { | ||
u := uuid.New() | ||
s := newSeqMap(u) | ||
t.Run("newSeqMap", func(t *testing.T) { | ||
u2 := uuid.New() | ||
s.newSeqMap(u2) | ||
|
||
if len(s.trackerUUIDs) != 1 { | ||
t.Errorf("Expected length of trackerUUIDs to be 2, got %d", len(s.trackerUUIDs)) | ||
} | ||
|
||
if _, ok := s.seqMap[u2]; !ok { | ||
t.Errorf("Expected seqMap to contain UUID %s", u2.String()) | ||
} | ||
}) | ||
|
||
t.Run("putElem", func(t *testing.T) { | ||
seq := 1 | ||
s.putElem(u, seq, time.Now()) | ||
|
||
// 检查 seqMap 中是否包含正确的元素 | ||
if _, ok := s.seqMap[u][seq]; !ok { | ||
t.Errorf("Expected seqMap[%s][%d] to exist", u.String(), seq) | ||
} | ||
|
||
if s.peekFirst().seq != seq { | ||
t.Errorf("Expected tail.prev.seq to be %d, got %d", seq, s.tail.prev.seq) | ||
} | ||
}) | ||
|
||
t.Run("getElem", func(t *testing.T) { | ||
seq := 1 | ||
elem, ok := s.getElem(u, seq) | ||
|
||
if !ok { | ||
t.Errorf("Expected getElem to return true for existing element") | ||
} | ||
|
||
if elem.seq != seq { | ||
t.Errorf("Expected element's seq to be %d, got %d", seq, elem.seq) | ||
} | ||
}) | ||
|
||
t.Run("removeElem", func(t *testing.T) { | ||
seq := 1 | ||
elem, ok := s.getElem(u, seq) | ||
if !ok { | ||
t.Fatalf("Expected getElem to return true for existing element") | ||
} | ||
|
||
s.removeElem(elem) | ||
|
||
if _, ok := s.seqMap[u][seq]; ok { | ||
t.Errorf("Expected seqMap[%s][%d] to be removed", u.String(), seq) | ||
} | ||
}) | ||
|
||
// test peekFirst | ||
t.Run("peekFirst", func(t *testing.T) { | ||
seq := 2 | ||
s.putElem(u, seq, time.Now()) | ||
|
||
elem := s.peekFirst() | ||
|
||
// 检查 peekFirst 是否返回链表的第一个元素 | ||
if elem.seq != seq { | ||
t.Errorf("Expected peekFirst to return element with seq %d, got %d", seq, elem.seq) | ||
} | ||
}) | ||
} | ||
|
||
func TestSeqMap2(t *testing.T) { | ||
u := uuid.New() | ||
s := newSeqMap(u) | ||
for i := 0; i < 100; i++ { | ||
s.putElem(u, i, time.Now()) | ||
} | ||
for i := 0; i < 100; i++ { | ||
e, ok := s.getElem(u, i) | ||
AssertTrue(t, ok && e.seq == i) | ||
} | ||
for i := 0; i < 20; i++ { | ||
first := s.peekFirst() | ||
AssertTrue(t, first.seq == i) | ||
s.removeElem(first) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the performance implications of this. IIRC this will spawn a new sleeping goroutine for every packet. If there are a lot of outstanding packets it could bloat memory quite a lot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I create a new pr: #49 and fix some problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about it. but I rewrite it use time.timer