-
Notifications
You must be signed in to change notification settings - Fork 175
/
conduit.go
76 lines (61 loc) · 2.47 KB
/
conduit.go
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package corruptnet
import (
"context"
"fmt"
"github.com/onflow/flow-go/network"
"github.com/onflow/flow-go/insecure"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/network/channels"
)
// Conduit implements a corruptible conduit that sends all incoming events to its registered controller (i.e., factory)
// without dispatching them to the networking layer.
type Conduit struct {
ctx context.Context
cancel context.CancelFunc
channel channels.Channel
egressController insecure.EgressController
}
var _ network.Conduit = &Conduit{}
// Publish sends the incoming events as publish events to the controller of this conduit (i.e., its factory) to handle.
func (c *Conduit) Publish(event interface{}, targetIDs ...flow.Identifier) error {
if c.ctx.Err() != nil {
return fmt.Errorf("conduit for channel %s closed", c.channel)
}
err := c.egressController.HandleOutgoingEvent(event, c.channel, insecure.Protocol_PUBLISH, 0, targetIDs...)
if err != nil {
return fmt.Errorf("factory could not handle the publish event: %w", err)
}
return nil
}
// Unicast sends the incoming events as unicast events to the controller of this conduit (i.e., its factory) to handle.
func (c *Conduit) Unicast(event interface{}, targetID flow.Identifier) error {
if c.ctx.Err() != nil {
return fmt.Errorf("conduit for channel %s closed", c.channel)
}
err := c.egressController.HandleOutgoingEvent(event, c.channel, insecure.Protocol_UNICAST, 0, targetID)
if err != nil {
return fmt.Errorf("factory could not handle the unicast event: %w", err)
}
return nil
}
// Multicast sends the incoming events as multicast events to the controller of this conduit (i.e., its factory) to handle.
func (c *Conduit) Multicast(event interface{}, num uint, targetIDs ...flow.Identifier) error {
if c.ctx.Err() != nil {
return fmt.Errorf("conduit for channel %s closed", c.channel)
}
err := c.egressController.HandleOutgoingEvent(event, c.channel, insecure.Protocol_MULTICAST, uint32(num), targetIDs...)
if err != nil {
return fmt.Errorf("factory could not handle the multicast event: %w", err)
}
return nil
}
// Close informs the conduit controller that the engine is not going to use this conduit anymore.
func (c *Conduit) Close() error {
if c.ctx.Err() != nil {
return fmt.Errorf("conduit for channel %s already closed", c.channel)
}
// close the conduit context
c.cancel()
// call the close function
return c.egressController.EngineClosingChannel(c.channel)
}