npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mysql2-timeout-additions

v1.1.2

Published

Provides timeout functionality for the mysql2 module

Downloads

93

Readme

mysql2-timeout-additions

Provides timeout functionality for the mysql2 module (see https://github.com/sidorares/node-mysql2), applied for both queries and connection acquisition.

npm Node.js Version npm

About

Many users of the mysql2 module find themselves having to implement custom timeout handlers for their queries. This involves cleaning up the connection when the timeout has been exceeded, writing a routine to kill the database query itself when the timeout is exceeded (which does not happen automatically as a result of closing the connection), and handling tcp errors on the client side.

It has been a heavily requested feature for the mysql2 project, but the maintainers have decided that timeout functionality is beyond the scope of the database driver as per https://github.com/sidorares/node-mysql2/issues/185.

This module allows you to specify a timeout parameter for promise pools created by mysql2 which will enhance all subsequent invocations to pool.query and connection.query to throw an error when the timeout threshold has been exceeded. This module will also seamlessly handle terminating the associated database queries and handle tcp connection errors on the client side in a way that ensures database queries are cleaned up after the client connection is terminated. It will also throw the timeout error if fetching a new database connection takes longer than the specified threshold, even if no query has been made yet.

Install

$ npm install mysql2-timeout-additions

Usage

const mysql = require('mysql2');
const mysql2Timeout = require('mysql2-timeout-additions');
const MAX_QUERY_EXECUTION_TIME_SECONDS = 5;

const pool = mysql.createPool({
    connectionLimit : 10,
    host            : "myHost",
    user            : "myUser",
    password    : "myPassword",
    database     : "myDatabase"
});
const promisePool = pool.promise();

mysql2Timeout.addTimeoutToPromisePool({ 
    pool: promisePool, 
    seconds: MAX_QUERY_EXECUTION_TIME_SECONDS 
});

Now whenever a query takes longer than the specified number of seconds to execute, it will throw an error message that contains the offending query as a substring. Timeout functionality is added seamlessly to any invocations to pool.query as well as any invocations to connection.query on any connections acquired through pool.getConnection. The same constraints are applied to pool.getConnection itself so that connections must be returned within the time bounds specified or an error will be thrown.

This module handles both cleaning up the connection object and returning it to the pool whenever a connection runs overtime, as well as killing the database query itself. It also handles killing the database query when the tcp socket on the client side fails by listening for the PROTOCOL_SEQUENCE_TIMEOUT error.

Requirements

Requires node >= 7.6

Limitations

Right now this module only supports adding timeout functionality to promise pools created by mysql2.

Todo

  • Add timeout functions for mysql2 objects besides promise pools.
  • Add tests.