Skip to content

feat: add evictor #16839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions pkg/limits/evict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package limits

import (
"context"
"time"

"github.com/coder/quartz"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
)

type Evictable interface {
Evict(context.Context) error
}

// Evictor runs scheduled evictions.
type Evictor struct {
ctx context.Context
interval time.Duration
target Evictable
logger log.Logger

// Used for tests.
clock quartz.Clock
}

// NewEvictor returns a new evictor over the interval.
func NewEvictor(ctx context.Context, interval time.Duration, target Evictable, logger log.Logger) (*Evictor, error) {
return &Evictor{
ctx: ctx,
interval: interval,
target: target,
logger: logger,
clock: quartz.NewReal(),
}, nil
}

// Runs the scheduler loop until the context is canceled.
func (e *Evictor) Run() error {
t := e.clock.TickerFunc(e.ctx, e.interval, e.doTick)
return t.Wait()
}

func (e *Evictor) doTick() error {
ctx, cancel := context.WithTimeout(e.ctx, e.interval)
defer cancel()
if err := e.target.Evict(ctx); err != nil {
level.Warn(e.logger).Log("failed to run eviction", "err", err.Error())
}
return nil
}
54 changes: 54 additions & 0 deletions pkg/limits/evict_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package limits

import (
"context"
"testing"
"time"

"github.com/coder/quartz"
"github.com/go-kit/log"
"github.com/stretchr/testify/require"
)

type mockEvictable struct {
calls []time.Time
clock quartz.Clock
}

func (m *mockEvictable) Evict(_ context.Context) error {
m.calls = append(m.calls, m.clock.Now())
return nil
}

func TestEvictor(t *testing.T) {
// Set a timeout on the test.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

clock := quartz.NewMock(t)
m := mockEvictable{clock: clock}
e, err := NewEvictor(ctx, time.Second, &m, log.NewNopLogger())
require.NoError(t, err)
e.clock = clock

// See https://github.com/coder/quartz/blob/v0.1.3/example_test.go#L48.
trap := clock.Trap().TickerFunc()
defer trap.Close()
go e.Run() //nolint:errcheck
call, err := trap.Wait(ctx)
require.NoError(t, err)
call.Release()

// Do a tick.
clock.Advance(time.Second).MustWait(ctx)
tick1 := clock.Now()
require.Len(t, m.calls, 1)
require.Equal(t, tick1, m.calls[0])

// Do another tick.
clock.Advance(time.Second).MustWait(ctx)
tick2 := clock.Now()
require.Len(t, m.calls, 2)
require.Equal(t, tick1, m.calls[0])
require.Equal(t, tick2, m.calls[1])
}