This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 112
Feat: Track Session Peer Latency More Accurately #149
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
98f01e7
feat(sessions): track real latency per peer
hannahhoward 8e59a71
feat(sessions): pass optimization rating
hannahhoward 1bf9ed3
feat(sessionpeermanager): track cancels
hannahhoward 0d8b75d
feat(sessions): record duplicate responses
hannahhoward 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 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 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 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 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,18 @@ | ||
package sessiondata | ||
|
||
import ( | ||
cid "github.com/ipfs/go-cid" | ||
peer "github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
// OptimizedPeer describes a peer and its level of optimization from 0 to 1. | ||
type OptimizedPeer struct { | ||
Peer peer.ID | ||
OptimizationRating float64 | ||
} | ||
|
||
// PartialRequest is represents one slice of an over request split among peers | ||
type PartialRequest struct { | ||
Peers []peer.ID | ||
Keys []cid.Cid | ||
} |
This file contains 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 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 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,75 @@ | ||
package sessionpeermanager | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
type requestData struct { | ||
startedAt time.Time | ||
wasCancelled bool | ||
timeoutFunc *time.Timer | ||
} | ||
|
||
type latencyTracker struct { | ||
requests map[cid.Cid]*requestData | ||
} | ||
|
||
func newLatencyTracker() *latencyTracker { | ||
return &latencyTracker{requests: make(map[cid.Cid]*requestData)} | ||
} | ||
|
||
type afterTimeoutFunc func(cid.Cid) | ||
|
||
func (lt *latencyTracker) SetupRequests(keys []cid.Cid, timeoutDuration time.Duration, afterTimeout afterTimeoutFunc) { | ||
startedAt := time.Now() | ||
for _, k := range keys { | ||
if _, ok := lt.requests[k]; !ok { | ||
lt.requests[k] = &requestData{ | ||
startedAt, | ||
false, | ||
time.AfterFunc(timeoutDuration, makeAfterTimeout(afterTimeout, k)), | ||
} | ||
} | ||
} | ||
} | ||
|
||
func makeAfterTimeout(afterTimeout afterTimeoutFunc, k cid.Cid) func() { | ||
return func() { afterTimeout(k) } | ||
} | ||
|
||
func (lt *latencyTracker) CheckDuration(key cid.Cid) (time.Duration, bool) { | ||
request, ok := lt.requests[key] | ||
var latency time.Duration | ||
if ok { | ||
latency = time.Now().Sub(request.startedAt) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
} | ||
return latency, ok | ||
} | ||
|
||
func (lt *latencyTracker) RemoveRequest(key cid.Cid) { | ||
request, ok := lt.requests[key] | ||
if ok { | ||
request.timeoutFunc.Stop() | ||
delete(lt.requests, key) | ||
} | ||
} | ||
|
||
func (lt *latencyTracker) RecordCancel(key cid.Cid) { | ||
request, ok := lt.requests[key] | ||
if ok { | ||
request.wasCancelled = true | ||
} | ||
} | ||
|
||
func (lt *latencyTracker) WasCancelled(key cid.Cid) bool { | ||
request, ok := lt.requests[key] | ||
return ok && request.wasCancelled | ||
} | ||
|
||
func (lt *latencyTracker) Shutdown() { | ||
for _, request := range lt.requests { | ||
request.timeoutFunc.Stop() | ||
} | ||
} |
This file contains 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,41 @@ | ||
package sessionpeermanager | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/ipfs/go-cid" | ||
) | ||
|
||
const ( | ||
newLatencyWeight = 0.5 | ||
) | ||
|
||
type peerData struct { | ||
hasLatency bool | ||
latency time.Duration | ||
lt *latencyTracker | ||
} | ||
|
||
func newPeerData() *peerData { | ||
return &peerData{ | ||
hasLatency: false, | ||
lt: newLatencyTracker(), | ||
latency: 0, | ||
} | ||
} | ||
|
||
func (pd *peerData) AdjustLatency(k cid.Cid, hasFallbackLatency bool, fallbackLatency time.Duration) { | ||
latency, hasLatency := pd.lt.CheckDuration(k) | ||
pd.lt.RemoveRequest(k) | ||
if !hasLatency { | ||
latency, hasLatency = fallbackLatency, hasFallbackLatency | ||
} | ||
if hasLatency { | ||
if pd.hasLatency { | ||
pd.latency = time.Duration(float64(pd.latency)*(1.0-newLatencyWeight) + float64(latency)*newLatencyWeight) | ||
} else { | ||
pd.latency = latency | ||
pd.hasLatency = true | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
Can we comment on what this rating means?