From cfadbae207e2cfc8a285100c321ea4c68ad9babe Mon Sep 17 00:00:00 2001 From: Nikita Kryuchkov Date: Mon, 25 Mar 2024 16:48:53 +0300 Subject: [PATCH] Add an option for reduced memory usage --- http/parameters.go | 29 +++++++++++++++++++---------- http/service.go | 2 ++ http/validators.go | 16 +++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/http/parameters.go b/http/parameters.go index de005d0d..222518e0 100644 --- a/http/parameters.go +++ b/http/parameters.go @@ -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. @@ -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{ diff --git a/http/service.go b/http/service.go index 2ec453af..7895230b 100644 --- a/http/service.go +++ b/http/service.go @@ -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. @@ -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. diff --git a/http/validators.go b/http/validators.go index 83579d82..10d53f24 100644 --- a/http/validators.go +++ b/http/validators.go @@ -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) {