-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.go
More file actions
39 lines (33 loc) · 873 Bytes
/
Copy pathscheduler.go
File metadata and controls
39 lines (33 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package browserpm
import "sync/atomic"
// Scheduler picks the next poolPage from a set of available pages.
// Implementations must be safe for concurrent use.
type Scheduler interface {
Select(pages []*poolPage) *poolPage
Reset()
}
// RoundRobinScheduler distributes requests evenly across pages.
type RoundRobinScheduler struct {
counter atomic.Uint64
}
func (s *RoundRobinScheduler) Select(pages []*poolPage) *poolPage {
n := uint64(len(pages))
if n == 0 {
return nil
}
idx := s.counter.Add(1) - 1
return pages[idx%n]
}
func (s *RoundRobinScheduler) Reset() {
s.counter.Store(0)
}
// NewScheduler creates a Scheduler by strategy name.
// Unrecognised strategies fall back to round-robin.
func NewScheduler(strategy string) Scheduler {
switch strategy {
case "round-robin":
return &RoundRobinScheduler{}
default:
return &RoundRobinScheduler{}
}
}