GithubHelp home page GithubHelp logo

isabella232 / pq-timeouts Goto Github PK

View Code? Open in Web Editor NEW

This project forked from datadog/pq-timeouts

0.0 0.0 0.0 16 KB

Postgres driver for Go that wraps lib/pq to provide read and write timeouts.

License: MIT License

Go 100.00%

pq-timeouts's Introduction

pq-timeouts

A Postgres driver for Go that wraps lib/pq to provide read and write timeouts.

Build Status Coverage Status

Why?

lib/pq is an excellent Postgres driver written in pure Go, but it only offers support for the default Postgres timeouts of connect_timeout and statement_timeout. These work well, but in a high availability situation, they might not be enough. statement_timeout only works if the connection to Postgres is alive and well. connect_timeout only provides a timeout during initial connection. Once the connection is in the pool, connect_timeout doesn't apply. If the database goes down, or the network dies, the open connections will hang. Without a read or write timeout on the connection, there is no way to recover quickly. pq-timeouts provides a way to add a timeout to every write and read to and from the database.

Install

go get github.com/Kount/pq-timeouts

Using pq-timeouts

pq-timeouts adds two new connection string parameters: read_timeout and write_timeout. Otherwise, usage is nearly the same as lib/pq:

import (
  "database/sql"

  _ "github.com/Kount/pq-timeouts"
}

func main() {
  // Note: read_timeout and write_timeout are specified in milliseconds.
  db, err := sql.Open(
    "pq-timeouts",
    "user=pqtest dbname=pqtest read_timeout=500 write_timeout=1000 sslmode=verify-full"
  )
  if err != nil {
    log.Fatal(err)
  }

  age := 21
  rows, err := db.Query("SELECT name FROM users WHERE age =$1", age)
  ...
}

Connections using a URL work as well:

  db,err := sql.Open("pq-timeouts", "postgres://pqtest:password@localhost/pqtest?read_timeout=500&write_timeout=1000")

read_timeout and write_timeout are specified in milliseconds. If read_timeout or write_timeout are not specified or set to 0, no timeout is set and the driver behaves as standard lib/pq. For other connection options, check out the documentation for lib/pq: https://godoc.org/github.com/lib/pq

pq-timeouts's People

Contributors

nockty avatar pneisen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.