11package com .launchdarkly .client ;
22
3+ import com .google .common .util .concurrent .ThreadFactoryBuilder ;
34import com .google .gson .Gson ;
45import org .apache .http .HttpStatus ;
56import org .apache .http .client .methods .CloseableHttpResponse ;
1415import java .io .IOException ;
1516import java .util .ArrayList ;
1617import java .util .List ;
18+ import java .util .Random ;
1719import java .util .concurrent .*;
1820
1921class EventProcessor implements Closeable {
20- private final ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor (new DaemonThreadFactory ());
22+ private final ScheduledExecutorService scheduler ;
23+ private final Random random = new Random ();
2124 private final BlockingQueue <Event > queue ;
2225 private final String apiKey ;
26+ private final LDConfig config ;
2327 private final Consumer consumer ;
2428
2529 EventProcessor (String apiKey , LDConfig config ) {
2630 this .apiKey = apiKey ;
2731 this .queue = new ArrayBlockingQueue <>(config .capacity );
2832 this .consumer = new Consumer (config );
33+ this .config = config ;
34+ ThreadFactory threadFactory = new ThreadFactoryBuilder ()
35+ .setDaemon (true )
36+ .setNameFormat ("LaunchDarkly-EventProcessor-%d" )
37+ .build ();
38+ this .scheduler = Executors .newSingleThreadScheduledExecutor (threadFactory );
2939 this .scheduler .scheduleAtFixedRate (consumer , 0 , config .flushInterval , TimeUnit .SECONDS );
3040 }
3141
3242 boolean sendEvent (Event e ) {
43+ if (config .samplingInterval > 0 && random .nextInt (config .samplingInterval ) != 0 ) {
44+ return true ;
45+ }
3346 return queue .offer (e );
3447 }
3548
@@ -43,18 +56,8 @@ public void flush() {
4356 this .consumer .flush ();
4457 }
4558
46- static class DaemonThreadFactory implements ThreadFactory {
47- public Thread newThread (Runnable r ) {
48- Thread thread = new Thread (r );
49- thread .setDaemon (true );
50- return thread ;
51- }
52- }
53-
5459 class Consumer implements Runnable {
5560 private final Logger logger = LoggerFactory .getLogger (Consumer .class );
56-
57-
5861 private final CloseableHttpClient client ;
5962 private final LDConfig config ;
6063
@@ -78,6 +81,7 @@ public void flush() {
7881 }
7982
8083 private void postEvents (List <Event > events ) {
84+ logger .debug ("Posting " + events .size () + " event(s).." );
8185 CloseableHttpResponse response = null ;
8286 Gson gson = new Gson ();
8387 String json = gson .toJson (events );
@@ -95,24 +99,21 @@ private void postEvents(List<Event> events) {
9599 if (status >= 300 ) {
96100 if (status == HttpStatus .SC_UNAUTHORIZED ) {
97101 logger .error ("Invalid API key" );
98- }
99- else {
102+ } else {
100103 logger .error ("Unexpected status code: " + status );
101104 }
102- }
103- else {
104- logger .debug ("Successfully processed events" );
105+ } else {
106+ logger .debug ("Successfully posted " + events .size () + " event(s)." );
105107 }
106108 } catch (IOException e ) {
107- logger .error ("Unhandled exception in LaunchDarkly client" , e );
109+ logger .error ("Unhandled exception in LaunchDarkly client attempting to connect to URI: " + config . eventsURI , e );
108110 } finally {
109111 try {
110112 if (response != null ) response .close ();
111113 } catch (IOException e ) {
112114 logger .error ("Unhandled exception in LaunchDarkly client" , e );
113115 }
114116 }
115-
116117 }
117118 }
118119}
0 commit comments