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

feat: remove mutex from terraform provider #1124

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions mgc/core/ref_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"strings"
"sync"

"github.com/getkin/kin-openapi/openapi3"
"github.com/go-openapi/jsonpointer"
Expand Down Expand Up @@ -217,6 +218,7 @@ func (r *MultiRefPathResolver) Add(url string, docResolver RefPathResolver) erro
type DocumentRefPathResolver struct {
cache map[RefPath]any // JSON Pointer string => resolved value
getRoot func() (any, error)
mutexl *sync.Mutex
}

var _ RefPathResolver = (*DocumentRefPathResolver)(nil)
Expand All @@ -226,6 +228,7 @@ func NewDocumentRefPathResolver(getRoot utils.LoadWithError[any]) *DocumentRefPa
return &DocumentRefPathResolver{
cache: map[RefPath]any{},
getRoot: getRoot,
mutexl: &sync.Mutex{},
}
}

Expand All @@ -251,9 +254,12 @@ func (r *DocumentRefPathResolver) ResolvePath(path RefPath) (result any, err err
}

var ok bool
r.mutexl.Lock()
if result, ok = r.cache[path]; ok {
r.mutexl.Unlock()
return
}
r.mutexl.Unlock()

url, _ := path.SplitUrl()
if url != "" {
Expand All @@ -274,11 +280,13 @@ func (r *DocumentRefPathResolver) ResolvePath(path RefPath) (result any, err err
}

if err == nil {
r.mutexl.Lock()
r.cache[path] = result
} else {
err = &RefPathResolveError{path, err}
}

r.mutexl.Unlock()
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"sync"

"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -15,10 +14,6 @@ import (
"magalu.cloud/sdk"
)

var (
bucketResourceMutex = &sync.Mutex{}
)

type ObjectStorageBucket struct {
Bucket types.String `tfsdk:"bucket"`
BucketIsPrefix types.Bool `tfsdk:"bucket_is_prefix"`
Expand Down Expand Up @@ -173,8 +168,6 @@ func (r *objectStorageBuckets) Schema(ctx context.Context, req resource.SchemaRe
}

func (r *objectStorageBuckets) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
bucketResourceMutex.Lock()
defer bucketResourceMutex.Unlock()
var model ObjectStorageBucket
diags := req.Plan.Get(ctx, &model)

Expand Down Expand Up @@ -230,8 +223,6 @@ func convertGrants(grants []Grant) []sdkBuckets.CreateParametersGrantFullControl
}

func (r *objectStorageBuckets) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
bucketResourceMutex.Lock()
defer bucketResourceMutex.Unlock()
var model ObjectStorageBucket
diags := req.State.Get(ctx, &model)

Expand All @@ -248,8 +239,6 @@ func (r *objectStorageBuckets) Update(ctx context.Context, req resource.UpdateRe
}

func (r *objectStorageBuckets) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
bucketResourceMutex.Lock()
defer bucketResourceMutex.Unlock()
var model ObjectStorageBucket
diags := req.State.Get(ctx, &model)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"math/big"
"sync"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
Expand All @@ -28,9 +27,8 @@ import (
)

var (
_ resource.Resource = &vmInstances{}
_ resource.ResourceWithConfigure = &vmInstances{}
vmResourceMutex = &sync.Mutex{}
_ resource.Resource = &vmInstances{}
_ resource.ResourceWithConfigure = &vmInstances{}
)

