Skip to content

Commit

Permalink
feat(任务): 增加全局限制条件
Browse files Browse the repository at this point in the history
  • Loading branch information
storezhang committed Apr 6, 2023
1 parent 2212702 commit a8b798c
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
8 changes: 7 additions & 1 deletion add_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,16 @@ func (ab *addBuilder) Random() (builder *randomBuilder) {
return
}

func (ab *addBuilder) Limit() *limitBuilder {
func (ab *addBuilder) Limit() *limitBuilder[*addBuilder] {
return newLimitBuilder(ab)
}

func (ab *addBuilder) Unlimited() *addBuilder {
ab.self.limit = newUnlimitedParams()

return ab
}

func (ab *addBuilder) Unique() *addBuilder {
ab.self.unique = true

Expand Down
2 changes: 2 additions & 0 deletions add_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func newAddParams(scheduler *Scheduler, worker worker) (add *addParams) {
func (ap *addParams) checkLimit(scheduler *Scheduler) (err error) {
if nil != ap.limit {
err = ap.limit.check(scheduler)
} else {
err = scheduler.checkLimit()
}

return
Expand Down
4 changes: 4 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (b *builder) Unique() *builder {
return b
}

func (b *builder) Limit() *limitBuilder[*builder] {
return newLimitBuilder(b)
}

func (b *builder) Build() *Scheduler {
return newScheduler(b.params)
}
27 changes: 16 additions & 11 deletions limit_builder.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
package schedule

type limitBuilder struct {
type limitBuilder[T limitType] struct {
params *limitParams
builder *addBuilder
builder T
}

func newLimitBuilder(builder *addBuilder) *limitBuilder {
return &limitBuilder{
func newLimitBuilder[T limitType](builder T) *limitBuilder[T] {
return &limitBuilder[T]{
params: newLimitParams(),
builder: builder,
}
}

func (lb *limitBuilder) Cpu(percent float64) *limitBuilder {
func (lb *limitBuilder[T]) Cpu(percent float64) *limitBuilder[T] {
lb.params.cpu = percent

return lb
}

func (lb *limitBuilder) Memory(percent float64) *limitBuilder {
func (lb *limitBuilder[T]) Memory(percent float64) *limitBuilder[T] {
lb.params.memory = percent

return lb
}

func (lb *limitBuilder) Process(count int) *limitBuilder {
func (lb *limitBuilder[T]) Process(count int) *limitBuilder[T] {
lb.params.process = count

return lb
}

func (lb *limitBuilder) Max(max int) *limitBuilder {
func (lb *limitBuilder[T]) Max(max int) *limitBuilder[T] {
lb.params.max = max

return lb
}

func (lb *limitBuilder) Build() (builder *addBuilder) {
lb.builder.self.limit = lb.params
builder = lb.builder
func (lb *limitBuilder[T]) Build() (t T) {
switch target := any(lb.builder).(type) {
case *builder:
target.params.limit = lb.params
case *addBuilder:
target.self.limit = lb.params
}
t = lb.builder

return
}
9 changes: 9 additions & 0 deletions limit_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ func newLimitParams() *limitParams {
}
}

func newUnlimitedParams() *limitParams {
return &limitParams{
cpu: math.MaxFloat64,
memory: math.MaxFloat64,
process: math.MaxInt,
max: math.MaxInt,
}
}

func (lp *limitParams) check(scheduler *Scheduler) (err error) {
if ce := lp.checkCount(scheduler); nil != ce {
err = ce
Expand Down
5 changes: 5 additions & 0 deletions limit_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package schedule

type limitType interface {
*builder | *addBuilder
}
1 change: 1 addition & 0 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
type params struct {
logger simaqian.Logger
unique bool
limit *limitParams
}

func newParams() *params {
Expand Down
8 changes: 8 additions & 0 deletions scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ func (s *Scheduler) remove(id string) {
s.ids.Delete(id)
}
}

func (s *Scheduler) checkLimit() (err error) {
if nil != s.params.limit {
err = s.params.limit.check(s)
}

return
}

0 comments on commit a8b798c

Please sign in to comment.