@@ -141,11 +141,6 @@ func (r *StateStore) Set(req *state.SetRequest) error {
141141 r .logger .Debugf ("saving %s" , req .Key )
142142
143143 err := r .writeRow (req )
144- if err != nil {
145- if req .ETag != nil {
146- return state .NewETagError (state .ETagMismatch , err )
147- }
148- }
149144
150145 return err
151146}
@@ -198,20 +193,25 @@ func (r *StateStore) writeRow(req *state.SetRequest) error {
198193 }
199194 entity .OdataEtag = etag
200195
201- // InsertOrReplace does not support ETag concurrency, therefore we will try to use Update method first
202- // as it's more frequent, and then Insert
196+ // InsertOrReplace does not support ETag concurrency, therefore we will use Insert to check for key existence
197+ // and then use Update to update the key if it exists with the specified ETag
203198
204- err := entity .Update ( false , nil )
199+ err := entity .Insert ( storage . FullMetadata , nil )
205200 if err != nil {
206- if isNotFoundError (err ) {
207- // When entity is not found (set state first time) create it
208- entity .OdataEtag = ""
209-
210- return entity .Insert (storage .FullMetadata , nil )
201+ if etag == "" {
202+ return state .NewETagError (state .ETagMismatch , err )
203+ }
204+ err := entity .Update (false , nil )
205+ if err != nil {
206+ if isNotFoundError (err ) {
207+ return state .NewETagError (state .ETagMismatch , err )
208+ }
211209 }
210+
211+ return err
212212 }
213213
214- return err
214+ return nil
215215}
216216
217217func isNotFoundError (err error ) bool {
@@ -233,9 +233,13 @@ func (r *StateStore) deleteRow(req *state.DeleteRequest) error {
233233 if req .ETag != nil {
234234 entity .OdataEtag = * req .ETag
235235
236+ // force=false sets the "If-Match: <ETag>" header to ensure that the delete is only performed if the
237+ // entity's ETag matches the specified ETag
236238 return entity .Delete (false , nil )
237239 }
238240
241+ // force=true sets the "If-Match: *" header to ensure that we delete a matching entity
242+ // regardless of the entity's ETag value
239243 return entity .Delete (true , nil )
240244}
241245
0 commit comments