-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathunicast.go
More file actions
43 lines (38 loc) · 1.45 KB
/
unicast.go
File metadata and controls
43 lines (38 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package gorums
import "github.com/relab/gorums/internal/stream"
// Unicast is a one-way call; no replies are returned to the client.
//
// By default, this method blocks until the message has been sent to the node.
// This ensures that send operations complete before the caller proceeds, which can
// be useful for observing context cancellation or for pacing message sends.
// If the sending fails, the error is returned to the caller.
//
// With the IgnoreErrors call option, the method returns nil immediately after
// enqueueing the message (fire-and-forget semantics).
//
// This method should be used by generated code only.
func Unicast[Req msg](ctx *NodeContext, req Req, method string, opts ...CallOption) error {
callOpts := getCallOptions(E_Unicast, opts...)
reqMsg, err := stream.NewMessage(ctx, ctx.nextMsgID(), method, req)
if err != nil {
return err
}
waitSendDone := callOpts.mustWaitSendDone()
if !waitSendDone {
// Fire-and-forget: enqueue and return immediately
ctx.enqueue(stream.Request{Ctx: ctx, Msg: reqMsg})
return nil
}
// Default: block until send completes
replyChan := make(chan NodeResponse[*stream.Message], 1)
ctx.enqueue(stream.Request{Ctx: ctx, Msg: reqMsg, WaitSendDone: true, ResponseChan: replyChan})
// Wait for send confirmation
select {
case r := <-replyChan:
// Unicast doesn't expect replies, but we still
// want to report errors from the send attempt.
return r.Err
case <-ctx.Done():
return ctx.Err()
}
}