33import com .bugsnag .serialization .Expose ;
44
55import java .util .Date ;
6+ import java .util .concurrent .Semaphore ;
67import java .util .concurrent .atomic .AtomicInteger ;
78
89class Session {
910
11+ private final Semaphore incrementRequest = new Semaphore (1 );
12+
1013 private final String id ;
1114 private final Date startedAt ;
1215 private final AtomicInteger handledCount ;
@@ -19,6 +22,13 @@ class Session {
1922 this .unhandledCount = new AtomicInteger (0 );
2023 }
2124
25+ private Session (String id , Date startedAt , int handledCount , int unhandledCount ) {
26+ this .id = id ;
27+ this .startedAt = startedAt ;
28+ this .handledCount = new AtomicInteger (handledCount );
29+ this .unhandledCount = new AtomicInteger (unhandledCount );
30+ }
31+
2232 int getHandledCount () {
2333 return handledCount .get ();
2434 }
@@ -27,6 +37,16 @@ void incrementHandledCount() {
2737 this .handledCount .incrementAndGet ();
2838 }
2939
40+ Session incrementHandledCountAndClone () throws InterruptedException {
41+ try {
42+ incrementRequest .acquire ();
43+ incrementHandledCount ();
44+ return cloneSession ();
45+ } finally {
46+ incrementRequest .release ();
47+ }
48+ }
49+
3050 int getUnhandledCount () {
3151 return unhandledCount .get ();
3252 }
@@ -35,6 +55,20 @@ void incrementUnhandledCount() {
3555 this .unhandledCount .incrementAndGet ();
3656 }
3757
58+ Session incrementUnhandledCountAndClone () throws InterruptedException {
59+ try {
60+ incrementRequest .acquire ();
61+ incrementUnhandledCount ();
62+ return cloneSession ();
63+ } finally {
64+ incrementRequest .release ();
65+ }
66+ }
67+
68+ private Session cloneSession () {
69+ return new Session (id , startedAt , handledCount .get (), unhandledCount .get ());
70+ }
71+
3872 String getId () {
3973 return id ;
4074 }
0 commit comments