Skip to content

Commit

Permalink
Merge pull request #1651 from orbs-network/feature/httptimeout
Browse files Browse the repository at this point in the history
Mgmt url related small fixes
  • Loading branch information
noambergIL authored Dec 29, 2020
2 parents c689c6c + 31629ef commit 9aff567
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bootstrap/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func NewNode(nodeConfig config.NodeConfig, logger log.Logger) *Node {
nodeLogger.Error("Cannot start node without a ManagementFilePath", log.Error(err))
panic(err)
} else {
managementProvider = managementAdapter.NewFileProvider(nodeConfig, nodeLogger)
managementProvider = managementAdapter.NewFileProvider(nodeConfig)
}

blockPersistence, err := filesystem.NewBlockPersistence(nodeConfig, nodeLogger, metricRegistry)
Expand Down
4 changes: 2 additions & 2 deletions config/config_system_presets.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func defaultProductionConfig() mutableNodeConfig {
cfg := emptyConfig()

cfg.SetUint32(GOSSIP_LISTEN_PORT, 4400)
cfg.SetDuration(MANAGEMENT_POLLING_INTERVAL, 10*time.Second)
cfg.SetUint32(MANAGEMENT_MAX_FILE_SIZE, 50*(1<<20)) // 50 MB
cfg.SetDuration(MANAGEMENT_POLLING_INTERVAL, 30*time.Second)
cfg.SetUint32(MANAGEMENT_MAX_FILE_SIZE, 500*(1<<20)) // 50 MB
cfg.SetDuration(MANAGEMENT_CONSENSUS_GRACE_TIMEOUT, 10*time.Minute)
// for private consider changing this to 2^62 nanos (100 years) for PoS v2
cfg.SetDuration(COMMITTEE_GRACE_PERIOD, 12*time.Hour)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/ethereum/go-ethereum v1.9.6
github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/google/go-cmp v0.3.1
github.com/huin/goupnp v1.0.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.1 // indirect
Expand Down
25 changes: 14 additions & 11 deletions services/management/adapter/file_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
"github.com/orbs-network/orbs-network-go/services/management"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/orbs-network/orbs-spec/types/go/services"
"github.com/orbs-network/scribe/log"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"os"
"sort"
"strings"
"time"
)

type FileConfig interface {
Expand All @@ -32,12 +32,15 @@ type FileConfig interface {
}

type FileProvider struct {
logger log.Logger
config FileConfig
client *http.Client
}

func NewFileProvider(config FileConfig, logger log.Logger) *FileProvider {
return &FileProvider{config: config, logger: logger}
func NewFileProvider(config FileConfig) *FileProvider {
client := &http.Client{
Timeout: 45 * time.Second,
}
return &FileProvider{config: config, client:client}
}

func (mp *FileProvider) Get(ctx context.Context, referenceTime primitives.TimestampSeconds) (*management.VirtualChainManagementData, error) {
Expand All @@ -47,21 +50,18 @@ func (mp *FileProvider) Get(ctx context.Context, referenceTime primitives.Timest

if strings.HasPrefix(path, "http") {
if contents, err = mp.readUrl(path); err != nil {
mp.logger.Error("Provider url reading error", log.Error(err))
return nil, err
return nil, errors.Wrap(err, "Provider url reading error")
}
} else {
if contents, err = mp.readFile(path); err != nil {
mp.logger.Error("Provider path file reading error", log.Error(err))
return nil, err
return nil, errors.Wrap(err, "Provider path file reading error")
}
}

isHistoric := referenceTime != 0
managementData, parseErr := mp.parseData(contents, isHistoric)
if parseErr != nil {
mp.logger.Error("Provider file parsing error", log.Error(parseErr))
return nil, parseErr
return nil, errors.Wrap(err, "Provider file parsing error")
}

return managementData, nil
Expand All @@ -78,7 +78,10 @@ func (mp *FileProvider) generatePath(referenceTime primitives.TimestampSeconds)
}

func (mp *FileProvider) readUrl(path string) ([]byte, error) {
res, err := http.Get(path)
res, err := mp.client.Get(path)
if res != nil && res.Body != nil {
defer res.Body.Close()
}

if err != nil || res == nil {
return nil, errors.Wrapf(err, "Failed http get of url %s", path)
Expand Down
10 changes: 5 additions & 5 deletions services/management/adapter/file_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestManagementFileProvider_GeneratePath(t *testing.T) {
with.Logging(t, func(parent *with.LoggingHarness) {
const url = "x1"
cfg := newConfig(42, url)
fileProvider := NewFileProvider(cfg, parent.Logger)
fileProvider := NewFileProvider(cfg)
path := fileProvider.generatePath(0)
pathWithRef := fileProvider.generatePath(100)
require.Equal(t, url, path)
Expand All @@ -35,7 +35,7 @@ func TestManagementFileProvider_NoMatchVc(t *testing.T) {
with.Logging(t, func(parent *with.LoggingHarness) {
with.Context(func(ctx context.Context) {
cfg := newConfig(42, "")
fileProvider := NewFileProvider(cfg, parent.Logger)
fileProvider := NewFileProvider(cfg)
_, err := fileProvider.parseData([]byte(`{
"CurrentRefTime": 3,
"PageStartRefTime": 0,
Expand All @@ -55,7 +55,7 @@ func TestManagementFileProvider_BadCurrentInPage(t *testing.T) {
with.Logging(t, func(parent *with.LoggingHarness) {
with.Context(func(ctx context.Context) {
cfg := newConfig(42, "")
fileProvider := NewFileProvider(cfg, parent.Logger)
fileProvider := NewFileProvider(cfg)
_, err := fileProvider.parseData([]byte(`{
"CurrentRefTime": 3,
"PageStartRefTime": 0,
Expand Down Expand Up @@ -112,7 +112,7 @@ func TestManagementFileProvider_ReadFile(t *testing.T) {
with.Logging(t, func(parent *with.LoggingHarness) {
topologyFilePath := filepath.Join(config.GetCurrentSourceFileDirPath(), "_data", "good.json")
cfg := newConfig(42, topologyFilePath)
fileProvider := NewFileProvider(cfg, parent.Logger)
fileProvider := NewFileProvider(cfg)
expectFileProviderToReadCorrectly(t, ctx, fileProvider)
})
})
Expand All @@ -123,7 +123,7 @@ func TestManagementFileProvider_ReadUrl(t *testing.T) {
with.Logging(t, func(parent *with.LoggingHarness) {
const url = "https://raw.githubusercontent.com/orbs-network/management-service/master/static-tests/management-vc-file.json"
cfg := newConfig(42, url)
fileProvider := NewFileProvider(cfg, parent.Logger)
fileProvider := NewFileProvider(cfg)
expectFileProviderToReadCorrectly(t, ctx, fileProvider)
})
})
Expand Down

0 comments on commit 9aff567

Please sign in to comment.