chore(queue): support option setting (#593)
This commit is contained in:
parent
41c61f11c4
commit
4349a17017
2
main.go
2
main.go
|
@ -316,7 +316,7 @@ func main() {
|
||||||
logx.LogError.Fatal(err)
|
logx.LogError.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w := simple.NewWorker(int(cfg.Core.QueueNum))
|
w := simple.NewWorker(simple.WithQueueNum(int(cfg.Core.QueueNum)))
|
||||||
q := queue.NewQueue(w, int(cfg.Core.WorkerNum))
|
q := queue.NewQueue(w, int(cfg.Core.WorkerNum))
|
||||||
q.Start()
|
q.Start()
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package simple
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/appleboy/gorush/gorush"
|
"github.com/appleboy/gorush/gorush"
|
||||||
"github.com/appleboy/gorush/queue"
|
"github.com/appleboy/gorush/queue"
|
||||||
|
@ -9,6 +10,9 @@ import (
|
||||||
|
|
||||||
var _ queue.Worker = (*Worker)(nil)
|
var _ queue.Worker = (*Worker)(nil)
|
||||||
|
|
||||||
|
// Option for queue system
|
||||||
|
type Option func(*Worker)
|
||||||
|
|
||||||
// Worker for simple queue using channel
|
// Worker for simple queue using channel
|
||||||
type Worker struct {
|
type Worker struct {
|
||||||
queueNotification chan gorush.PushNotification
|
queueNotification chan gorush.PushNotification
|
||||||
|
@ -49,9 +53,24 @@ func (s *Worker) Queue(job interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWorker for struct
|
// WithQueueNum setup the capcity of queue
|
||||||
func NewWorker(num int) *Worker {
|
func WithQueueNum(num int) Option {
|
||||||
return &Worker{
|
return func(w *Worker) {
|
||||||
queueNotification: make(chan gorush.PushNotification, num),
|
w.queueNotification = make(chan gorush.PushNotification, num)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewWorker for struc
|
||||||
|
func NewWorker(opts ...Option) *Worker {
|
||||||
|
w := &Worker{
|
||||||
|
queueNotification: make(chan gorush.PushNotification, runtime.NumCPU()<<1),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop through each option
|
||||||
|
for _, opt := range opts {
|
||||||
|
// Call the option giving the instantiated
|
||||||
|
opt(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
|
@ -46,7 +46,7 @@ func TestMain(m *testing.M) {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w = simple.NewWorker(int(cfg.Core.QueueNum))
|
w = simple.NewWorker()
|
||||||
q = queue.NewQueue(w, 4)
|
q = queue.NewQueue(w, 4)
|
||||||
q.Start()
|
q.Start()
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
Loading…
Reference in New Issue