Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Oct 1, 2024
1 parent 1763fd1 commit 2e35868
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
48 changes: 27 additions & 21 deletions cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,27 +408,7 @@ func createVersionsRegistry(
return nil, err
}

httpClient := &http.Client{}
httpClient.Timeout = time.Duration(cfg.GeneralSettings.RequestTimeoutSec) * time.Second
observersList := make([]string, 0, len(cfg.Observers))
for _, node := range cfg.Observers {
observersList = append(observersList, node.Address)
}
argsNumShardsProcessor := process.ArgNumShardsProcessor{
HttpClient: httpClient,
Observers: observersList,
TimeBetweenNodesRequestsInSec: cfg.GeneralSettings.TimeBetweenNodesRequestsInSec,
NumShardsTimeoutInSec: cfg.GeneralSettings.NumShardsTimeoutInSec,
RequestTimeoutInSec: cfg.GeneralSettings.RequestTimeoutSec,
}
numShardsProcessor, err := process.NewNumShardsProcessor(argsNumShardsProcessor)
if err != nil {
return nil, err
}

ctx, cancel := context.WithCancel(context.Background())
numShards, err := numShardsProcessor.GetNetworkNumShards(ctx)
cancel()
numShards, err := getNumOfShards(cfg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -636,6 +616,32 @@ func waitForServerShutdown(httpServer *http.Server, closableComponents *data.Clo
_ = httpServer.Close()
}

// getNumOfShards will delay the start of proxy until it successfully gets the number of shards
func getNumOfShards(cfg *config.Config) (uint32, error) {
httpClient := &http.Client{}
httpClient.Timeout = time.Duration(cfg.GeneralSettings.RequestTimeoutSec) * time.Second
observersList := make([]string, 0, len(cfg.Observers))
for _, node := range cfg.Observers {
observersList = append(observersList, node.Address)
}
argsNumShardsProcessor := process.ArgNumShardsProcessor{
HttpClient: httpClient,
Observers: observersList,
TimeBetweenNodesRequestsInSec: cfg.GeneralSettings.TimeBetweenNodesRequestsInSec,
NumShardsTimeoutInSec: cfg.GeneralSettings.NumShardsTimeoutInSec,
RequestTimeoutInSec: cfg.GeneralSettings.RequestTimeoutSec,
}
numShardsProcessor, err := process.NewNumShardsProcessor(argsNumShardsProcessor)
if err != nil {
return 0, err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

return numShardsProcessor.GetNetworkNumShards(ctx)
}

func removeLogColors() {
err := logger.RemoveLogObserver(os.Stdout)
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions process/numShardsProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import (

var errTimeIsOut = errors.New("time is out")

const (
networkConfigPath = "/network/config"
)

type networkConfigResponseData struct {
Config struct {
NumShards uint32 `json:"erd_num_shards_without_meta"`
Expand Down Expand Up @@ -112,7 +108,7 @@ func (processor *numShardsProcessor) tryGetnumShardsFromObserver(observerAddress
ctx, cancel := context.WithTimeout(context.Background(), processor.requestTimeout)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, observerAddress+networkConfigPath, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, observerAddress+NetworkConfigPath, nil)
if err != nil {
return 0, http.StatusNotFound
}
Expand Down

0 comments on commit 2e35868

Please sign in to comment.