Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
to fit the style of configuration
- NewOption -> DefaultOption
- Set -> Get
  • Loading branch information
mission-liao committed Dec 29, 2015
1 parent c06e66d commit 474632f
Show file tree
Hide file tree
Showing 20 changed files with 77 additions and 33 deletions.
3 changes: 3 additions & 0 deletions amqp/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type backend struct {
}

func NewBackend(cfg *AmqpConfig) (v *backend, err error) {
if cfg == nil {
cfg = DefaultAmqpConfig()
}
v = &backend{
reporters: dingo.NewHetroRoutines(),
rids: make(map[string]map[string]int),
Expand Down
3 changes: 3 additions & 0 deletions amqp/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type broker struct {
}

func NewBroker(cfg *AmqpConfig) (v *broker, err error) {
if cfg == nil {
cfg = DefaultAmqpConfig()
}
v = &broker{
consumers: dingo.NewRoutines(),
cfg: *cfg,
Expand Down
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ type Config struct {
Mappers_ int `json:"Mappers"`
}

/*Mappers is to set the count of mappers initiated.
Note: "mapper" is the replacement of "Broker" in local mode.
*/
func (cfg *Config) Mappers(count int) *Config {
cfg.Mappers_ = count
return cfg
Expand Down
2 changes: 1 addition & 1 deletion dingo.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ func (dg *App) Call(name string, opt *Option, args ...interface{}) (reports <-ch
//
// if we poll after calling, we may lose some report if
// the task finished very quickly.
if !opt.IgnoreReport() {
if !opt.GetIgnoreReport() {
reports, err = dg.b.Poll(t)
if err != nil {
return
Expand Down
8 changes: 4 additions & 4 deletions dingo_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (me *localSingleAppTestSuite) TestIgnoreReport() {
// initiate a task with an option(IgnoreReport == true)
reports, err := me.app.Call(
"TestIgnoreReport",
dingo.NewOption().SetIgnoreReport(true).SetMonitorProgress(true),
dingo.DefaultOption().IgnoreReport(true).MonitorProgress(true),
)
me.Nil(err)
me.Nil(reports)
Expand Down Expand Up @@ -210,7 +210,7 @@ func (me *localSingleAppTestSuite) TestMyMarshaller() {

reports, err := me.app.Call(
"TestMyMarshaller",
dingo.NewOption(),
dingo.DefaultOption(),
12345, "mission",
)
me.Nil(err)
Expand Down Expand Up @@ -278,7 +278,7 @@ func (me *localSingleAppTestSuite) TestCustomMarshaller() {

// initiate a task with an option(IgnoreReport == true)
reports, err := me.app.Call(
"TestCustomMarshaller", dingo.NewOption().SetMonitorProgress(true), 12345, "mission",
"TestCustomMarshaller", dingo.DefaultOption().MonitorProgress(true), 12345, "mission",
)
me.Nil(err)

Expand Down Expand Up @@ -353,7 +353,7 @@ func (me *localSingleAppTestSuite) TestCustomMarshallerWithMinimalFunc() {

// initiate a task with an option(IgnoreReport == true)
reports, err := me.app.Call(
"TestCustomMarshallerWithMinimalFunc", dingo.NewOption().SetMonitorProgress(true),
"TestCustomMarshallerWithMinimalFunc", dingo.DefaultOption().MonitorProgress(true),
)
me.Nil(err)

Expand Down
2 changes: 1 addition & 1 deletion dingo_multi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (ts *DingoMultiAppTestSuite) TestOrder() {

// register worker function
ts.register("TestOrder", work)
ts.setOption("TestOrder", dingo.NewOption().SetMonitorProgress(true))
ts.setOption("TestOrder", dingo.DefaultOption().MonitorProgress(true))
ts.allocate("TestOrder", 1, 1)

// sending tasks
Expand Down
4 changes: 2 additions & 2 deletions dingo_single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (ts *DingoSingleAppTestSuite) TestBasic() {
ts.Equal(0, remain)

// call that function
reports, err := ts.app.Call("TestBasic", dingo.NewOption().SetMonitorProgress(true), 5)
reports, err := ts.app.Call("TestBasic", dingo.DefaultOption().MonitorProgress(true), 5)
ts.Nil(err)
ts.NotNil(reports)

Expand Down Expand Up @@ -179,7 +179,7 @@ func (ts *DingoSingleAppTestSuite) TestSameID() {
if err != nil {
return
}
err = ts.app.SetOption(name, dingo.NewOption().SetMonitorProgress(true))
err = ts.app.SetOption(name, dingo.DefaultOption().MonitorProgress(true))
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (ts *mapperTestSuite) TestParellelMapping() {
// compose corresponding task
t, err := ts._trans.ComposeTask(
"ParellelMapping",
NewOption().SetMonitorProgress(true),
DefaultOption().MonitorProgress(true),
[]interface{}{i},
)
ts.Nil(err)
Expand Down
9 changes: 5 additions & 4 deletions marshaller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Marshaller
//

// TODO: make this private
type MarshallerTestSuite struct {
suite.Suite

Expand All @@ -20,7 +21,7 @@ type MarshallerTestSuite struct {

func (ts *MarshallerTestSuite) TestTask() {
task, err := composeTask(
"test", NewOption().SetIgnoreReport(true).SetMonitorProgress(true),
"test", DefaultOption().IgnoreReport(true).MonitorProgress(true),
[]interface{}{float64(1.5), "user", "password"},
)
ts.NotNil(task)
Expand All @@ -46,7 +47,7 @@ func (ts *MarshallerTestSuite) TestTask() {
ts.NotNil(t)
if t != nil {
ts.True(t.almostEqual(task))
ts.True(t.Option().IgnoreReport())
ts.True(t.Option().GetIgnoreReport())
}
}
}
Expand All @@ -59,7 +60,7 @@ func (ts *MarshallerTestSuite) TestTask() {
}

func (ts *MarshallerTestSuite) TestReport() {
task, err := composeTask("test", NewOption().SetIgnoreReport(true).SetMonitorProgress(true), nil)
task, err := composeTask("test", DefaultOption().IgnoreReport(true).MonitorProgress(true), nil)
ts.Nil(err)
if err != nil {
return
Expand Down Expand Up @@ -88,7 +89,7 @@ func (ts *MarshallerTestSuite) TestReport() {
ts.NotNil(r)
if r != nil {
ts.Equal(report, r)
ts.True(r.Option().IgnoreReport())
ts.True(r.Option().GetIgnoreReport())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (mgr *fnMgr) Register(name string, fn interface{}) (err error) {
}
nfns[name] = &fnOpt{
fn: fn,
opt: NewOption(),
opt: DefaultOption(),
mash: struct {
task, report int
}{Encode.Default, Encode.Default},
Expand Down Expand Up @@ -286,7 +286,7 @@ func (mgr *fnMgr) ComposeTask(name string, o *Option, args []interface{}) (t *Ta
}

if o == nil {
o = NewOption()
o = DefaultOption()
}

if id, err = m.NewID(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestMgrOption(t *testing.T) {
trans := newFnMgr()

// name doesn't register
ass.NotNil(trans.SetOption("TestMgrOption", NewOption()))
ass.NotNil(trans.SetOption("TestMgrOption", DefaultOption()))

// get won't work
opt, err := trans.GetOption("TestMgrOption")
Expand All @@ -105,7 +105,7 @@ func TestMgrOption(t *testing.T) {
ass.Nil(trans.Register("TestMgrOption", func() {}))

// ok
ass.Nil(trans.SetOption("TestMgrOption", NewOption()))
ass.Nil(trans.SetOption("TestMgrOption", DefaultOption()))

// ok to get
opt, err = trans.GetOption("TestMgrOption")
Expand Down
10 changes: 5 additions & 5 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ type Option struct {
MP bool
}

func (opt *Option) IgnoreReport() bool { return opt.IR }
func (opt *Option) MonitorProgress() bool { return opt.MP }
func (opt *Option) GetIgnoreReport() bool { return opt.IR }
func (opt *Option) GetMonitorProgress() bool { return opt.MP }

func (opt *Option) SetIgnoreReport(ignore bool) *Option {
func (opt *Option) IgnoreReport(ignore bool) *Option {
opt.IR = ignore
return opt
}
func (opt *Option) SetMonitorProgress(only bool) *Option {
func (opt *Option) MonitorProgress(only bool) *Option {
opt.MP = only
return opt
}

func NewOption() *Option {
func DefaultOption() *Option {
return &Option{}
}
28 changes: 28 additions & 0 deletions redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
##Redis

Builtin supports using __Redis__ as both brokers and backends:
```go
import dgredis // package name is prefixed 'dg' to avoid confliction with "redigo/redis" pacakge

brk, err := dgredis.NewBroker(nil) // create a Redis-Broker with default configuration
bkd, err := dgredis.NewBackend(nil) // create a Redis-Backend with default configuration
```

There is nothing much to config so far. More option would be added as needed.
```go
import dgredis

config := dgredis.DefaultRedisConfig()
cfg.Host("127.0.0.1") // host address
.Port(123) // host port
.Password("pwd123") // password
.PollTimeout(1) // interval between polling, in seconds
.MaxIdle(3) // ref(redigo): Maximum number of idle connections in the pool
.IdleTimeout(240*time.Second) // ref(redigo): Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.

// create with new configuration
brk, err := dgredis.NewBroker(cfg)
bkd, err := dgredis.NewBackend(cfg)
```
3 changes: 3 additions & 0 deletions redis/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type backend struct {
}

func NewBackend(cfg *RedisConfig) (v *backend, err error) {
if cfg == nil {
cfg = DefaultRedisConfig()
}
v = &backend{
reporters: dingo.NewHetroRoutines(),
rids: make(map[string]map[string]int),
Expand Down
3 changes: 3 additions & 0 deletions redis/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type broker struct {
}

func NewBroker(cfg *RedisConfig) (v *broker, err error) {
if cfg == nil {
cfg = DefaultRedisConfig()
}
v = &broker{
listeners: dingo.NewRoutines(),
cfg: *cfg,
Expand Down
6 changes: 3 additions & 3 deletions report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestReportMarshal(t *testing.T) {
P: &dingo.ReportPayload{
S: 101,
E: &dingo.Error{102, "test error"},
O: dingo.NewOption().SetIgnoreReport(true).SetMonitorProgress(true),
O: dingo.DefaultOption().IgnoreReport(true).MonitorProgress(true),
R: nil,
},
})
Expand All @@ -30,7 +30,7 @@ func TestReportMarshal(t *testing.T) {
ass.Equal("test_id", r.ID())
ass.Equal(int32(102), r.Error().Code())
ass.Equal("test error", r.Error().Msg())
ass.Equal(true, r.Option().IgnoreReport())
ass.Equal(true, r.Option().MonitorProgress())
ass.Equal(true, r.Option().GetIgnoreReport())
ass.Equal(true, r.Option().GetMonitorProgress())
}
}
2 changes: 1 addition & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Task struct {

func composeTask(name string, opt *Option, args []interface{}) (t *Task, err error) {
if opt == nil {
opt = NewOption() // make sure it's the default option
opt = DefaultOption() // make sure it's the default option
}
var id string
if id, err = (&uuidMaker{}).NewID(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func TestTaskEqual(t *testing.T) {

// different option
{
t, err := composeTask("name#1", NewOption().SetIgnoreReport(true), []interface{}{1, "test#123"})
t, err := composeTask("name#1", DefaultOption().IgnoreReport(true), []interface{}{1, "test#123"})
ass.Nil(err)

o, err := composeTask("name#1", NewOption().SetIgnoreReport(false), []interface{}{2, "test#123"})
o, err := composeTask("name#1", DefaultOption().IgnoreReport(false), []interface{}{2, "test#123"})
ass.Nil(err)

o.H.I = t.H.ID()
Expand Down
4 changes: 2 additions & 2 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (wrk *_workers) workerRoutine(
defer wait.Done()
rep := func(task *Task, status int16, payload []interface{}, err error, alreadySent bool) (sent bool) {
sent = alreadySent
if task.Option().IgnoreReport() {
if task.Option().GetIgnoreReport() {
return
}

Expand All @@ -263,7 +263,7 @@ func (wrk *_workers) workerRoutine(
}
}

if r.Done() || r.Option().MonitorProgress() {
if r.Done() || r.Option().GetMonitorProgress() {
if !sent {
sent = true
wrk.hooks.ReporterHook(ReporterEvent.BeforeReport, task)
Expand Down
6 changes: 3 additions & 3 deletions worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (ts *workerTestSuite) TestPanic() {
ts.Len(reports, 1)

// an option with MonitorProgress == false
task, err := ts._trans.ComposeTask("TestPanic", NewOption(), nil)
task, err := ts._trans.ComposeTask("TestPanic", DefaultOption(), nil)
ts.NotNil(task)
ts.Nil(err)
if task != nil {
Expand All @@ -113,7 +113,7 @@ func (ts *workerTestSuite) TestIgnoreReport() {
ts.Len(reports, 1)

// an option with IgnoreReport == true
task, err := ts._trans.ComposeTask("TestIgnoreReport", NewOption().SetIgnoreReport(true), nil)
task, err := ts._trans.ComposeTask("TestIgnoreReport", DefaultOption().IgnoreReport(true), nil)
ts.NotNil(task)
ts.Nil(err)

Expand All @@ -139,7 +139,7 @@ func (ts *workerTestSuite) TestMonitorProgress() {
ts.Len(reports, 1)

// an option with MonitorProgress == false
task, err := ts._trans.ComposeTask("TestOnlyResult", NewOption(), nil)
task, err := ts._trans.ComposeTask("TestOnlyResult", DefaultOption(), nil)
ts.NotNil(task)
ts.Nil(err)

Expand Down

0 comments on commit 474632f

Please sign in to comment.