Overriding/Modifying Rails Class (ActiveResource)

2019-08-10 11:29发布

I've been struggling with an issue with ActiveResource for a bit now: when a hostname resolves for an ActiveResource request, but there's no server on the other end to return information, ActiveResource's timeout value doesn't work. The request just hangs.

After reviewing the ActiveResource code, I've realized that this is because the underlying Net:Http object only has one timeout value set: read_timeout. The Net:Http library defines this as "Seconds to wait until reading one block (by one read(2) call)". The Net:Http lib also defines another timeout value, open_timeout, which is defined as "Seconds to wait until connection is opened".

I'm unsure why open_timeout isn't set by default with the timeout value set on an ActiveResource class, but after modifying the ActiveResource::Connection class to include the open_timeout on the http objects, my issue was resolved!

I'm new to rails so I'm unsure of the best way to actually make this modification in my project; I don't want to just change the code in my gem directory. Is there a proper way to make these modifications in a rails project? I've seen that it's possible to load rails classes from the /vendor folder, but do they all have to be there for it to work? I started to make subclasses of the ActiveResource::Base and ActiveResource::Connection classes, but it seemed like maybe there was an easier way to do this, as the function that creates the Net:Http instance is private...any thoughts?

1条回答
相关推荐>>
2楼-- · 2019-08-10 11:56

First of all, this is an issue you should report to Rails' bugtracker: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/

When I have to hotfix minor things like these, I usually create an initializer file in RAILS_ROOT/config/initializers and reopen the class I intend to fix.

class ActiveResource::Base
  # your fix goes here
end

This is called monkey patching and is somewhat controversial. But I personally consider it quite brutal to introduce a new level into the inheritance hierarchy that has no semantical meaning to my code.

查看更多
登录 后发表回答