Skip to content

Commit 3db73d5

Browse files
committed
Last revive fixes
See: https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#exported As an aside we really should make sure we always return exported types. UNexported are really terrible to work with: you can not switch type/type assert them - this is good for things like if we are very anal about interface breakage, but in the grand scheme of things it makes dealing with some code pretty painful at times. Signed-off-by: Milos Gajdos <[email protected]>
1 parent b93b89e commit 3db73d5

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ linters:
1212
- bodyclose
1313
- tparallel
1414
- errcheck
15-
#- revive
15+
- revive
1616

1717
issues:
1818
exlude-dirs:

api/pkg/scheduler/allocator.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@ type WorkloadAllocator interface {
2929
// TimeoutFunc defines a function type that determines if a runner has timed out based on the last activity.
3030
type TimeoutFunc func(runnerID string, lastActivityTime time.Time) bool
3131

32-
// allocator implements the WorkloadAllocator interface, managing runners, slots, and workload allocation.
33-
type allocator struct {
32+
// Allocator implements the WorkloadAllocator interface, managing runners, slots, and workload allocation.
33+
type Allocator struct {
3434
slots *xsync.MapOf[uuid.UUID, *Slot] // Maps slot ID to Slot details.
3535
modelStaleFunc TimeoutFunc // Function to check if models are stale
3636
slotTimeoutFunc TimeoutFunc // Function to check if slots have timed out due to error
3737
}
3838

39+
var _ WorkloadAllocator = &Allocator{}
40+
3941
// NewWorkloadAllocator creates a new allocator instance with timeout functions for models and runners.
40-
func NewWorkloadAllocator(staleFunc TimeoutFunc, slotTimeoutFunc TimeoutFunc) *allocator {
41-
return &allocator{
42+
func NewWorkloadAllocator(staleFunc TimeoutFunc, slotTimeoutFunc TimeoutFunc) *Allocator {
43+
return &Allocator{
4244
slots: xsync.NewMapOf[uuid.UUID, *Slot](),
4345
modelStaleFunc: staleFunc,
4446
slotTimeoutFunc: slotTimeoutFunc,
4547
}
4648
}
4749

4850
// AllocateSlot assigns a workload to a specific slot, validating the model and slot before scheduling.
49-
func (a *allocator) AllocateSlot(slotID uuid.UUID, req *Workload) error {
51+
func (a *Allocator) AllocateSlot(slotID uuid.UUID, req *Workload) error {
5052
// Validate model
5153
if _, err := model.GetModel(req.ModelName().String()); err != nil {
5254
return fmt.Errorf("unable to get model (%s): %v", req.ModelName(), err)
@@ -81,7 +83,7 @@ func (a *allocator) AllocateSlot(slotID uuid.UUID, req *Workload) error {
8183
}
8284

8385
// AllocateNewSlot creates a new slot for a workload and allocates it to the best available runner.
84-
func (a *allocator) AllocateNewSlot(runnerID string, req *Workload) (*Slot, error) {
86+
func (a *Allocator) AllocateNewSlot(runnerID string, req *Workload) (*Slot, error) {
8587
// Create a new slot and schedule the workload.
8688
slot := NewSlot(runnerID, req, a.modelStaleFunc, a.slotTimeoutFunc)
8789
log.Trace().
@@ -100,7 +102,7 @@ func (a *allocator) AllocateNewSlot(runnerID string, req *Workload) (*Slot, erro
100102
}
101103

102104
// ReleaseSlot frees the resources allocated to a specific slot.
103-
func (a *allocator) ReleaseSlot(slotID uuid.UUID) error {
105+
func (a *Allocator) ReleaseSlot(slotID uuid.UUID) error {
104106
// Find the slot.
105107
slot, ok := a.slots.Load(slotID)
106108
if !ok {
@@ -121,7 +123,7 @@ func (a *allocator) ReleaseSlot(slotID uuid.UUID) error {
121123
}
122124

123125
// ReconcileSlots updates the state of a runner and reconciles its slots with the allocator's records.
124-
func (a *allocator) ReconcileSlots(props *types.RunnerState) error {
126+
func (a *Allocator) ReconcileSlots(props *types.RunnerState) error {
125127
// Log runner state update.
126128
l := log.With().
127129
Str("runner_id", props.ID).
@@ -216,7 +218,7 @@ func (a *allocator) ReconcileSlots(props *types.RunnerState) error {
216218
}
217219

218220
// WarmSlots returns a list of available slots with warm models waiting for work.
219-
func (a *allocator) WarmSlots(req *Workload) []*Slot {
221+
func (a *Allocator) WarmSlots(req *Workload) []*Slot {
220222
cosyWarm := make([]*Slot, 0, a.slots.Size())
221223

222224
a.slots.Range(func(id uuid.UUID, slot *Slot) bool {
@@ -268,7 +270,7 @@ func (a *allocator) WarmSlots(req *Workload) []*Slot {
268270
}
269271

270272
// RunnerSlots returns all slots associated with a specific runner ID.
271-
func (a *allocator) RunnerSlots(id string) []*Slot {
273+
func (a *Allocator) RunnerSlots(id string) []*Slot {
272274
allSlots := Values(a.slots)
273275
// Filter slots to include only those belonging to the specified runner.
274276
return Filter(allSlots, func(s *Slot) bool {
@@ -278,7 +280,7 @@ func (a *allocator) RunnerSlots(id string) []*Slot {
278280

279281
// DeadSlots checks for any runners that have timed out and removes them.
280282
// It returns the slots associated with the dead runners.
281-
func (a *allocator) DeadSlots(deadRunnerIDs []string) []*Slot {
283+
func (a *Allocator) DeadSlots(deadRunnerIDs []string) []*Slot {
282284
deadSlots := make([]*Slot, 0)
283285
// Iterate through runners to check if any have timed out.
284286
for _, runnerID := range deadRunnerIDs {
@@ -303,7 +305,7 @@ func (a *allocator) DeadSlots(deadRunnerIDs []string) []*Slot {
303305
}
304306

305307
// StartSlot marks scheduled work as in progress
306-
func (a *allocator) StartSlot(slotID uuid.UUID) error {
308+
func (a *Allocator) StartSlot(slotID uuid.UUID) error {
307309
// Find the slot.
308310
slot, ok := a.slots.Load(slotID)
309311
if !ok {
@@ -326,6 +328,6 @@ func (a *allocator) StartSlot(slotID uuid.UUID) error {
326328
return nil
327329
}
328330

329-
func (a *allocator) DeleteSlot(slotID uuid.UUID) {
331+
func (a *Allocator) DeleteSlot(slotID uuid.UUID) {
330332
a.slots.Delete(slotID)
331333
}

api/pkg/scheduler/cluster.go

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ type cluster struct {
2020
runnerTimeoutFunc TimeoutFunc // Function to check if runners have timed out.
2121
}
2222

23+
var _ Cluster = &cluster{}
24+
25+
// NOTE(milosgajdos): we really should make sure we return exported types.
26+
// If we want the type fields to be inaccessible we should make them unexported.
27+
// nolint:revive
2328
func NewCluster(runnerTimeoutFunc TimeoutFunc) *cluster {
2429
return &cluster{
2530
runners: xsync.NewMapOf[string, *runner](),

api/pkg/scheduler/scheduler.go

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ var _ Scheduler = &scheduler{}
5050

5151
// NewScheduler creates a new scheduler with a workload allocator.
5252
// This also starts a goroutine to process the queue in the background.
53+
//
54+
// NOTE(milosgajdos): we really should make sure we return exported types.
55+
// If we want the type fields to be inaccessible we should make them unexported.
56+
// nolint:revive
5357
func NewScheduler(ctx context.Context, cfg *config.ServerConfig, onSchedulingErr func(work *Workload, err error)) *scheduler {
5458
scheduler := newSchedulerWithoutGoroutines(cfg, onSchedulingErr)
5559

0 commit comments

Comments
 (0)