66import io .split .telemetry .domain .enums .StreamEventsEnum ;
77import io .split .telemetry .storage .TelemetryRuntimeProducer ;
88import org .apache .hc .client5 .http .classic .methods .HttpGet ;
9- import org .apache .hc .client5 .http .classic .methods .HttpPost ;
109import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
1110import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
1211import org .slf4j .Logger ;
2524import java .util .concurrent .TimeUnit ;
2625import java .util .concurrent .atomic .AtomicBoolean ;
2726import java .util .concurrent .atomic .AtomicReference ;
27+ import java .util .concurrent .locks .Lock ;
28+ import java .util .concurrent .locks .ReentrantLock ;
2829import java .util .function .Function ;
2930
3031import static com .google .common .base .Preconditions .checkNotNull ;
@@ -49,6 +50,8 @@ private enum ConnectionState {
4950 private final static String SOCKET_CLOSED_MESSAGE = "Socket closed" ;
5051 private final static String KEEP_ALIVE_PAYLOAD = ":keepalive\n " ;
5152 private final static long CONNECT_TIMEOUT = 30000 ;
53+ private static final Lock openLock = new ReentrantLock ();
54+ private static final Lock closeLock = new ReentrantLock ();
5255 private static final Logger _log = LoggerFactory .getLogger (SSEClient .class );
5356 private final ExecutorService _connectionExecutor ;
5457 private final CloseableHttpClient _client ;
@@ -60,7 +63,6 @@ private enum ConnectionState {
6063 private AtomicBoolean _forcedStop ;
6164 private final RequestDecorator _requestDecorator ;
6265 private final TelemetryRuntimeProducer _telemetryRuntimeProducer ;
63- private final AtomicBoolean openGuard = new AtomicBoolean (false );
6466
6567 public SSEClient (Function <RawEvent , Void > eventCallback ,
6668 Function <StatusMessage , Void > statusCallback ,
@@ -78,53 +80,56 @@ public SSEClient(Function<RawEvent, Void> eventCallback,
7880 }
7981
8082 public boolean open (URI uri ) {
81- if (isOpen ()) {
82- _log .info ("SSEClient already open." );
83- return false ;
84- }
85-
86- if (!openGuard .compareAndSet (false , true )) {
87- _log .debug ("Open SSEClient already running" );
88- return false ;
89- }
90-
91- _statusCallback .apply (StatusMessage .INITIALIZATION_IN_PROGRESS );
92-
93- CountDownLatch signal = new CountDownLatch (1 );
94- _connectionExecutor .submit (() -> connectAndLoop (uri , signal ));
9583 try {
96- if (!signal .await (CONNECT_TIMEOUT , TimeUnit .SECONDS )) {
84+ openLock .lock ();
85+ if (isOpen ()) {
86+ _log .info ("SSEClient already open." );
9787 return false ;
9888 }
99- } catch (InterruptedException e ) {
100- Thread .currentThread ().interrupt ();
101- if (e .getMessage () == null ){
102- _log .info ("The thread was interrupted while opening SSEClient" );
89+
90+ _statusCallback .apply (StatusMessage .INITIALIZATION_IN_PROGRESS );
91+
92+ CountDownLatch signal = new CountDownLatch (1 );
93+ _connectionExecutor .submit (() -> connectAndLoop (uri , signal ));
94+ try {
95+ if (!signal .await (CONNECT_TIMEOUT , TimeUnit .SECONDS )) {
96+ return false ;
97+ }
98+ } catch (InterruptedException e ) {
99+ Thread .currentThread ().interrupt ();
100+ if (e .getMessage () == null ){
101+ _log .info ("The thread was interrupted while opening SSEClient" );
102+ return false ;
103+ }
104+ _log .info (e .getMessage ());
103105 return false ;
104106 }
105- _log .info (e .getMessage ());
106- return false ;
107+ return isOpen ();
107108 } finally {
108- openGuard . set ( false );
109+ openLock . unlock ( );
109110 }
110- return isOpen ();
111111 }
112112
113113 public boolean isOpen () {
114114 return (ConnectionState .OPEN .equals (_state .get ()));
115115 }
116116
117- public synchronized void close () {
118- _forcedStop .set (true );
119- if (_state .compareAndSet (ConnectionState .OPEN , ConnectionState .CLOSED )) {
120- if (_ongoingResponse .get () != null ) {
121- try {
122- _ongoingRequest .get ().abort ();
123- _ongoingResponse .get ().close ();
124- } catch (IOException e ) {
125- _log .debug (String .format ("SSEClient close forced: %s" , e .getMessage ()));
117+ public void close () {
118+ try {
119+ closeLock .lock ();
120+ _forcedStop .set (true );
121+ if (_state .compareAndSet (ConnectionState .OPEN , ConnectionState .CLOSED )) {
122+ if (_ongoingResponse .get () != null ) {
123+ try {
124+ _ongoingRequest .get ().abort ();
125+ _ongoingResponse .get ().close ();
126+ } catch (IOException e ) {
127+ _log .debug (String .format ("SSEClient close forced: %s" , e .getMessage ()));
128+ }
126129 }
127130 }
131+ } finally {
132+ closeLock .unlock ();
128133 }
129134 }
130135
0 commit comments