Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdn): missing default config panic #5744

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions engine/cdn/storage/storageunit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ func Init(ctx context.Context, m *gorpmapper.Mapper, store cache.Store, db *gorp
}
}

var result = RunningStorageUnits{
m: m,
db: db,
cache: store,
config: config,
if config.SyncNbElements <= 0 || config.SyncNbElements > 1000 {
config.SyncNbElements = 100
}

if config.SyncSeconds <= 0 {
config.SyncSeconds = 30
}

if config.PurgeSeconds <= 0 {
config.PurgeSeconds = 30
}

if config.PurgeNbElements <= 0 {
config.PurgeNbElements = 1000
}

if len(config.HashLocatorSalt) < 8 {
Expand Down Expand Up @@ -75,20 +84,11 @@ func Init(ctx context.Context, m *gorpmapper.Mapper, store cache.Store, db *gorp
return nil, sdk.WithStack(fmt.Errorf("invalid CDN configuration. Missing storage unit"))
}

if config.SyncNbElements <= 0 || config.SyncNbElements > 1000 {
config.SyncNbElements = 100
}

if config.SyncSeconds <= 0 {
config.SyncSeconds = 30
}

if config.PurgeSeconds <= 0 {
config.PurgeSeconds = 30
}

if config.PurgeNbElements <= 0 {
config.PurgeNbElements = 1000
var result = RunningStorageUnits{
m: m,
db: db,
cache: store,
config: config,
}

for name, bu := range config.Buffers {
Expand Down Expand Up @@ -144,12 +144,10 @@ func Init(ctx context.Context, m *gorpmapper.Mapper, store cache.Store, db *gorp
return nil, sdk.WithStack(errors.New("unsupported buffer units"))
}

result.Buffers = append(result.Buffers, bufferUnit)
tx, err := db.Begin()
if err != nil {
return nil, err
return nil, sdk.WithStack(err)
}
defer tx.Rollback() // nolint

u, err := LoadUnitByName(ctx, m, tx, name)
if sdk.ErrorIs(err, sdk.ErrNotFound) {
Expand All @@ -162,17 +160,20 @@ func Init(ctx context.Context, m *gorpmapper.Mapper, store cache.Store, db *gorp
Name: name,
Config: srvConfig,
}
if err := InsertUnit(ctx, m, tx, u); err != nil {
return nil, err
}
} else if err != nil {
err = InsertUnit(ctx, m, tx, u)
}
if err != nil {
_ = tx.Rollback() // nolint
return nil, err
}
bufferUnit.Set(*u)

if err := tx.Commit(); err != nil {
return nil, err
_ = tx.Rollback() // nolint
return nil, sdk.WithStack(err)
}

result.Buffers = append(result.Buffers, bufferUnit)
}

// Then initialize the storages unit
Expand Down Expand Up @@ -264,7 +265,6 @@ func Init(ctx context.Context, m *gorpmapper.Mapper, store cache.Store, db *gorp
var srvConfig sdk.ServiceConfig
b, _ := json.Marshal(cfg)
_ = json.Unmarshal(b, &srvConfig) // nolint

u = &sdk.CDNUnit{
ID: sdk.UUID(),
Created: time.Now(),
Expand All @@ -279,11 +279,12 @@ func Init(ctx context.Context, m *gorpmapper.Mapper, store cache.Store, db *gorp
}
storageUnit.Set(*u)

result.Storages = append(result.Storages, storageUnit)
if err := tx.Commit(); err != nil {
_ = tx.Rollback() // nolint
return nil, sdk.WithStack(err)
}

result.Storages = append(result.Storages, storageUnit)
}

return &result, nil
Expand Down