Skip to content

Commit

Permalink
add the Oneshot method
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Nov 2, 2023
1 parent b73eba8 commit 2781b4c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- add PostHTTPRequestPlugin
- support io_uring
- add CacheDiscovery
- add Oneshot method for XClient


## 1.8.0
Expand Down
27 changes: 15 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,17 +262,20 @@ func (client *Client) Go(ctx context.Context, servicePath, serviceMethod string,

call.Args = args
call.Reply = reply
if done == nil {
done = make(chan *Call, 10) // buffered.
} else {
// If caller passes done != nil, it must arrange that
// done has enough buffer for the number of simultaneous
// RPCs that will be using that channel. If the channel
// is totally unbuffered, it's best not to run at all.
if cap(done) == 0 {
log.Panic("rpc: done channel is unbuffered")
}
}

// // we allow done is nil
// if done == nil {
// done = make(chan *Call, 10) // buffered.
// } else {
// // If caller passes done != nil, it must arrange that
// // done has enough buffer for the number of simultaneous
// // RPCs that will be using that channel. If the channel
// // is totally unbuffered, it's best not to run at all.
// if cap(done) == 0 {
// log.Panic("rpc: done channel is unbuffered")
// }
// }

call.Done = done

if share.Trace {
Expand Down Expand Up @@ -305,7 +308,7 @@ func (client *Client) call(ctx context.Context, servicePath, serviceMethod strin
}()
}

Done := client.Go(ctx, servicePath, serviceMethod, args, reply, make(chan *Call, 1)).Done
Done := client.Go(ctx, servicePath, serviceMethod, args, reply, make(chan *Call, 10)).Done

var err error
select {
Expand Down
8 changes: 8 additions & 0 deletions client/xclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type XClient interface {

Go(ctx context.Context, serviceMethod string, args interface{}, reply interface{}, done chan *Call) (*Call, error)
Call(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
Oneshot(ctx context.Context, serviceMethod string, args interface{}) error
Broadcast(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
Fork(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) error
Inform(ctx context.Context, serviceMethod string, args interface{}, reply interface{}) ([]Receipt, error)
Expand Down Expand Up @@ -652,6 +653,13 @@ func (c *xClient) Call(ctx context.Context, serviceMethod string, args interface
}
}

// Oneshot invokes the named function, ** DOEST NOT ** wait for it to complete, and returns immediately.
func (c *xClient) Oneshot(ctx context.Context, serviceMethod string, args interface{}) error {
_, err := c.Go(ctx, serviceMethod, args, nil, nil)

return err
}

func uncoverError(err error) bool {
if _, ok := err.(ServiceError); ok {
return false
Expand Down

0 comments on commit 2781b4c

Please sign in to comment.