Skip to content

Commit

Permalink
Merge pull request #216 from cfieber/master
Browse files Browse the repository at this point in the history
Make OkHttpClient prototype scope and add some configuration options for connectionPool/retry
  • Loading branch information
cfieber committed May 12, 2016
2 parents cbdd298 + 28ee723 commit 597c2d1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +43,9 @@ class EurekaLookupService {
@Autowired
ServiceConfiguration serviceConfiguration

@Autowired
OkHttpClient okHttpClient

@PostConstruct
void init() {
Executors.newScheduledThreadPool(1).scheduleAtFixedRate({
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 597c2d1

Please sign in to comment.