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,7 @@ 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 lock = new ReentrantLock ();
5254 private static final Logger _log = LoggerFactory .getLogger (SSEClient .class );
5355 private final ExecutorService _connectionExecutor ;
5456 private final CloseableHttpClient _client ;
@@ -59,7 +61,6 @@ private enum ConnectionState {
5961 private final AtomicReference <HttpGet > _ongoingRequest = new AtomicReference <>();
6062 private AtomicBoolean _forcedStop ;
6163 private final RequestDecorator _requestDecorator ;
62-
6364 private final TelemetryRuntimeProducer _telemetryRuntimeProducer ;
6465
6566 public SSEClient (Function <RawEvent , Void > eventCallback ,
@@ -77,47 +78,57 @@ public SSEClient(Function<RawEvent, Void> eventCallback,
7778 _requestDecorator = requestDecorator ;
7879 }
7980
80- public synchronized boolean open (URI uri ) {
81- if (isOpen ()) {
82- _log .info ("SSEClient already open." );
83- return false ;
84- }
85-
86- _statusCallback .apply (StatusMessage .INITIALIZATION_IN_PROGRESS );
87-
88- CountDownLatch signal = new CountDownLatch (1 );
89- _connectionExecutor .submit (() -> connectAndLoop (uri , signal ));
81+ public boolean open (URI uri ) {
9082 try {
91- if (!signal .await (CONNECT_TIMEOUT , TimeUnit .SECONDS )) {
83+ lock .lock ();
84+ if (isOpen ()) {
85+ _log .info ("SSEClient already open." );
9286 return false ;
9387 }
94- } catch (InterruptedException e ) {
95- Thread .currentThread ().interrupt ();
96- if (e .getMessage () == null ){
97- _log .info ("The thread was interrupted while opening SSEClient" );
88+
89+ _statusCallback .apply (StatusMessage .INITIALIZATION_IN_PROGRESS );
90+
91+ CountDownLatch signal = new CountDownLatch (1 );
92+ _connectionExecutor .submit (() -> connectAndLoop (uri , signal ));
93+ try {
94+ if (!signal .await (CONNECT_TIMEOUT , TimeUnit .SECONDS )) {
95+ return false ;
96+ }
97+ } catch (InterruptedException e ) {
98+ Thread .currentThread ().interrupt ();
99+ if (e .getMessage () == null ){
100+ _log .info ("The thread was interrupted while opening SSEClient" );
101+ return false ;
102+ }
103+ _log .info (e .getMessage ());
98104 return false ;
99105 }
100- _log .info (e .getMessage ());
101- return false ;
106+ return isOpen ();
107+ } finally {
108+ lock .unlock ();
102109 }
103- return isOpen ();
104110 }
105111
106112 public boolean isOpen () {
107113 return (ConnectionState .OPEN .equals (_state .get ()));
108114 }
109115
110- public synchronized void close () {
111- _forcedStop .set (true );
112- if (_state .compareAndSet (ConnectionState .OPEN , ConnectionState .CLOSED )) {
113- if (_ongoingResponse .get () != null ) {
114- try {
115- _ongoingRequest .get ().abort ();
116- _ongoingResponse .get ().close ();
117- } catch (IOException e ) {
118- _log .debug (String .format ("SSEClient close forced: %s" , e .getMessage ()));
116+ public void close () {
117+ try {
118+ lock .lock ();
119+ _forcedStop .set (true );
120+ if (_state .compareAndSet (ConnectionState .OPEN , ConnectionState .CLOSED )) {
121+ if (_ongoingResponse .get () != null ) {
122+ try {
123+ _ongoingRequest .get ().abort ();
124+ _ongoingResponse .get ().close ();
125+ } catch (IOException e ) {
126+ _log .debug (String .format ("SSEClient close forced: %s" , e .getMessage ()));
127+ }
119128 }
120129 }
130+ } finally {
131+ lock .unlock ();
121132 }
122133 }
123134
0 commit comments