Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Ability to Set Connection Timeout Values #289

Merged
merged 10 commits into from
Aug 30, 2017

Conversation

sgreen-r7
Copy link
Contributor

@sgreen-r7 sgreen-r7 commented Aug 24, 2017

  • Misc Whitespace/formatting clean-up.
  • Adding :timeout and :open_timeout to Nexpose::Connection.
    • Default for both :timeout and :open_timeout are set to 120 seconds.
    • The default values will populate down to anything that uses a http(s) timeouts.
    • Added comments with links to Ruby docs for further info about how the different http timeouts are used.
  • Added logging to $stderr when rescuing uncommon errors, asking users to open Github issues if these errors are seen in the wild.
  • Fixed Namespace errors in CredentialHelper, which were missed in Update for Credentials classes #287

return nil if url.nil? or url.empty?
uri = URI.parse(url)
http = Net::HTTP.new(@host, @port)
return nil if (url.nil? || url.empty?)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use parentheses around the condition of an if.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually right, but currently wrong houndci-bot ... this can cause issues with compound if's . i would leave this

@sgreen-r7 sgreen-r7 requested a review from jmartin-tech August 24, 2017 22:17
@rapid7 rapid7 deleted a comment from houndci-bot Aug 24, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Aug 24, 2017
@@ -129,15 +128,15 @@ def execute(options = {})
@error = "Error parsing response: #{exc.message}"
end

if !(@success or @error)
if !(@success || @error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Favor unless over if for negative conditions.

# If an explicit timeout is set, don't retry.
retry unless options.key? :timeout
if options.key?(:timeout)
retry if (time_tracker + options[:timeout].to_i) > Time.now

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid more than 3 levels of block nesting.

Copy link
Contributor

@jmartin-tech jmartin-tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me, I am not sure I would call this a breaking change as tagged since API does not change only the default behavior does. That is more up to the primary maintainers however.

# drops our HTTP connection before processing. We try 5 times to establish a
# connection in these situations. The actual exception occurs in the Ruby
# http library, which is why we use such generic error classes.
rescue OpenSSL::SSL::SSLError => e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know code inspection complains about these but capturing the object makes runtime debugging easier, I would prefer they be kept.

@password = pass
@token = token
@silo_id = silo_id
unless trust_cert.nil?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to nit pick this is another case where code inspection is not friendly to debugging, by making this a single line call you can lose break point granularity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give an example of what you mean? are you saying you'd want to put a breakdown on the inside the unless statement? if that's the case, you'd be able to just put the breakpoint inside the create_trust_store method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again is just a convenience thing to me being able to break when trust_cert.nil? without setting a conditional on the break point that is already in the code and see @trust_store before and after the call to create_trust_store(trust_cert)

In this case it may not be all that useful since create_trust_store is local code, I am just adding context about some things that rubocop thinks are bad that do little to impact performance but can help debugging and readability. Especially if the method called is from some utility library that is used heavily.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I see what you're saying. Personally, I am using breakpoints with binding.pry so for your example I would probably do binding.pry if trust_cert.nil? on a new line. Where I am assuming if we were to talk about "clicking an IDE" to create a breakpoint, that's what you're referring to.

That's a fair enough concern, and I'll keep it in mind going forward. On this particular line, do you have a preference if we leave it as is, or should I change it back?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine to leave as is.

if @conn_tries < 5
@conn_tries += 1
retry
end
rescue ::Timeout::Error
$stdout.puts @http.read_timeout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a debugging statement that needs to be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it will be, i'm still testing this branch

# If an explicit timeout is set, don't retry.
retry unless options.key? :timeout
if options.key?(:timeout)
retry if (time_tracker + options[:timeout].to_i) > Time.now
Copy link
Contributor

@jmartin-tech jmartin-tech Aug 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not really be about this PR but more a general thought, should the retry here even occur now? Originally this was retried up to 5 times if no :timeout value was ever set, since there is now a default :timeout and the request surpassed it, would it make sense to simplify this now to just end at a single timeout either the default or user specified one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scott and I were talking about not having retries at all anymore, maybe. At least as far as time out retries go, they are often more of a problem than just failing as many Nexpose API endpoints will do a lot of work on a heavy-load console before returning a response. Retrying in that case just repeats the work being done which we don't want to do IMO.

if @conn_tries < 5
@conn_tries += 1
retry
end
rescue ::Timeout::Error
rescue ::Timeout::Error => error

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless assignment to variable - error.

end
@error = "Nexpose did not respond within #{@http.read_timeout} seconds."
rescue ::Timeout::Error => error
@error = "Nexpose did not respond within #{@http.open_timeout} seconds. See <README> for information on setting the different Timeout values."
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will link to the wiki documentation once it's ready

attr_accessor :timeout
# The optional HTTP open_timeout value
# For more information visit the link below:
# https://ruby-doc.org/stdlib-<YOUR_RUBY_VERSION>/libdoc/net/http/rdoc/Net/HTTP.html#open_timeout-attribute-method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this link version-agnostic (i.e. latest) or just pick a version? might be confusing to newbies since it's not a valid link

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@rapid7 rapid7 deleted a comment from houndci-bot Aug 30, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Aug 30, 2017
@rapid7 rapid7 deleted a comment from houndci-bot Aug 30, 2017
@sgreen-r7 sgreen-r7 merged commit 96ca52b into master Aug 30, 2017
@sgreen-r7 sgreen-r7 deleted the adding_connection_timeouts branch September 1, 2017 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants