1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

request-promise KEEPALIVEで接続を維持したい

Posted at

接続にかかる時間を短縮するためや接続しづらさを解消するために接続を維持したまま続けてリクエストをしたい場合がある。

request-promiseはkeepalive接続に対応している
オプションにforeverという項目があるのでこれをtrueにすると接続が維持されるようになる

以下はそのサンプルである

const rp = require('request-promise')
const get = async() => {
    const uri = "http://example.com"
    const opt = {
        uri : uri,
        method: "GET",
        timeout: 10 * 1000,
        forever: true,
    }
    const result = { success: null, error: null, time: 0 }
    const begintime = process.uptime()
    try{
        result.success = await rp(opt)
    }catch(e){
        result.error = e
    }
    const endtime = process.uptime()
    result.time = endtime - begintime
    return result
}

const main = async() => {
    for(let i=0; i<5; ++i) await get().then(console.log)
}

main()
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?