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

same request produces different response bodies (body inconsistently returns instance of Hash or String) #1388

Open
ksilex opened this issue Jan 26, 2022 · 4 comments

Comments

@ksilex
Copy link

ksilex commented Jan 26, 2022

Basic Info

  • Faraday Version: 1.6.0
  • Ruby Version: 2.7.4

Issue description

seems like when requests done in parallel response body doesn't get parsed

Steps to reproduce

def run1
    req = Faraday.new(config[:url]) do |conn|
      conn.response :json, content_type: %r{application/json}
    end
    req.get('/') do |conn|
      conn.body = body
    end
end

Screenshot_2

also seen similar problem here: https://stackoverflow.com/questions/67182790/error-response-not-parsed-by-faraday-despite-adding-json-middleware
except no middlewares were used

@olleolleolle
Copy link
Member

👋 Can you attempt with a higher version of 1.x series?

@olleolleolle
Copy link
Member

olleolleolle commented Jan 27, 2022

Update: Here's a Bundler inline example, which allows you to pick versions used and have the example actually runnable.

I guessed at some information, so I may be off: this program only returned [Hash, Hash...] for me.

(Edited the example.)

#!/usr/bin/env ruby

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem "faraday", "= 1.6.0"
  gem "faraday_middleware"
end

def run
  url = "https://httpbingo.org"
  body = "{}"
  req = Faraday.new(url) do |conn|
    conn.response :json, content_type: %r{application/json}
  end
  req.get('/json') do |conn|
    conn.body = body
  end
end

def thread_run
  threads = []
  10.times do
    threads << Thread.new do
      run.body.class
    end
  end
  threads.map(&:value)
end

pp thread_run

@ksilex
Copy link
Author

ksilex commented Jan 27, 2022

olleolleolle

I'm sorry, it seems like problem can occure only when requesting ClickHouse database through http method, sometimes it parsed clickhouse's json response(returned Hash instance), sometimes returned it raw (as String instance)
Also, it's never the case, when requests done successively (always returned Hash instance)

@iMacTia
Copy link
Member

iMacTia commented Jan 27, 2022

@ksilex sorry you're having this issue, could you please add the logger middleware to your stack and check if the failing responses have anything different from the others?

req = Faraday.new(config[:url]) do |conn|
  conn.response :logger, nil, { headers: true, bodies: true }
  conn.response :json, content_type: %r{application/json}
end

If you need, more info on the logger middleware are available here: https://lostisland.github.io/faraday/middleware/logger
It's also worth pointing out that ClickHouse provide a database driver for Ruby, so I'm not sure if it makes sense to implement your own connector in this case. I'd suggest using the driver instead (there's even an ActiveRecord adapter)

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

No branches or pull requests

3 participants