pybitflyer icon indicating copy to clipboard operation
pybitflyer copied to clipboard

Add an option for HTTP Keep-Alive

Open hrtshu opened this issue 7 years ago • 2 comments

To enable HTTP keep-alive over multiple requests, I would like to propose this pull request (including some minor changes).

hrtshu avatar Nov 29 '17 22:11 hrtshu

Thank you for your pull req. I have to consider carefully whether merge of not, because all pybitflyer users will have to change their codes. Please give me a little time to think about this.

yagays avatar Dec 02 '17 00:12 yagays

I have been using this PR for about 3 months, but it does not interfere.

HTTP keep-alive over multiple requests can be confirmed with the following code.

import pybitflyer
import logging
import time

KEEP_SESSION=True

logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s")
api = pybitflyer.API(keep_session=KEEP_SESSION)
while True:
    api.ticker()
    time.sleep(1)

KEEP_SESSION=False

2018-04-02 08:40:53,441 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:53,531 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:54,541 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:54,626 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:55,635 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:55,732 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:56,740 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:56,829 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:40:57,837 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:40:57,933 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None

KEEP_SESSION=True

2018-04-02 08:41:17,315 Starting new HTTPS connection (1): api.bitflyer.jp
2018-04-02 08:41:17,400 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:18,432 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:19,466 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:20,507 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:21,537 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:22,572 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:23,602 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:24,632 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:25,661 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:41:26,691 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None

In this case, the HTTP connection is reset every 100 seconds. It is probably due to HTTP server configuration of Cloudflare(CDN of API server).

2018-04-02 08:38:29,996 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:38:31,004 Resetting dropped connection: api.bitflyer.jp
2018-04-02 08:38:31,094 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None
2018-04-02 08:38:32,133 https://api.bitflyer.jp:443 "GET /v1/ticker HTTP/1.1" 200 None

nagadomi avatar Apr 01 '18 23:04 nagadomi