Add limit - #185
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces a limit on the outbound permit release behavior in the PortalProtocol's content lookup and reorganizes some definitions for clarity. Key changes include refactoring the handleFindContent functionality into two functions, updating the dependency version in go.mod, and moving constants and type definitions for better grouping.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| portalwire/portal_protocol.go | Refactored and reorganized functions and constants; introduced a new helper function for content lookup limits |
| go.mod | Upgraded the utp-go dependency and updated the replacement directive |
| var n int | ||
| n, err = conn.Write(writeCtx, content) | ||
| conn.Close() | ||
| permit.Release() |
There was a problem hiding this comment.
The permit.Release() call appears both in a defer statement and explicitly within the same goroutine, which may lead to a double release. Consider removing the explicit call to rely solely on the deferred release.
| permit.Release() |
There was a problem hiding this comment.
Pull Request Overview
This PR refactors UTP connection permits by introducing a Permit enum, replacing the old Release() interface, and updating related tests and rate-limit logic.
- Replace
ReleasePermitinterface withPermitenum and associated controller methods. - Update tests to use new
getPermit/releasePermitAPI. - Refactor portal protocol and API to use the new permit-based rate-limiting and bump
DefaultUtpConnSize.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| portalwire/utp_transport.go | Introduce Permit enum, refactor controller methods. |
| portalwire/utp_transport_test.go | Update tests to call getPermit/releasePermit. |
| portalwire/portal_protocol.go | Integrate permit API, update rate-limit logic. |
| portalwire/portal_protocol_test.go | Adjust tests for new Permit, add rate-limit test. |
| portalwire/api.go | Update API calls to pass PermitNotLimit. |
| go.mod | Bump utp-go dependency version. |
Comments suppressed due to low confidence (5)
portalwire/portal_protocol_test.go:1040
- The test name 'TestAcceptCode_Ratelmit' has a typo. Rename to 'TestAcceptCode_RateLimit' for clarity.
func TestAcceptCode_Ratelmit(t *testing.T) {
portalwire/utp_transport.go:35
- [nitpick] The field name
getInboundLimitsuggests it returns a limit, but it returns aPermit. Consider renaming togetInboundPermitto match its return type.
getInboundLimit func() Permit
portalwire/utp_transport.go:112
- The comment for
GetOutboundPermitincorrectly starts withGetInboundPermit. Update it to reflectGetOutboundPermitand its behavior.
// GetInboundPermit tries to acquire a permit for outbound UTP connections.
portalwire/portal_protocol.go:107
- The 'overheadvar' text merges two declarations and will not compile. Split the
enrOverheadcomment and theexpirationVersionMinutesdeclaration onto separate lines.
enrOverhead = 4 // per added ENR, 4 bytes offset overheadvar expirationVersionMinutes = 5 * time.Minute // cache versionsCache expiration time in minutes
portalwire/portal_protocol_test.go:13
- The test uses
fmt.Sprintfandhexutil.Encodebut neitherfmtnorgithub.com/ethereum/go-ethereum/common/hexutilare imported. Add the missing imports to make the test compile.
"sync/atomic"
| _, err := p.offer(offerRequestWithNode.Node, offerRequestWithNode.Request, offerRequestWithNode.permit) | ||
| permit := p.Utp.getOutboundLimit() | ||
| if permit == PermitReject { | ||
| p.Log.Debug("utp rate limited, rejecting offer", "offerRequestWithNode", offerRequestWithNode) |
There was a problem hiding this comment.
When permit == PermitReject, the code logs a rate-limit but still calls p.offer, sending work even when it should be skipped. Add a continue or conditional to skip p.offer on PermitReject.
| p.Log.Debug("utp rate limited, rejecting offer", "offerRequestWithNode", offerRequestWithNode) | |
| p.Log.Debug("utp rate limited, rejecting offer", "offerRequestWithNode", offerRequestWithNode) | |
| continue |
| socket *utp.UtpSocket | ||
| socketConfig *utp.ConnectionConfig | ||
| ListenAddr string | ||
| utpControllerRef *utpController |
There was a problem hiding this comment.
[nitpick] The utpControllerRef field is only used to set function pointers and never directly referenced. Consider removing it to simplify the struct.
| utpControllerRef *utpController |
There was a problem hiding this comment.
Pull Request Overview
This PR updates the UTP permit logic to use a simple integer-based Permit type (with constants PermitNotLimit, PermitReject, PermitInbound, and PermitOutbound) instead of an interface-based approach, and adjusts the associated tests, API calls, and UTP transport functionality accordingly.
- Refactored permit acquisition and release functions in utp_transport.go.
- Updated test functions and API calls to use new Permit constants.
- Modified protocol handling functions in portal_protocol.go to incorporate the new permit patterns.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| portalwire/utp_transport_test.go | Updated test helpers to use the new permit API and release functions. |
| portalwire/utp_transport.go | Refactored UTP controller to use integer-based permits with new limit and release functions. |
| portalwire/portal_protocol_test.go | Modified tests to replace old permit types with the new Permit constants. |
| portalwire/portal_protocol.go | Updated offer, processOffer, and related rate limit logic to work with new permit API. |
| portalwire/api.go | Revised API calls to use new permit constants instead of the deprecated NoPermit type. |
| go.mod | Updated UTP module version and adjusted the replacement for the go-ethereum dependency. |
| } | ||
|
|
||
| func (p *PortalProtocol) offer(node *enode.Node, offerRequest *OfferRequest, permit Permit) ([]byte, error) { | ||
| permitReleaseInProcessOffer := false |
There was a problem hiding this comment.
The permit release mechanism using the boolean flag 'permitReleaseInProcessOffer' is somewhat convoluted. Consider refactoring this logic (possibly by encapsulating permit management in a separate helper) to simplify the control flow and reduce the risk of accidental double-releases.
| permitReleaseInGo := false | ||
| defer func() { | ||
| if notStartedUtp { | ||
| permit.Release() | ||
| if !permitReleaseInGo { | ||
| p.Utp.releasePermit(permit) | ||
| } | ||
| }() |
There was a problem hiding this comment.
The use of the 'permitReleaseInGo' flag in processOffer, along with its paired deferred release, makes the permit management logic harder to follow. A refactoring to streamline the permit release flow would improve overall code clarity and maintainability.
There was a problem hiding this comment.
Pull Request Overview
This PR replaces the previous Permit interface with a Permit enum to manage UTP connection rate limits, refactors UtpTransportService to use function fields for acquiring and releasing permits, and updates the portal protocol to integrate the new permit logic. Tests and API calls are updated to pass the appropriate Permit values.
- Refactor
utpControllerto return aPermitenum instead of an interface, and wire it intoUtpTransportService. - Update portal protocol’s offer/find-content logic to acquire/release permits.
- Adjust tests and API usage to use the new
Permitvalues.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| portalwire/utp_transport.go | Refactored utpController to use Permit enum and added func fields for permit operations. |
| portalwire/utp_transport_test.go | Updated tests to match new Get*Permit signatures. |
| portalwire/portal_protocol.go | Integrated permit acquisition/release in offer and find-content flows; moved and updated related constants/types. |
| portalwire/portal_protocol_test.go | Added rate-limit tests; updated imports for atomic usage. |
| portalwire/api.go | Changed API calls to pass PermitNotLimit instead of &NoPermit{}. |
| go.mod | Bumped utp-go dependency and replace directive. |
Comments suppressed due to low confidence (5)
portalwire/utp_transport.go:35
- [nitpick] The field name
getInboundLimitis misleading since it acquires a permit. Consider renaming toacquireInboundPermitorgetInboundPermitfor clarity.
getInboundLimit func() Permit
portalwire/utp_transport.go:36
- [nitpick] Similarly,
getOutboundLimitreturns a permit, so renaming toacquireOutboundPermitorgetOutboundPermitwould better reflect its behavior.
getOutboundLimit func() Permit
portalwire/portal_protocol.go:103
- The
Tagconstant is defined before theClientTagtype is declared, causing a compile error. Move thetype ClientTag stringdeclaration above this const.
const Tag ClientTag = "shisui"
portalwire/portal_protocol.go:107
- The comment for
enrOverheadunintentionally includes the definition ofexpirationVersionMinutes. Split this into two separate lines to avoid a syntax error.
enrOverhead = 4 // per added ENR, 4 bytes offset overheadvar expirationVersionMinutes = 5 * time.Minute // cache versionsCache expiration time in minutes
portalwire/portal_protocol_test.go:1028
- The test uses
fmt.Printlnandhexutil.Encodebutfmtandhexutilare not imported. Addimport "fmt"and the appropriatehexutilpackage.
fmt.Println(hexutil.Encode(content))
Signed-off-by: Chen Kai <281165273grape@gmail.com>
No description provided.