Skip to content

Commit af85f07

Browse files
committed
chore(options): Add default options
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 8cbabf0 commit af85f07

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

nsq.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package nsq
33
import (
44
"context"
55
"encoding/json"
6-
"runtime"
76
"sync"
87
"sync/atomic"
98
"time"
@@ -42,27 +41,8 @@ func (w *Worker) BusyWorkers() uint64 {
4241

4342
// NewWorker for struc
4443
func NewWorker(opts ...Option) *Worker {
45-
defaultOpts := options{
46-
addr: "127.0.0.1:4150",
47-
topic: "gorush",
48-
channel: "ch",
49-
maxInFlight: runtime.NumCPU(),
50-
51-
logger: queue.NewLogger(),
52-
runFunc: func(context.Context, queue.QueuedMessage) error {
53-
return nil
54-
},
55-
metric: queue.NewMetric(),
56-
}
57-
58-
// Loop through each option
59-
for _, opt := range opts {
60-
// Call the option giving the instantiated
61-
opt(&defaultOpts)
62-
}
63-
6444
w := &Worker{
65-
opts: defaultOpts,
45+
opts: newOptions(opts...),
6646
stop: make(chan struct{}),
6747
}
6848

options.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nsq
22

33
import (
44
"context"
5+
"runtime"
56

67
"github.com/golang-queue/queue"
78
)
@@ -74,3 +75,26 @@ func withDisable() Option {
7475
w.disable = true
7576
}
7677
}
78+
79+
func newOptions(opts ...Option) options {
80+
defaultOpts := options{
81+
addr: "127.0.0.1:4150",
82+
topic: "gorush",
83+
channel: "ch",
84+
maxInFlight: runtime.NumCPU(),
85+
86+
logger: queue.NewLogger(),
87+
runFunc: func(context.Context, queue.QueuedMessage) error {
88+
return nil
89+
},
90+
metric: queue.NewMetric(),
91+
}
92+
93+
// Loop through each option
94+
for _, opt := range opts {
95+
// Call the option giving the instantiated
96+
opt(&defaultOpts)
97+
}
98+
99+
return defaultOpts
100+
}

0 commit comments

Comments
 (0)