Skip to content

Commit

Permalink
enqueue with options
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed Oct 3, 2018
1 parent 9ac5ba5 commit f32837d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
13 changes: 13 additions & 0 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gogo/protobuf/proto"
"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/route"
"github.com/topfreegames/pitaya/worker"
)

// RPC calls a method in a different server
Expand All @@ -40,6 +41,7 @@ func RPCTo(ctx context.Context, serverID, routeStr string, reply proto.Message,
}

// ReliableRPC enqueues RPC to worker so it's executed asynchronously
// Default enqueue options are used
func ReliableRPC(
routeStr string,
metadata map[string]interface{},
Expand All @@ -48,6 +50,17 @@ func ReliableRPC(
return app.worker.EnqueueRPC(routeStr, metadata, reply, arg)
}

// ReliableRPCWithOptions enqueues RPC to worker
// Receive worker options for this specific RPC
func ReliableRPCWithOptions(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *worker.EnqueueOpts,
) (jid string, err error) {
return app.worker.EnqueueRPCWithOptions(routeStr, metadata, reply, arg, opts)
}

func doSendRPC(ctx context.Context, serverID, routeStr string, reply proto.Message, arg proto.Message) error {
if app.rpcServer == nil {
return constants.ErrRPCServerNotInitialized
Expand Down
33 changes: 25 additions & 8 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (w *Worker) EnqueueRPC(
metadata map[string]interface{},
reply, arg proto.Message,
) (jid string, err error) {
opts := w.enqueueOptions()
opts := w.enqueueOptions(w.opts)
return workers.EnqueueWithOptions(rpcQueue, class, &rpcInfo{
Route: routeStr,
Metadata: metadata,
Expand All @@ -93,6 +93,21 @@ func (w *Worker) EnqueueRPC(
}, opts)
}

// EnqueueRPCWithOptions enqueues rpc job to worker
func (w *Worker) EnqueueRPCWithOptions(
routeStr string,
metadata map[string]interface{},
reply, arg proto.Message,
opts *EnqueueOpts,
) (jid string, err error) {
return workers.EnqueueWithOptions(rpcQueue, class, &rpcInfo{
Route: routeStr,
Metadata: metadata,
Arg: arg,
Reply: reply,
}, w.enqueueOptions(opts))
}

// RegisterRPCJob registers a RPC job
func (w *Worker) RegisterRPCJob(rpcJob RPCJob) error {
if w.registered {
Expand Down Expand Up @@ -152,15 +167,17 @@ func (w *Worker) parsedRPCJob(rpcJob RPCJob) func(*workers.Msg) {
}
}

func (w *Worker) enqueueOptions() workers.EnqueueOptions {
func (w *Worker) enqueueOptions(
opts *EnqueueOpts,
) workers.EnqueueOptions {
return workers.EnqueueOptions{
Retry: w.opts.RetryEnabled,
RetryMax: w.opts.MaxRetries,
Retry: opts.RetryEnabled,
RetryMax: opts.MaxRetries,
RetryOptions: workers.RetryOptions{
Exp: w.opts.ExponentialFactor,
MinDelay: w.opts.MinDelayToRetry,
MaxDelay: w.opts.MaxDelayToRetry,
MaxRand: w.opts.MaxRandom,
Exp: opts.ExponentialFactor,
MinDelay: opts.MinDelayToRetry,
MaxDelay: opts.MaxDelayToRetry,
MaxRand: opts.MaxRandom,
},
}
}
Expand Down

0 comments on commit f32837d

Please sign in to comment.