Simple send rate limit

This commit is contained in:
Jakob Borg
2014-01-12 08:59:35 -07:00
parent b601fc5627
commit 55f61ccb5e
2 changed files with 27 additions and 0 deletions

View File

@@ -53,6 +53,8 @@ type Model struct {
fileLastChanged map[string]time.Time
fileWasSuppressed map[string]int
limitRequestRate chan struct{}
}
type Connection interface {
@@ -97,6 +99,21 @@ func NewModel(dir string) *Model {
return m
}
func (m *Model) LimitRate(kbps int) {
m.limitRequestRate = make(chan struct{}, kbps)
n := kbps/10 + 1
go func() {
for {
time.Sleep(100 * time.Millisecond)
for i := 0; i < n; i++ {
select {
case m.limitRequestRate <- struct{}{}:
}
}
}
}()
}
// Trace enables trace logging of the given facility. This is a debugging function; grep for m.trace.
func (m *Model) Trace(t string) {
m.Lock()
@@ -334,6 +351,12 @@ func (m *Model) Request(nodeID, name string, offset uint64, size uint32, hash []
return nil, err
}
if m.limitRequestRate != nil {
for s := 0; s < len(buf); s += 1024 {
<-m.limitRequestRate
}
}
return buf, nil
}