-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to configure max-rate for pg_basebackup
Signed-off-by: Aswin Karthik <[email protected]>
- Loading branch information
1 parent
a27bcae
commit 6ede9b8
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2016 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
// Bytesize is useful to represent bytes and perform operations on it | ||
type Bytesize int | ||
|
||
// Common bytesizes than can be used | ||
const ( | ||
Byte Bytesize = 1 | ||
Kilobyte = 1024 * Byte | ||
Megabyte = 1024 * Kilobyte | ||
) | ||
|
||
const pattern = `([0-9]+)(k|M)` | ||
|
||
// ParseBytesize allows to parse the given input into Bytesize | ||
// Allowed formats is [:digit:] suffixed by k (kilobyte) or M (Megabyte) | ||
func ParseBytesize(input string) (Bytesize, error) { | ||
re := regexp.MustCompile(pattern) | ||
allMatches := re.FindStringSubmatch(input) | ||
if len(allMatches) != 3 { | ||
return 0, fmt.Errorf("unable to parse %s: must be of the format %s", input, pattern) | ||
} | ||
|
||
size, err := strconv.Atoi(allMatches[1]) | ||
if err != nil { | ||
return 0, fmt.Errorf("unable to parse as integer %s: %v", allMatches[1], err) | ||
} | ||
|
||
unit := allMatches[2] | ||
switch unit { | ||
case "k": | ||
return Bytesize(size) * Kilobyte, nil | ||
case "M": | ||
return Bytesize(size) * Megabyte, nil | ||
default: | ||
return 0, fmt.Errorf("%s can either be suffixed with k or M", allMatches[2]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2016 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
import "testing" | ||
|
||
func TestParseBytesize(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected Bytesize | ||
shouldError bool | ||
}{ | ||
{"10", 0, true}, | ||
{"10k", 10 * Kilobyte, false}, | ||
{"32k", 32 * Kilobyte, false}, | ||
{"10M", 10 * Megabyte, false}, | ||
{"fM", 0, true}, | ||
{"", 0, true}, | ||
} | ||
|
||
for i, tt := range tests { | ||
actual, err := ParseBytesize(tt.input) | ||
|
||
if tt.shouldError && (err == nil) { | ||
t.Errorf("#%d: expected error for: %s, actual no error", i, tt.input) | ||
} | ||
|
||
if !tt.shouldError && (err != nil) { | ||
t.Errorf("#%d: unexpected error for: %s, error: %v", i, tt.input, err) | ||
} | ||
|
||
if actual != tt.expected { | ||
t.Errorf("#%d: unexpected value. got: %d, expected: %d", i, actual, tt.expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters