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

Out of range latitude and longitude #29

Closed
kenn opened this issue Jun 12, 2013 · 8 comments
Closed

Out of range latitude and longitude #29

kenn opened this issue Jun 12, 2013 · 8 comments

Comments

@kenn
Copy link

kenn commented Jun 12, 2013

I'm using this gem to retrieve lat/lon from the user's IP, like so:

city = GeoIP::DB.city(@user.ip)
@user.lat = city.try(:latitude)
@user.lng = city.try(:longitude)

But I'm getting incorrect values sporadically.

@user.ip => "182.249.223.182",
@user.lat => 1387.872 - #<BigDecimal:f5f9c04,'0.1387872E4',18(45)>,
@user.lng => 1387.9004 - #<BigDecimal:f5f93d0,'0.13879004E4',18(45)>

If I run the same query from console, it works as expected:

> city = GeoIP::DB.city('182.249.223.182')
> city.try(:latitude)
=> 36.0
> city.try(:longitude)
=> 138.0

So, I'm guessing it has something to do with long-running reuse of the same GeoIP instance? GeoIP::DB is initialized at boot, only once:

class GeoIP
  DB = GeoIP.new(Rails.root.join('some/path/to/GeoLiteCity.dat'))
end

Thoughts?

@cjheath
Copy link
Owner

cjheath commented Jun 12, 2013

GeoIP is not thread-safe, because it seeks a file pointer and reads from that location multiple times for each lookup. If you are initialising it once, you should store the handle in thread-local storage.

@cjheath cjheath closed this as completed Jun 12, 2013
@kenn
Copy link
Author

kenn commented Jun 12, 2013

I'm not using threads, just a regular Rails app on Unicorn, which is single threaded. Do I still need to assign it to thread-local?

@cjheath
Copy link
Owner

cjheath commented Jun 12, 2013

Try it with a Mutex and see if you still get problems, but the behaviour you describe sounds very like a race.

@kenn
Copy link
Author

kenn commented Jun 12, 2013

Well, we don't have config.threadsafe! on production, which means all requests are inside Rack::Lock so it's already mutex'd.

@cjheath
Copy link
Owner

cjheath commented Jun 12, 2013

Perhaps, but try it anyway. EventMachine can still flip things around, I believe.

@kenn
Copy link
Author

kenn commented Jun 12, 2013

Will try, but I doubt that's the direct cause. We're not using EventMachine either. (If you use EM you'd use Rainbows! instead of Unicorn) It's a vanilla Rails 3.2 app with Unicorn, which I believe is the most popular setup.

@cjheath
Copy link
Owner

cjheath commented Jun 12, 2013

Ok, well then I have no idea... but a lot of folk are using GeoIP without problems, including long-running instances. If you can find a cause and/or a fix, I'll be happy to re-open this issue.

@kenn
Copy link
Author

kenn commented Jun 12, 2013

Yeah I had been using the gem for quite a while without problems, then the problem started to appear at some point. I'll update when I find something out. Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants