Skip to content

Commit 4f28788

Browse files
authored
Merge pull request #140 from CyberSource/feature/add-keep-alive-time
Feature/add keep alive time
2 parents 00f3106 + 393aea0 commit 4f28788

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

generator/cybersource-ruby-template/api_client.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require 'logger'
88
require 'tempfile'
99
require 'typhoeus'
1010
require 'addressable/uri'
11+
require_relative 'ethon_extensions'
1112

1213
module {{moduleName}}
1314
class ApiClient
@@ -144,7 +145,8 @@ module {{moduleName}}
144145
:sslcert => @config.cert_file,
145146
:sslkeypasswd => @merchantconfig.sslKeyPassword || "",
146147
:sslkey => @config.key_file,
147-
:verbose => @config.debugging
148+
:verbose => @config.debugging,
149+
:maxage_conn => @merchantconfig.keepAliveTime || 118 # Default to 118 seconds as same as default of libcurl
148150
}
149151

150152
# set custom cert, if provided

lib/AuthenticationSDK/core/MerchantConfig.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def initialize(cybsPropertyObj)
4444
@log_config = LogConfiguration.new(cybsPropertyObj['logConfiguration'])
4545
# Custom Default Headers
4646
@defaultCustomHeaders = cybsPropertyObj['defaultCustomHeaders']
47+
# Keep Alive Time for Connection Pooling
48+
@keepAliveTime = cybsPropertyObj['keepAliveTime'] || 118 # Default to 118 seconds as same as default of libcurl
4749
# Path to client JWE pem file directory
4850
@pemFileDirectory = cybsPropertyObj['pemFileDirectory']
4951
@mleKeyAlias = cybsPropertyObj['mleKeyAlias']
@@ -56,6 +58,11 @@ def initialize(cybsPropertyObj)
5658

5759
#fall back logic
5860
def validateMerchantDetails()
61+
if !@keepAliveTime.is_a?(Integer)
62+
err = StandardError.new(Constants::ERROR_PREFIX + "keepAliveTime must be an integer and in seconds")
63+
raise err
64+
end
65+
5966
logmessage = ''
6067
@log_config.validate(logmessage)
6168
@log_obj = Log.new @log_config, "MerchantConfig"
@@ -306,6 +313,7 @@ def logAllProperties(merchantPropertyObj)
306313
attr_accessor :keyFilename
307314
attr_accessor :useMetaKey
308315
attr_accessor :portfolioID
316+
attr_accessor :keepAliveTime
309317
attr_accessor :enableClientCert
310318
attr_accessor :clientCertDirectory
311319
attr_accessor :sslClientCert

lib/cybersource_rest_client/api_client.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require 'tempfile'
1616
require 'typhoeus'
1717
require 'addressable/uri'
18+
require_relative 'ethon_extensions'
1819

1920
module CyberSource
2021
class ApiClient
@@ -148,7 +149,8 @@ def build_request(http_method, path, opts = {})
148149
:sslcert => @config.cert_file,
149150
:sslkeypasswd => @merchantconfig.sslKeyPassword || "",
150151
:sslkey => @config.key_file,
151-
:verbose => @config.debugging
152+
:verbose => @config.debugging,
153+
:maxage_conn => @merchantconfig.keepAliveTime || 118 # Default to 118 seconds as same as default of libcurl
152154
}
153155

154156
# set custom cert, if provided
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
# Extension to add CURLOPT_MAXAGE_CONN support to Ethon without modifying the gem
4+
# This file extends Ethon to add the maxage_conn option
5+
6+
require 'ethon'
7+
8+
# Now extend the Easy class to add the setter/getter methods
9+
# This follows the same pattern as other Ethon options
10+
module Ethon
11+
class Easy
12+
# Add the maxage_conn setter method - it will get call from set_attributes(options) from http actionable in ethon lib.
13+
def maxage_conn=(value)
14+
return if value.nil?
15+
@maxage_conn = value.to_i
16+
17+
# CURLOPT_MAXAGE_CONN = CURLOPTTYPE_LONG + 288
18+
# CURLOPTTYPE_LONG = 0, so the option value is 0 + 288 = 288
19+
curl_option = 288
20+
# Call curl directly using Ethon::Curl.easy_setopt
21+
Ethon::Curl.easy_setopt(@handle, curl_option, :long, @maxage_conn)
22+
end
23+
24+
# Add the maxage_conn getter method
25+
def maxage_conn
26+
@maxage_conn
27+
end
28+
29+
# Add alias methods
30+
def max_connection_age=(value)
31+
self.maxage_conn = value
32+
end
33+
34+
def max_connection_age
35+
@maxage_conn
36+
end
37+
end
38+
end

0 commit comments

Comments
 (0)