From 28ee723d5de3cd473739bb69f01f054dbc0be59b Mon Sep 17 00:00:00 2001 From: Cameron Fieber Date: Thu, 12 May 2016 11:21:57 -0700 Subject: [PATCH] Make OkHttpClient prototype scope and add some configuration options for connectionPool/retry. --- .../gate/services/EurekaLookupService.groovy | 10 ++-- .../spinnaker/gate/config/GateConfig.groovy | 9 ---- .../gate/config/RetrofitConfig.groovy | 52 +++++++++++++++++++ 3 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/RetrofitConfig.groovy diff --git a/gate-core/src/main/groovy/com/netflix/spinnaker/gate/services/EurekaLookupService.groovy b/gate-core/src/main/groovy/com/netflix/spinnaker/gate/services/EurekaLookupService.groovy index 2033a42a82..a318890344 100644 --- a/gate-core/src/main/groovy/com/netflix/spinnaker/gate/services/EurekaLookupService.groovy +++ b/gate-core/src/main/groovy/com/netflix/spinnaker/gate/services/EurekaLookupService.groovy @@ -22,6 +22,7 @@ import com.netflix.spinnaker.gate.config.ServiceConfiguration import com.netflix.spinnaker.gate.model.discovery.DiscoveryApplication import com.netflix.spinnaker.gate.retrofit.Slf4jRetrofitLogger import com.netflix.spinnaker.gate.services.internal.EurekaService +import com.squareup.okhttp.OkHttpClient import groovy.transform.Immutable import java.util.concurrent.* import javax.annotation.PostConstruct @@ -42,6 +43,9 @@ class EurekaLookupService { @Autowired ServiceConfiguration serviceConfiguration + @Autowired + OkHttpClient okHttpClient + @PostConstruct void init() { Executors.newScheduledThreadPool(1).scheduleAtFixedRate({ @@ -83,13 +87,13 @@ class EurekaLookupService { app.applications } - private static EurekaService getEurekaService(String host) { - def endpoint = retrofit.Endpoints.newFixedEndpoint(host) + private EurekaService getEurekaService(String host) { + def endpoint = newFixedEndpoint(host) new RestAdapter.Builder() .setEndpoint(endpoint) .setConverter(new JacksonConverter(new ObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true))) - .setClient(new OkClient()) + .setClient(new OkClient(okHttpClient)) .setLogLevel(RestAdapter.LogLevel.BASIC) .setLog(new Slf4jRetrofitLogger(EurekaService)) .build() diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy index 3f74d957a6..a1ee7713a6 100644 --- a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/GateConfig.groovy @@ -23,7 +23,6 @@ import javax.servlet.http.HttpServletRequest import javax.servlet.http.HttpServletResponse import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext import com.netflix.spectator.api.Registry -import com.netflix.spinnaker.config.OkHttpClientConfiguration import com.netflix.spinnaker.filters.AuthenticatedRequestFilter import com.netflix.spinnaker.gate.retrofit.EurekaOkClient import com.netflix.spinnaker.gate.retrofit.Slf4jRetrofitLogger @@ -97,14 +96,6 @@ class GateConfig { @Autowired ServiceConfiguration serviceConfiguration - @Autowired - OkHttpClientConfiguration okHttpClientConfig - - @Bean - OkHttpClient okHttpClient() { - okHttpClientConfig.create() - } - @Bean OrcaService orcaService(OkHttpClient okHttpClient) { createClient "orca", OrcaService, okHttpClient diff --git a/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/RetrofitConfig.groovy b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/RetrofitConfig.groovy new file mode 100644 index 0000000000..69d9011215 --- /dev/null +++ b/gate-web/src/main/groovy/com/netflix/spinnaker/gate/config/RetrofitConfig.groovy @@ -0,0 +1,52 @@ +/* + * Copyright 2016 Netflix, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.netflix.spinnaker.gate.config + +import com.netflix.spinnaker.config.OkHttpClientConfiguration +import com.squareup.okhttp.ConnectionPool +import com.squareup.okhttp.OkHttpClient +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.beans.factory.config.ConfigurableBeanFactory +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Scope + +@Configuration +class RetrofitConfig { + @Value('${okHttpClient.connectionPool.maxIdleConnections:5}') + int maxIdleConnections + + @Value('${okHttpClient.connectionPool.keepAliveDurationMs:300000}') + int keepAliveDurationMs + + @Value('${okHttpClient.retryOnConnectionFailure:true}') + boolean retryOnConnectionFailure + + @Autowired + OkHttpClientConfiguration okHttpClientConfig + + @Bean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + OkHttpClient okHttpClient() { + def okHttpClient = okHttpClientConfig.create() + okHttpClient.connectionPool = new ConnectionPool(maxIdleConnections, keepAliveDurationMs) + okHttpClient.retryOnConnectionFailure = retryOnConnectionFailure + return okHttpClient + } + +}