Simulating Slow Internet Connection

前端 未结 16 1739
深忆病人
深忆病人 2020-11-27 09:30

I know this is kind of an odd question. Since I usually develop applications based on the \"assumption\" that all users have a slow internet connection. But, does anybody th

16条回答
  •  生来不讨喜
    2020-11-27 10:10

    One common case of shaping a single TCP connection can actually be assembled from dual pairs of socat and cpipe in UNIX fashion like this:

    socat TCP-LISTEN:5555,reuseaddr,reuseport,fork SYSTEM:'cpipe -ngr -b 1 -s 10 | socat - "TCP:localhost:5000" | cpipe -ngr -b 1 -s 300'

    This simulates a connection with bandwidth of approximately 300kB/s from your service at :5000 and to at approximately 10kB/s and listens on :5555 for incoming connections. Caveat: Note that this per-connection, so each individual TCP connection gets this amount.

    Explanation: The outer (left) socat listens with the given options on :5555 as a forking server. The first cpipe command in the SYSTEM:... option then throttles data that went into socket :5555 (and comes out of the first, outer socat) to at most 10kByte/s. That data is then forwarding using another socat which connects to localhost:5000 (where the service you want to slow down should be listening). Data from localhost:5000 is then put into the right cpipe command, which (with the given values) throttles it to about 300kB/s.

    The option -ngr to cpipe is important. It causes cpipe to read non-greedily from its input file-descriptor. Otherwise, you might get stuck with data in the buffers not being forwarded and waiting for a reply.

    Using the more common buffer tool instead of cpipe is likely possible as well.

    (Credits: This is based on the "double-tee" recipe by Christophe Loor from the socat documentation)

提交回复
热议问题