-
Notifications
You must be signed in to change notification settings - Fork 7
/
point_to_point_channel.go
34 lines (27 loc) · 1.12 KB
/
point_to_point_channel.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
package strategies
import (
"context"
"fmt"
pb "github.com/buoyantio/bb/gen"
"github.com/buoyantio/bb/service"
)
// PointToPointStrategyName is the user-friendly name of this strategy
const PointToPointStrategyName = "point-to-point-channel"
// PointToPointChannelStrategy is a strategy that takes a request and forwards it to a single downstream service.
type PointToPointChannelStrategy struct {
clients []service.Client
}
func (s *PointToPointChannelStrategy) Do(ctx context.Context, req *pb.TheRequest) (*pb.TheResponse, error) {
client := s.clients[0]
resp, err := client.Send(ctx, req)
return resp, err
}
// NewPointToPointChannel creates a new PointToPointChannelStrategy
func NewPointToPointChannel(config *service.Config, servers []service.Server, clients []service.Client) (service.Strategy, error) {
if len(clients) != 1 || len(servers) != 1 {
return nil, fmt.Errorf("strategy [%s] requires exactly one server and one downstream service, but had clients [%v] servers [%v] and configured as: %+v", PointToPointStrategyName, clients, servers, config)
}
return &PointToPointChannelStrategy{
clients: clients,
}, nil
}