Exemplo n.º 1
0
func TestRateLimiting(_t *testing.T) {
	t := NewTailTest("rate-limiting", _t)
	t.CreateFile("test.txt", "hello\nworld\nagain\nextra\n")
	config := Config{
		Follow:      true,
		RateLimiter: ratelimiter.NewLeakyBucket(2, time.Second)}
	leakybucketFull := "Too much log activity; waiting a second before resuming tailing"
	tail := t.StartTail("test.txt", config)

	// TODO: also verify that tail resumes after the cooloff period.
	go t.VerifyTailOutput(tail, []string{
		"hello", "world", "again",
		leakybucketFull,
		"more", "data",
		leakybucketFull})

	// Add more data only after reasonable delay.
	<-time.After(1200 * time.Millisecond)
	t.AppendFile("test.txt", "more\ndata\n")

	// Delete after a reasonable delay, to give tail sufficient time
	// to read all lines.
	<-time.After(100 * time.Millisecond)
	t.RemoveFile("test.txt")

	// tail.Stop()
	tail.Cleanup()
}
Exemplo n.º 2
0
func (c *Config) GetLeakyBucket() *ratelimiter.LeakyBucket {
	rate := c.MaxLinesPerSecond
	if rate < 1 {
		log.Warnf("max_lines_per_second must be a positive integer; using default")
		rate = 100
	}

	burstSize := c.MaxLinesBurst
	if burstSize < 1 {
		log.Warnf("max_lines_burst must be a positive integer; using default")
		burstSize = 10000
	}

	interval := time.Duration(int64(time.Second) / rate)

	return ratelimiter.NewLeakyBucket(burstSize, interval)
}