Skip to content

Commit

Permalink
Add an option for reduced memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Mar 25, 2024
1 parent 35616e0 commit cfadbae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
29 changes: 19 additions & 10 deletions http/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ import (
)

type parameters struct {
logLevel zerolog.Level
monitor metrics.Service
address string
timeout time.Duration
indexChunkSize int
pubKeyChunkSize int
extraHeaders map[string]string
enforceJSON bool
allowDelayedStart bool
hooks *Hooks
logLevel zerolog.Level
monitor metrics.Service
address string
timeout time.Duration
indexChunkSize int
pubKeyChunkSize int
extraHeaders map[string]string
enforceJSON bool
allowDelayedStart bool
hooks *Hooks
reducedMemoryUsage bool
}

// Parameter is the interface for service parameters.
Expand Down Expand Up @@ -115,6 +116,14 @@ func WithHooks(hooks *Hooks) Parameter {
})
}

// WithReducedMemoryUsage reduces memory usage by disabling certain actions that may take significant amount of memory.
// Enabling this may result in longer response times.
func WithReducedMemoryUsage(reduceMemUsage bool) Parameter {
return parameterFunc(func(p *parameters) {
p.reducedMemoryUsage = reduceMemUsage
})
}

// parseAndCheckParameters parses and checks parameters to ensure that mandatory parameters are present and correct.
func parseAndCheckParameters(params ...Parameter) (*parameters, error) {
parameters := parameters{
Expand Down
2 changes: 2 additions & 0 deletions http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Service struct {
connectionSynced bool
enforceJSON bool
connectedToDVTMiddleware bool
reducedMemoryUsage bool
}

// New creates a new Ethereum 2 client service, connecting with a standard HTTP.
Expand Down Expand Up @@ -124,6 +125,7 @@ func New(ctx context.Context, params ...Parameter) (client.Service, error) {
enforceJSON: parameters.enforceJSON,
pingSem: semaphore.NewWeighted(1),
hooks: parameters.hooks,
reducedMemoryUsage: parameters.reducedMemoryUsage,
}

// Ping the client to see if it is ready to serve requests.
Expand Down
16 changes: 9 additions & 7 deletions http/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,16 @@ func (s *Service) Validators(ctx context.Context,
return nil, errors.Join(errors.New("cannot specify both indices and public keys"), client.ErrInvalidOptions)
}

if len(opts.Indices) == 0 && len(opts.PubKeys) == 0 {
// Request is for all validators; fetch from state.
return s.validatorsFromState(ctx, opts)
}
if !s.reducedMemoryUsage {
if len(opts.Indices) == 0 && len(opts.PubKeys) == 0 {
// Request is for all validators; fetch from state.
return s.validatorsFromState(ctx, opts)
}

if len(opts.Indices) > s.indexChunkSize(ctx)*16 || len(opts.PubKeys) > s.pubKeyChunkSize(ctx)*16 {
// Request is for multiple pages of validators; fetch from state.
return s.validatorsFromState(ctx, opts)
if len(opts.Indices) > s.indexChunkSize(ctx)*16 || len(opts.PubKeys) > s.pubKeyChunkSize(ctx)*16 {
// Request is for multiple pages of validators; fetch from state.
return s.validatorsFromState(ctx, opts)
}
}

if len(opts.Indices) > s.indexChunkSize(ctx) || len(opts.PubKeys) > s.pubKeyChunkSize(ctx) {
Expand Down

0 comments on commit cfadbae

Please sign in to comment.