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

Handle dying DB connections #75

Open
vladfaust opened this issue Mar 3, 2019 · 6 comments
Open

Handle dying DB connections #75

vladfaust opened this issue Mar 3, 2019 · 6 comments
Assignees
Labels
enhancement New feature or request
Projects

Comments

@vladfaust
Copy link
Member

Retry multiple times, then raise.

@vladfaust vladfaust added the enhancement New feature or request label Mar 3, 2019
@vladfaust vladfaust self-assigned this Mar 3, 2019
@vladfaust vladfaust added this to To do in Kanban via automation Mar 3, 2019
@acidtib
Copy link
Contributor

acidtib commented May 30, 2019

Recently we started to run into this issue. Seems to happen when dead connections are involved, we are using DigitalOceans Managed database

INFO [16:52:57.861 #1] [c1a2aeae]   POST /atm/new 500 9.568ms
 INFO [16:53:58.708 #1] Endpoints::Atm::New::Params::JSON(@machine_id="xxxxxxx-xxxxxx-xxxxxx")
DEBUG [16:53:58.708 #1] [postgresql] SELECT atms.id FROM atms WHERE (atms.machine_id = $1)
ERROR [16:53:58.713 #1] [2fe65c94]  ERROR  Error writing to socket: Broken pipe

Error writing to socket: Broken pipe (Errno)
  from /usr/share/crystal/src/thread.cr:69:3 in 'unbuffered_write'
  from /usr/share/crystal/src/openssl/bio.cr:24:7 in '->'
  from BIO_write
  from ???
  from ???
  from /usr/share/crystal/src/openssl/ssl/socket.cr:122:5 in 'unbuffered_write'
  from /usr/share/crystal/src/io/buffered.cr:180:5 in 'send_sync_message'
  from lib/pg/src/pg/statement.cr:18:5 in 'perform_query'
  from lib/db/src/db/statement.cr:103:14 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:9:11 in 'query'
  from lib/onyx-sql/src/onyx-sql/repository/query.cr:23:12 in 'query'

@vladfaust
Copy link
Member Author

vladfaust commented May 30, 2019

Sorry about that. Ideally, a DB connection should handle such errors itself. It should not be the responsibility of an ORM to handle those dying connections.

@vladfaust
Copy link
Member Author

crystal-db has pool implemented, which is claimed to handle connection errors. I suggest you to try raise the problem there, @arubinofaux.

@vladfaust
Copy link
Member Author

Try adding retry_attempts=x&retry_delay=y to your database URL (which is shitty design IMO, as these params being out of scope of URI specification and 12-factor violation).

@vladfaust
Copy link
Member Author

@arubinofaux did you manage to make it work better?

@acidtib
Copy link
Contributor

acidtib commented Jun 19, 2019

@vladfaust we manage to control the issue by constructing the database url like this.

postgres://user:password@host:port/database?sslmode=require&initial_pool_size=32&max_pool_size=32&max_idle_pool_size=0&retry_attemps=5

If you notice all the magic happens in max_idle_pool_size when set to 0 we stopped getting Error writing to socket: Broken pipe

Due to the current state of crystal, i think this is the best fix we can expect without going in and rewriting crystal-pg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Kanban
  
To do
Development

No branches or pull requests

2 participants