func NewVirtualMachineInstancesResource() resource.Resource {
Expand Down Expand Up @@ -312,8 +310,6 @@ func (r *vmInstances) ModifyPlanResponse(ctx context.Context, req resource.Modif
}

func (r *vmInstances) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
vmResourceMutex.Lock()
defer vmResourceMutex.Unlock()
data := vmInstancesResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

Expand All @@ -332,8 +328,6 @@ func (r *vmInstances) Read(ctx context.Context, req resource.ReadRequest, resp *
}

func (r *vmInstances) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
vmResourceMutex.Lock()
defer vmResourceMutex.Unlock()
plan := vmInstancesResourceModel{}

resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
Expand Down Expand Up @@ -422,8 +416,6 @@ func (r *vmInstances) Create(ctx context.Context, req resource.CreateRequest, re
}

func (r *vmInstances) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
vmResourceMutex.Lock()
defer vmResourceMutex.Unlock()
data := vmInstancesResourceModel{}
currState := &vmInstancesResourceModel{}
req.State.Get(ctx, currState)
Expand Down Expand Up @@ -470,8 +462,6 @@ func (r *vmInstances) Update(ctx context.Context, req resource.UpdateRequest, re
}

func (r *vmInstances) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
vmResourceMutex.Lock()
defer vmResourceMutex.Unlock()
var data vmInstancesResourceModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"sync"
"time"

"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -21,7 +20,6 @@ import (
var (
_ resource.Resource = &vmSnapshots{}
_ resource.ResourceWithConfigure = &vmSnapshots{}
snapshotsResourceMutex = &sync.Mutex{}
)

func NewVirtualMachineSnapshotsResource() resource.Resource {
Expand Down Expand Up @@ -119,9 +117,6 @@ func (r *vmSnapshots) getVmSnapshot(id string) (sdkVmSnapshots.GetResult, error)
}

func (r *vmSnapshots) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
snapshotsResourceMutex.Lock()
defer snapshotsResourceMutex.Unlock()

data := &vmSnapshotsResourceModel{}
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

Expand All @@ -141,8 +136,6 @@ func (r *vmSnapshots) Read(ctx context.Context, req resource.ReadRequest, resp *
}

func (r *vmSnapshots) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
snapshotsResourceMutex.Lock()
defer snapshotsResourceMutex.Unlock()

plan := &vmSnapshotsResourceModel{}
diags := req.Plan.Get(ctx, &plan)
Expand Down Expand Up @@ -182,8 +175,6 @@ func (r *vmSnapshots) Update(ctx context.Context, req resource.UpdateRequest, re
}

func (r *vmSnapshots) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
snapshotsResourceMutex.Lock()
defer snapshotsResourceMutex.Unlock()
var data vmSnapshotsResourceModel

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"fmt"
"sync"
"time"

"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -21,10 +20,6 @@ const (
AttachVolumeCompletedStatus = "completed"
)

var (
volumeAttachMutex = &sync.Mutex{}
)

type VolumeAttach struct {
sdkClient *mgcSdk.Client
blockStorageVolumes sdkVolumes.Service
Expand Down Expand Up @@ -81,8 +76,6 @@ func (r *VolumeAttach) Schema(ctx context.Context, req resource.SchemaRequest, r
}

func (r *VolumeAttach) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
volumeAttachMutex.Lock()
defer volumeAttachMutex.Unlock()
var model VolumeAttachResourceModel
diags := req.Plan.Get(ctx, &model)

Expand Down Expand Up @@ -116,8 +109,6 @@ func (r *VolumeAttach) Create(ctx context.Context, req resource.CreateRequest, r
}

func (r *VolumeAttach) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
volumeAttachMutex.Lock()
defer volumeAttachMutex.Unlock()
var model VolumeAttachResourceModel
diags := req.State.Get(ctx, &model)

Expand Down Expand Up @@ -150,8 +141,6 @@ func (r *VolumeAttach) Read(ctx context.Context, req resource.ReadRequest, resp
}

func (r *VolumeAttach) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
volumeAttachMutex.Lock()
defer volumeAttachMutex.Unlock()
var model VolumeAttachResourceModel
diags := req.Plan.Get(ctx, &model)

Expand Down Expand Up @@ -185,8 +174,6 @@ func (r *VolumeAttach) Update(ctx context.Context, req resource.UpdateRequest, r
}

func (r *VolumeAttach) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
volumeAttachMutex.Lock()
defer volumeAttachMutex.Unlock()
var model VolumeAttachResourceModel
diags := req.State.Get(ctx, &model)

Expand Down
Loading