Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for non-blocking updates during min-syc-period
- Loading branch information
Timothy St. Clair
committed
Jan 25, 2017
1 parent
f42440c
commit cc45ab9
Showing
5 changed files
with
160 additions
and
24 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,52 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
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 iptables | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBack2BackSync(t *testing.T) { | ||
st := newSyncThrottle(time.Second, time.Second*10) | ||
t1 := st.allowSync() | ||
t2 := st.allowSync() | ||
if !t1 || !t2 { | ||
t.Errorf("Back 2 Back Sync failed") | ||
} | ||
} | ||
|
||
func TestRejectResetAccept(t *testing.T) { | ||
minSync := time.Millisecond * 50 | ||
st := newSyncThrottle(minSync, time.Second) | ||
st.resetTimer() | ||
t1 := st.allowSync() | ||
t2 := st.allowSync() | ||
t3 := st.allowSync() | ||
if !t1 || !t2 || t3 { | ||
t.Errorf("Failed to reject spamming") | ||
} | ||
<-st.timer.C | ||
if st.timeElapsedSinceLastSync() < minSync { | ||
t.Errorf("Failed to wait till elapsed minSync") | ||
} | ||
t4 := st.allowSync() | ||
if !t4 { | ||
t.Errorf("Failed to allow after minSync ellapsed") | ||
} | ||
|
||
} |
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,89 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
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 iptables | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"k8s.io/client-go/util/flowcontrol" | ||
) | ||
|
||
// syncThrottle is a utility wrapper to handle time based | ||
// iptables synchronization. | ||
// The purpose is to rate limit updates to prevent excessive | ||
// iptables synchronizations during endpoint watch updates. | ||
// It is not thread-safe. | ||
type syncThrottle struct { | ||
rl flowcontrol.RateLimiter // rate limiter to prevent accessive iptables sync | ||
timer *time.Timer // timer used to trigger a iptables sync | ||
lastSync time.Time // time since last sync | ||
minSyncPeriod time.Duration // the minimum period allowed between iptables sync e.g. 1 second | ||
syncPeriod time.Duration // default rectification cycle | ||
} | ||
|
||
// creates a new syncThrottle | ||
func newSyncThrottle(minSyncPeriod time.Duration, syncPeriod time.Duration) *syncThrottle { | ||
st := &syncThrottle{ | ||
minSyncPeriod: minSyncPeriod, | ||
syncPeriod: syncPeriod, | ||
} | ||
|
||
if minSyncPeriod != 0 { | ||
// input of minSyncPeriod is a duration typically in seconds, but could be .5 | ||
syncsPerSecond := float32(time.Second) / float32(minSyncPeriod) | ||
// The average use case will process 2 updates in short succession | ||
st.rl = flowcontrol.NewTokenBucketRateLimiter(syncsPerSecond, 2) | ||
} | ||
return st | ||
} | ||
|
||
// allowSync returns true when we are allowed to sync | ||
// based on the minSyncPeriod. When false, the | ||
// timer for the syncPeriod is reset | ||
func (s *syncThrottle) allowSync() bool { | ||
if s.rl != nil { | ||
if s.rl.TryAccept() == false { | ||
duration := s.timeElapsedSinceLastSync() | ||
glog.V(4).Infof("Attempting to synch too often. Duration: %v, min period: %v", duration, s.minSyncPeriod) | ||
s.timer.Reset(s.minSyncPeriod - duration) | ||
return false | ||
} | ||
} | ||
s.lastSync = time.Now() | ||
return true | ||
} | ||
|
||
// timeEllapsedSinceLastSync will return the duration since the last sync | ||
func (s *syncThrottle) timeElapsedSinceLastSync() time.Duration { | ||
return time.Since(s.lastSync) | ||
} | ||
|
||
// resetTimer sets the timer back to the default syncPeriod | ||
func (s *syncThrottle) resetTimer() { | ||
if s.timer == nil { | ||
s.timer = time.NewTimer(s.syncPeriod) | ||
} else { | ||
s.stopTimer() | ||
s.timer.Reset(s.syncPeriod) | ||
} | ||
} | ||
|
||
// stopTimer will stop the currently running timer | ||
func (s *syncThrottle) stopTimer() { | ||
s.timer.Stop() | ||
} |