Skip to content

Commit 4c33c00

Browse files
committed
refactor: move factory to its own file
1 parent 3c1dca2 commit 4c33c00

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

dependency.go

+1-35
Original file line numberDiff line numberDiff line change
@@ -43,46 +43,12 @@ func Providers() di.Deps {
4343
// Gauge is an alias used for dependency injection
4444
type Gauge metrics.Gauge
4545

46-
// ConsumableDispatcher is the key JobFrom *Queue in the dependencies graph. Used as a type hint for injection.
46+
// ConsumableDispatcher is the key of *Queue in the dependencies graph. Used as a type hint for injection.
4747
type ConsumableDispatcher interface {
4848
JobDispatcher
4949
Consume(ctx context.Context) error
5050
}
5151

52-
// DispatcherFactory is a factory for *Queue. Note DispatcherFactory doesn't contain the factory method
53-
// itself. ie. How to factory a dispatcher left there for users to define. Users then can use this type to create
54-
// their own dispatcher implementation.
55-
//
56-
// Here is an example on how to create a custom DispatcherFactory with an InProcessDriver.
57-
//
58-
// factory := di.NewFactory(func(name string) (di.Pair, error) {
59-
// queuedDispatcher := queue.NewQueue(
60-
// &Jobs.SyncDispatcher{},
61-
// queue.NewInProcessDriver(),
62-
// )
63-
// return di.Pair{Conn: queuedDispatcher}, nil
64-
// })
65-
// dispatcherFactory := DispatcherFactory{Factory: factory}
66-
//
67-
type DispatcherFactory struct {
68-
*di.Factory
69-
}
70-
71-
// Make returns a Queue by the given name. If it has already been created under the same name,
72-
// the that one will be returned.
73-
func (s DispatcherFactory) Make(name string) (*Queue, error) {
74-
client, err := s.Factory.Make(name)
75-
if err != nil {
76-
return nil, err
77-
}
78-
return client.(*Queue), nil
79-
}
80-
81-
// DispatcherMaker is the key JobFrom *DispatcherFactory in the dependencies graph. Used as a type hint for injection.
82-
type DispatcherMaker interface {
83-
Make(string) (*Queue, error)
84-
}
85-
8652
type configuration struct {
8753
RedisName string `yaml:"redisName" json:"redisName"`
8854
Parallelism int `yaml:"parallelism" json:"parallelism"`

factory.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package queue
2+
3+
import "github.com/DoNewsCode/core/di"
4+
5+
// DispatcherFactory is a factory for *Queue. Note DispatcherFactory doesn't contain the factory method
6+
// itself. ie. How to factory a dispatcher left there for users to define. Users then can use this type to create
7+
// their own dispatcher implementation.
8+
//
9+
// Here is an example on how to create a custom DispatcherFactory with an InProcessDriver.
10+
//
11+
// factory := di.NewFactory(func(name string) (di.Pair, error) {
12+
// queuedDispatcher := queue.NewQueue(
13+
// &Jobs.SyncDispatcher{},
14+
// queue.NewInProcessDriver(),
15+
// )
16+
// return di.Pair{Conn: queuedDispatcher}, nil
17+
// })
18+
// dispatcherFactory := DispatcherFactory{Factory: factory}
19+
//
20+
type DispatcherFactory struct {
21+
*di.Factory
22+
}
23+
24+
// Make returns a Queue by the given name. If it has already been created under the same name,
25+
// the that one will be returned.
26+
func (s DispatcherFactory) Make(name string) (*Queue, error) {
27+
client, err := s.Factory.Make(name)
28+
if err != nil {
29+
return nil, err
30+
}
31+
return client.(*Queue), nil
32+
}
33+
34+
// DispatcherMaker is the key of *DispatcherFactory in the dependencies graph. Used as a type hint for injection.
35+
type DispatcherMaker interface {
36+
Make(string) (*Queue, error)
37+
}

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,6 @@ github.com/klauspost/compress v1.9.8/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0
458458
github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
459459
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
460460
github.com/knadh/koanf v0.15.0/go.mod h1:Ut3d4JaTRZYfO5a0wdYIGE+oyGaGFo4vXQ3ZvaSWxNc=
461-
github.com/knadh/koanf v1.2.0 h1:fANR65nD9cI3HGm0s3pmzqh4hkknt4TasmZzh+rLy1M=
462-
github.com/knadh/koanf v1.2.0/go.mod h1:xpPTwMhsA/aaQLAilyCCqfpEiY1gpa160AiCuWHJUjY=
463461
github.com/knadh/koanf v1.2.1 h1:tVR+BbAM5PA2YkB0OMyfSnEsmt3uygpn3R0WB6jKw7s=
464462
github.com/knadh/koanf v1.2.1/go.mod h1:xpPTwMhsA/aaQLAilyCCqfpEiY1gpa160AiCuWHJUjY=
465463
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=

0 commit comments

Comments
 (0)