-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: PRT provider load rate (#1720)
* load rate report in trailer * fix trailer name * fix lint * fix load manager logic * fix lint * fix spelling * fix logic * fixed flag & header names * fix load provider manager and creation logic * fix logs for relay load rate * fix rpcprovider server relay load handling * fix tests * fix typo * fix init lava script * fix provider load manager * fix provider server and load manager * fix lint - fix protocol test * fix provider load manager applyProviderLoadMetadataToContextTrailer * change cmdRPCProvider load rate flag to uint64 * try fix * fix cmd flag reading * adjusting uint64 * fix redundent nil check in provider load manager * fix providerLoadManager per chain creation * rename and fix instance passing unnecessarily * fixed chainlib common formatting * fix provider load manager comments * fix e2e tests * fix pr - unite add relay load and set trailer * fix common.go provider load header * fix edge case of getProviderLoad * fix command flag description * fix command flag description * add metric for load rate * fix division to be float and not uint * roll back init lava only with node two consumers * fix load metric * merge main * Update protocol/chainlib/common.go Co-authored-by: Elad Gildnur <[email protected]> * fix load calculation * tidy code * changing rate limit to 1k * fix bug * fix pr * v4 * fix pr * fix --------- Co-authored-by: leon mandel <[email protected]> Co-authored-by: Ran Mishael <[email protected]> Co-authored-by: Leon Magma <[email protected]> Co-authored-by: Elad Gildnur <[email protected]> Co-authored-by: Omer <[email protected]>
- Loading branch information
1 parent
479a906
commit d677c37
Showing
10 changed files
with
144 additions
and
45 deletions.
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
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,55 @@ | ||
package rpcprovider | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"sync/atomic" | ||
|
||
"github.com/lavanet/lava/v4/protocol/chainlib" | ||
grpc "google.golang.org/grpc" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type ProviderLoadManager struct { | ||
rateLimitThreshold uint64 | ||
activeRequestsPerSecond atomic.Uint64 | ||
} | ||
|
||
func NewProviderLoadManager(rateLimitThreshold uint64) *ProviderLoadManager { | ||
if rateLimitThreshold == 0 { | ||
return nil | ||
} | ||
loadManager := &ProviderLoadManager{rateLimitThreshold: rateLimitThreshold} | ||
return loadManager | ||
} | ||
|
||
func (loadManager *ProviderLoadManager) subtractRelayCall() { | ||
if loadManager == nil { | ||
return | ||
} | ||
loadManager.activeRequestsPerSecond.Add(^uint64(0)) | ||
} | ||
|
||
func (loadManager *ProviderLoadManager) getProviderLoad(activeRequests uint64) float64 { | ||
rateLimitThreshold := loadManager.rateLimitThreshold | ||
if rateLimitThreshold == 0 { | ||
return 0 | ||
} | ||
return float64(activeRequests) / float64(rateLimitThreshold) | ||
} | ||
|
||
// Add relay count, calculate current load | ||
func (loadManager *ProviderLoadManager) addAndSetRelayLoadToContextTrailer(ctx context.Context) float64 { | ||
if loadManager == nil { | ||
return 0 | ||
} | ||
activeRequestsPerSecond := loadManager.activeRequestsPerSecond.Add(1) | ||
provideRelayLoad := loadManager.getProviderLoad(activeRequestsPerSecond) | ||
if provideRelayLoad == 0 { | ||
return provideRelayLoad | ||
} | ||
formattedProviderLoad := strconv.FormatFloat(provideRelayLoad, 'f', -1, 64) | ||
trailerMd := metadata.Pairs(chainlib.RpcProviderLoadRateHeader, formattedProviderLoad) | ||
grpc.SetTrailer(ctx, trailerMd) | ||
return provideRelayLoad | ||
} |
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
Oops, something went wrong.