Skip to content

Commit

Permalink
network/zone: Optimize db transactions
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Hipp <thomashipp@gmail.com>
  • Loading branch information
monstermunchkin committed Jan 24, 2024
1 parent 26b7fa4 commit ceade1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
32 changes: 15 additions & 17 deletions internal/server/network/zone/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,32 @@ func (d *zone) GetRecords() ([]api.NetworkZoneRecord, error) {
s := d.state

var names []string
records := []api.NetworkZoneRecord{}
var record *api.NetworkZoneRecord

err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error

// Get the record names.
names, err = tx.GetNetworkZoneRecordNames(ctx, d.id)
if err != nil {
return err
}

return err
})
if err != nil {
return nil, err
}

// Load all the records.
records := []api.NetworkZoneRecord{}
var record *api.NetworkZoneRecord

for _, name := range names {
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
// Load all the records.
for _, name := range names {
_, record, err = tx.GetNetworkZoneRecord(ctx, d.id, name)
if err != nil {
return err
}

return err
})
if err != nil {
return nil, err
records = append(records, *record)
}

records = append(records, *record)
return nil
})
if err != nil {
return nil, err
}

return records, nil
Expand Down
10 changes: 3 additions & 7 deletions internal/server/network/zone/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,10 @@ func (d *zone) usedBy(firstOnly bool) ([]string, error) {

// Find networks using the zone.
networkNames, err = tx.GetCreatedNetworkNamesByProject(ctx, d.projectName)
if err != nil && !response.IsNotFoundError(err) {
return fmt.Errorf("Failed loading networks for project %q: %w", d.projectName, err)
}

return err
})
if err != nil && !response.IsNotFoundError(err) {
return nil, fmt.Errorf("Failed loading networks for project %q: %w", d.projectName, err)
}

err = d.state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
for _, networkName := range networkNames {
_, network, _, err := tx.GetNetworkInAnyState(ctx, d.projectName, networkName)
if err != nil {
Expand Down

0 comments on commit ceade1e

Please sign in to comment.