diff --git a/lib/pgsync/client.rb b/lib/pgsync/client.rb index 56997f9c..dca6019d 100644 --- a/lib/pgsync/client.rb +++ b/lib/pgsync/client.rb @@ -62,6 +62,7 @@ def slop_options o.boolean "--defer-constraints-v2", "defer constraints", default: false o.boolean "--disable-integrity", "disable foreign key triggers", default: false o.integer "-j", "--jobs", "number of tables to sync at a time" + o.integer "--throttle-bytes-per-second", "limit the sync to roughly this many bytes per second" # replaced by v2 o.boolean "--defer-constraints", "defer constraints", default: false, help: false diff --git a/lib/pgsync/task.rb b/lib/pgsync/task.rb index 4776d48c..bcd5157e 100644 --- a/lib/pgsync/task.rb +++ b/lib/pgsync/task.rb @@ -213,11 +213,17 @@ def copy(source_command, dest_table:, dest_fields:) source.log_sql(source_command) destination.log_sql(destination_command) + bytes_count = 0 + start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) destination.conn.copy_data(destination_command) do source.conn.copy_data(source_command) do while (row = source.conn.get_copy_data) + while opts[:throttle_bytes_per_second] && (bytes_count / (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time)) > opts[:throttle_bytes_per_second] + sleep(0.01) + end destination.conn.put_copy_data(row) + bytes_count += row.bytesize end end end