Skip to content

Commit 0cadabf

Browse files
committed
Merge branch '6.1.x' into 6.2.x
Closes gh-14568
2 parents fd1db06 + 75fdcd1 commit 0cadabf

File tree

1 file changed

+40
-55
lines changed

1 file changed

+40
-55
lines changed

web/src/main/java/org/springframework/security/web/server/ObservationWebFilterChainDecorator.java

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,10 +19,6 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.ListIterator;
22-
import java.util.concurrent.atomic.AtomicInteger;
23-
import java.util.concurrent.atomic.AtomicReference;
24-
import java.util.concurrent.locks.Lock;
25-
import java.util.concurrent.locks.ReentrantLock;
2622

2723
import io.micrometer.common.KeyValue;
2824
import io.micrometer.common.KeyValues;
@@ -258,12 +254,13 @@ default Observation after() {
258254

259255
class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
260256

257+
private final Object lock = new Object();
258+
261259
private final PhasedObservation before;
262260

263261
private final PhasedObservation after;
264262

265-
private final AtomicReference<PhasedObservation> currentObservation = new AtomicReference<>(
266-
PhasedObservation.NOOP);
263+
private volatile PhasedObservation currentObservation = PhasedObservation.NOOP;
267264

268265
SimpleAroundWebFilterObservation(Observation before, Observation after) {
269266
this.before = new PhasedObservation(before);
@@ -272,22 +269,26 @@ class SimpleAroundWebFilterObservation implements AroundWebFilterObservation {
272269

273270
@Override
274271
public Observation start() {
275-
if (this.currentObservation.compareAndSet(PhasedObservation.NOOP, this.before)) {
276-
this.before.start();
277-
return this.before.observation;
278-
}
279-
if (this.currentObservation.compareAndSet(this.before, this.after)) {
280-
this.before.stop();
281-
this.after.start();
282-
return this.after.observation;
272+
synchronized (this.lock) {
273+
if (this.currentObservation == PhasedObservation.NOOP) {
274+
this.before.start();
275+
this.currentObservation = this.before;
276+
return this.currentObservation;
277+
}
278+
if (this.currentObservation == this.before) {
279+
this.before.stop();
280+
this.after.start();
281+
this.currentObservation = this.after;
282+
return this.currentObservation;
283+
}
283284
}
284285
return Observation.NOOP;
285286
}
286287

287288
@Override
288289
public Observation error(Throwable ex) {
289-
this.currentObservation.get().error(ex);
290-
return this.currentObservation.get().observation;
290+
this.currentObservation.error(ex);
291+
return this.currentObservation.observation;
291292
}
292293

293294
@Override
@@ -303,42 +304,42 @@ private void close() {
303304

304305
@Override
305306
public Observation contextualName(String contextualName) {
306-
return this.currentObservation.get().observation.contextualName(contextualName);
307+
return this.currentObservation.observation.contextualName(contextualName);
307308
}
308309

309310
@Override
310311
public Observation parentObservation(Observation parentObservation) {
311-
return this.currentObservation.get().observation.parentObservation(parentObservation);
312+
return this.currentObservation.observation.parentObservation(parentObservation);
312313
}
313314

314315
@Override
315316
public Observation lowCardinalityKeyValue(KeyValue keyValue) {
316-
return this.currentObservation.get().observation.lowCardinalityKeyValue(keyValue);
317+
return this.currentObservation.observation.lowCardinalityKeyValue(keyValue);
317318
}
318319

319320
@Override
320321
public Observation highCardinalityKeyValue(KeyValue keyValue) {
321-
return this.currentObservation.get().observation.highCardinalityKeyValue(keyValue);
322+
return this.currentObservation.observation.highCardinalityKeyValue(keyValue);
322323
}
323324

324325
@Override
325326
public Observation observationConvention(ObservationConvention<?> observationConvention) {
326-
return this.currentObservation.get().observation.observationConvention(observationConvention);
327+
return this.currentObservation.observation.observationConvention(observationConvention);
327328
}
328329

329330
@Override
330331
public Observation event(Event event) {
331-
return this.currentObservation.get().observation.event(event);
332+
return this.currentObservation.observation.event(event);
332333
}
333334

334335
@Override
335336
public Context getContext() {
336-
return this.currentObservation.get().observation.getContext();
337+
return this.currentObservation.observation.getContext();
337338
}
338339

339340
@Override
340341
public Scope openScope() {
341-
return this.currentObservation.get().observation.openScope();
342+
return this.currentObservation.observation.openScope();
342343
}
343344

344345
@Override
@@ -386,7 +387,7 @@ public Observation after() {
386387

387388
@Override
388389
public String toString() {
389-
return this.currentObservation.get().observation.toString();
390+
return this.currentObservation.observation.toString();
390391
}
391392

392393
}
@@ -665,9 +666,9 @@ private static final class PhasedObservation implements Observation {
665666

666667
private static final PhasedObservation NOOP = new PhasedObservation(Observation.NOOP);
667668

668-
private final Lock lock = new ReentrantLock();
669+
private final Object lock = new Object();
669670

670-
private final AtomicInteger phase = new AtomicInteger(0);
671+
private volatile int phase = 0;
671672

672673
private final Observation observation;
673674

@@ -717,57 +718,41 @@ public Scope openScope() {
717718

718719
@Override
719720
public PhasedObservation start() {
720-
try {
721-
this.lock.lock();
722-
if (this.phase.compareAndSet(0, 1)) {
721+
synchronized (this.lock) {
722+
if (this.phase == 0) {
723723
this.observation.start();
724+
this.phase = 1;
724725
}
725726
}
726-
finally {
727-
this.lock.unlock();
728-
}
729727
return this;
730728
}
731729

732730
@Override
733731
public PhasedObservation error(Throwable ex) {
734-
try {
735-
this.lock.lock();
736-
if (this.phase.get() == 1) {
732+
synchronized (this.lock) {
733+
if (this.phase == 1) {
737734
this.observation.error(ex);
738735
}
739736
}
740-
finally {
741-
this.lock.unlock();
742-
}
743737
return this;
744738
}
745739

746740
@Override
747741
public void stop() {
748-
try {
749-
this.lock.lock();
750-
if (this.phase.compareAndSet(1, 2)) {
742+
synchronized (this.lock) {
743+
if (this.phase == 1) {
751744
this.observation.stop();
745+
this.phase = 2;
752746
}
753747
}
754-
finally {
755-
this.lock.unlock();
756-
}
757748
}
758749

759750
void close() {
760-
try {
761-
this.lock.lock();
762-
if (this.phase.compareAndSet(1, 3)) {
751+
synchronized (this.lock) {
752+
if (this.phase == 1) {
763753
this.observation.stop();
764754
}
765-
else {
766-
this.phase.set(3);
767-
}
768-
}
769-
finally {
770-
this.lock.unlock();
755+
this.phase = 3;
771756
}
772757
}
773758

0 commit comments

Comments
 (0)