Просмотр исходного кода

Rename Jitter function and parameter for clarity

Rod Hynes 9 лет назад
Родитель
Сommit
8f0a7bb87d
3 измененных файлов с 15 добавлено и 16 удалено
  1. 9 10
      psiphon/common/utils.go
  2. 3 3
      psiphon/common/utils_test.go
  3. 3 3
      psiphon/meekConn.go

+ 9 - 10
psiphon/common/utils.go

@@ -135,20 +135,19 @@ func MakeRandomStringBase64(byteLength int) (string, error) {
 	return base64.RawURLEncoding.EncodeToString(bytes), nil
 }
 
-// JitterPercentage returns n +/- the given percentage.
-// For example, for n = 100 and p = 0.1, the return value
-// will be in the range [90, 110].
-func JitterPercentage(n int64, percentage float64) int64 {
-	a := int64(math.Ceil(float64(n) * percentage))
+// Jitter returns n +/- the given factor.
+// For example, for n = 100 and factor = 0.1, the
+// return value will be in the range [90, 110].
+func Jitter(n int64, factor float64) int64 {
+	a := int64(math.Ceil(float64(n) * factor))
 	r, _ := MakeSecureRandomInt64(2*a + 1)
 	return n + r - a
 }
 
-// JitterDurationPercentage is a helper function that
-// wraps JitterPercentage.
-func JitterDurationPercentage(
-	d time.Duration, percentage float64) time.Duration {
-	return time.Duration(JitterPercentage(int64(d), percentage))
+// JitterDuration is a helper function that wraps Jitter.
+func JitterDuration(
+	d time.Duration, factor float64) time.Duration {
+	return time.Duration(Jitter(int64(d), factor))
 }
 
 // GetCurrentTimestamp returns the current time in UTC as

+ 3 - 3
psiphon/common/utils_test.go

@@ -56,11 +56,11 @@ func TestMakeRandomPeriod(t *testing.T) {
 	}
 }
 
-func TestJitterPercentage(t *testing.T) {
+func TestJitter(t *testing.T) {
 
 	testCases := []struct {
 		n           int64
-		p           float64
+		factor      float64
 		expectedMin int64
 		expectedMax int64
 	}{
@@ -76,7 +76,7 @@ func TestJitterPercentage(t *testing.T) {
 
 			for i := 0; i < 100000; i++ {
 
-				x := JitterPercentage(testCase.n, testCase.p)
+				x := Jitter(testCase.n, testCase.factor)
 				if x < min {
 					min = x
 				}

+ 3 - 3
psiphon/meekConn.go

@@ -533,20 +533,20 @@ func (meek *MeekConn) relay() {
 
 		} else if interval == 0 {
 
-			interval = common.JitterDurationPercentage(
+			interval = common.JitterDuration(
 				MIN_POLL_INTERVAL,
 				MIN_POLL_INTERVAL_JITTER)
 
 		} else {
 
 			if common.FlipCoin() {
-				interval = common.JitterDurationPercentage(
+				interval = common.JitterDuration(
 					time.Duration(float64(interval)*POLL_INTERVAL_MULTIPLIER),
 					POLL_INTERVAL_JITTER)
 			}
 
 			if interval >= MAX_POLL_INTERVAL {
-				interval = common.JitterDurationPercentage(
+				interval = common.JitterDuration(
 					MAX_POLL_INTERVAL,
 					MAX_POLL_INTERVAL_JITTER)
 			}