1- package dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .sync ;
1+ package dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .sync . http ;
22
33import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .QueuePayload ;
44import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .QueuePayloadType ;
55import dev .openfeature .contrib .providers .flagd .resolver .process .storage .connector .QueueSource ;
66import dev .openfeature .contrib .providers .flagd .util .ConcurrentUtils ;
77import lombok .Builder ;
88import lombok .NonNull ;
9- import lombok .SneakyThrows ;
109import lombok .extern .slf4j .Slf4j ;
1110
1211import java .io .IOException ;
1312import java .net .InetSocketAddress ;
1413import java .net .ProxySelector ;
1514import java .net .URI ;
16- import java .net .URL ;
1715import java .net .http .HttpClient ;
1816import java .net .http .HttpRequest ;
1917import java .net .http .HttpResponse ;
2018import java .time .Duration ;
21- import java .util .HashMap ;
2219import java .util .Map ;
2320import java .util .concurrent .BlockingQueue ;
2421import java .util .concurrent .ExecutorService ;
4340@ Slf4j
4441public class HttpConnector implements QueueSource {
4542
46- private static final int DEFAULT_POLL_INTERVAL_SECONDS = 60 ;
47- private static final int DEFAULT_LINKED_BLOCKING_QUEUE_CAPACITY = 100 ;
48- private static final int DEFAULT_SCHEDULED_THREAD_POOL_SIZE = 2 ;
49- private static final int DEFAULT_REQUEST_TIMEOUT_SECONDS = 10 ;
50- private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 10 ;
51-
5243 private Integer pollIntervalSeconds ;
5344 private Integer requestTimeoutSeconds ;
5445 private BlockingQueue <QueuePayload > queue ;
@@ -64,85 +55,36 @@ public class HttpConnector implements QueueSource {
6455 private String url ;
6556
6657 @ Builder
67- public HttpConnector (Integer pollIntervalSeconds , Integer linkedBlockingQueueCapacity ,
68- Integer scheduledThreadPoolSize , Integer requestTimeoutSeconds , Integer connectTimeoutSeconds , String url ,
69- Map <String , String > headers , ExecutorService httpClientExecutor , String proxyHost , Integer proxyPort ,
70- PayloadCacheOptions payloadCacheOptions , PayloadCache payloadCache , Boolean useHttpCache ) {
71- validate (url , pollIntervalSeconds , linkedBlockingQueueCapacity , scheduledThreadPoolSize , requestTimeoutSeconds ,
72- connectTimeoutSeconds , proxyHost , proxyPort , payloadCacheOptions , payloadCache );
73- this .pollIntervalSeconds = pollIntervalSeconds == null ? DEFAULT_POLL_INTERVAL_SECONDS : pollIntervalSeconds ;
74- int thisLinkedBlockingQueueCapacity = linkedBlockingQueueCapacity == null ? DEFAULT_LINKED_BLOCKING_QUEUE_CAPACITY : linkedBlockingQueueCapacity ;
75- int thisScheduledThreadPoolSize = scheduledThreadPoolSize == null ? DEFAULT_SCHEDULED_THREAD_POOL_SIZE : scheduledThreadPoolSize ;
76- this .requestTimeoutSeconds = requestTimeoutSeconds == null ? DEFAULT_REQUEST_TIMEOUT_SECONDS : requestTimeoutSeconds ;
77- int thisConnectTimeoutSeconds = connectTimeoutSeconds == null ? DEFAULT_CONNECT_TIMEOUT_SECONDS : connectTimeoutSeconds ;
58+ public HttpConnector (HttpConnectorOptions httpConnectorOptions ) {
59+ this .pollIntervalSeconds = httpConnectorOptions .getPollIntervalSeconds ();
60+ this .requestTimeoutSeconds = httpConnectorOptions .getRequestTimeoutSeconds ();
7861 ProxySelector proxySelector = NO_PROXY ;
79- if (proxyHost != null && proxyPort != null ) {
80- proxySelector = ProxySelector .of (new InetSocketAddress (proxyHost , proxyPort ));
81- }
82-
83- this .url = url ;
84- this .headers = headers ;
85- this .httpClientExecutor = httpClientExecutor == null ? Executors .newFixedThreadPool (1 ) :
86- httpClientExecutor ;
87- scheduler = Executors .newScheduledThreadPool (thisScheduledThreadPoolSize );
88- if (headers == null ) {
89- this .headers = new HashMap <>();
90- }
62+ if (httpConnectorOptions .getProxyHost () != null && httpConnectorOptions .getProxyPort () != null ) {
63+ proxySelector = ProxySelector .of (new InetSocketAddress (httpConnectorOptions .getProxyHost (),
64+ httpConnectorOptions .getProxyPort ()));
65+ }
66+ this .url = httpConnectorOptions .getUrl ();
67+ this .headers = httpConnectorOptions .getHeaders ();
68+ this .httpClientExecutor = httpConnectorOptions .getHttpClientExecutor ();
69+ scheduler = Executors .newScheduledThreadPool (httpConnectorOptions .getScheduledThreadPoolSize ());
9170 this .client = HttpClient .newBuilder ()
92- .connectTimeout (Duration .ofSeconds (thisConnectTimeoutSeconds ))
71+ .connectTimeout (Duration .ofSeconds (httpConnectorOptions . getConnectTimeoutSeconds () ))
9372 .proxy (proxySelector )
9473 .executor (this .httpClientExecutor )
9574 .build ();
96- this .queue = new LinkedBlockingQueue <>(thisLinkedBlockingQueueCapacity );
97- this .payloadCache = payloadCache ;
75+ this .queue = new LinkedBlockingQueue <>(httpConnectorOptions . getLinkedBlockingQueueCapacity () );
76+ this .payloadCache = httpConnectorOptions . getPayloadCache () ;
9877 if (payloadCache != null ) {
9978 this .payloadCacheWrapper = PayloadCacheWrapper .builder ()
10079 .payloadCache (payloadCache )
101- .payloadCacheOptions (payloadCacheOptions )
80+ .payloadCacheOptions (httpConnectorOptions . getPayloadCacheOptions () )
10281 .build ();
10382 }
104- if (Boolean .TRUE .equals (useHttpCache )) {
83+ if (Boolean .TRUE .equals (httpConnectorOptions . getUseHttpCache () )) {
10584 httpCacheFetcher = new HttpCacheFetcher ();
10685 }
10786 }
10887
109- @ SneakyThrows
110- private void validate (String url , Integer pollIntervalSeconds , Integer linkedBlockingQueueCapacity ,
111- Integer scheduledThreadPoolSize , Integer requestTimeoutSeconds , Integer connectTimeoutSeconds ,
112- String proxyHost , Integer proxyPort , PayloadCacheOptions payloadCacheOptions ,
113- PayloadCache payloadCache ) {
114- new URL (url ).toURI ();
115- if (pollIntervalSeconds != null && (pollIntervalSeconds < 1 || pollIntervalSeconds > 600 )) {
116- throw new IllegalArgumentException ("pollIntervalSeconds must be between 1 and 600" );
117- }
118- if (linkedBlockingQueueCapacity != null && (linkedBlockingQueueCapacity < 1 || linkedBlockingQueueCapacity > 1000 )) {
119- throw new IllegalArgumentException ("linkedBlockingQueueCapacity must be between 1 and 1000" );
120- }
121- if (scheduledThreadPoolSize != null && (scheduledThreadPoolSize < 1 || scheduledThreadPoolSize > 10 )) {
122- throw new IllegalArgumentException ("scheduledThreadPoolSize must be between 1 and 10" );
123- }
124- if (requestTimeoutSeconds != null && (requestTimeoutSeconds < 1 || requestTimeoutSeconds > 60 )) {
125- throw new IllegalArgumentException ("requestTimeoutSeconds must be between 1 and 60" );
126- }
127- if (connectTimeoutSeconds != null && (connectTimeoutSeconds < 1 || connectTimeoutSeconds > 60 )) {
128- throw new IllegalArgumentException ("connectTimeoutSeconds must be between 1 and 60" );
129- }
130- if (proxyPort != null && (proxyPort < 1 || proxyPort > 65535 )) {
131- throw new IllegalArgumentException ("proxyPort must be between 1 and 65535" );
132- }
133- if (proxyHost != null && proxyPort == null ) {
134- throw new IllegalArgumentException ("proxyPort must be set if proxyHost is set" );
135- } else if (proxyHost == null && proxyPort != null ) {
136- throw new IllegalArgumentException ("proxyHost must be set if proxyPort is set" );
137- }
138- if (payloadCacheOptions != null && payloadCache == null ) {
139- throw new IllegalArgumentException ("payloadCache must be set if payloadCacheOptions is set" );
140- }
141- if (payloadCache != null && payloadCacheOptions == null ) {
142- throw new IllegalArgumentException ("payloadCacheOptions must be set if payloadCache is set" );
143- }
144- }
145-
14688 @ Override
14789 public void init () throws Exception {
14890 log .info ("init Http Connector" );
@@ -158,7 +100,7 @@ public BlockingQueue<QueuePayload> getStreamQueue() {
158100 }
159101 }
160102 Runnable pollTask = buildPollTask ();
161- scheduler .scheduleAtFixedRate (pollTask , pollIntervalSeconds , pollIntervalSeconds , TimeUnit .SECONDS );
103+ scheduler .scheduleWithFixedDelay (pollTask , pollIntervalSeconds , pollIntervalSeconds , TimeUnit .SECONDS );
162104 return queue ;
163105 }
164106
0 commit comments