diff options
| author | Chris Lu <chris.lu@gmail.com> | 2020-05-05 02:05:28 -0700 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2020-05-05 02:05:28 -0700 |
| commit | 1e3e4b3072071341b4bb4b0bb7c611457e927f97 (patch) | |
| tree | d32efe43fdc34d203393d45d8343d54eaa19f180 /weed/messaging/msgclient/client.go | |
| parent | 47234760f40e4d2cea87b4a83d2178b8181598f5 (diff) | |
| download | seaweedfs-1e3e4b3072071341b4bb4b0bb7c611457e927f97.tar.xz seaweedfs-1e3e4b3072071341b4bb4b0bb7c611457e927f97.zip | |
add broker connects to filer
Diffstat (limited to 'weed/messaging/msgclient/client.go')
| -rw-r--r-- | weed/messaging/msgclient/client.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/weed/messaging/msgclient/client.go b/weed/messaging/msgclient/client.go new file mode 100644 index 000000000..f4e11232e --- /dev/null +++ b/weed/messaging/msgclient/client.go @@ -0,0 +1,56 @@ +package msgclient + +import ( + "context" + "fmt" + "log" + + "google.golang.org/grpc" + + "github.com/chrislusf/seaweedfs/weed/messaging/broker" + "github.com/chrislusf/seaweedfs/weed/pb" + "github.com/chrislusf/seaweedfs/weed/pb/messaging_pb" + "github.com/chrislusf/seaweedfs/weed/security" + "github.com/chrislusf/seaweedfs/weed/util" +) + +type MessagingClient struct { + bootstrapBrokers []string + grpcConnections map[broker.TopicPartition]*grpc.ClientConn + grpcDialOption grpc.DialOption +} + +func NewMessagingClient(bootstrapBrokers ...string) *MessagingClient { + return &MessagingClient{ + bootstrapBrokers: bootstrapBrokers, + grpcConnections: make(map[broker.TopicPartition]*grpc.ClientConn), + grpcDialOption: security.LoadClientTLS(util.GetViper(), "grpc.msg_client"), + } +} + + +func (mc *MessagingClient) findBroker(tp broker.TopicPartition) (*grpc.ClientConn, error) { + + for _, broker := range mc.bootstrapBrokers { + grpcConnection, err := pb.GrpcDial(context.Background(), broker, mc.grpcDialOption) + if err != nil { + log.Printf("dial broker %s: %v", broker, err) + continue + } + defer grpcConnection.Close() + + resp, err := messaging_pb.NewSeaweedMessagingClient(grpcConnection).FindBroker(context.Background(), + &messaging_pb.FindBrokerRequest{ + Namespace: tp.Namespace, + Topic: tp.Topic, + Parition: tp.Partition, + }) + if err != nil { + return nil, err + } + + targetBroker := resp.Broker + return pb.GrpcDial(context.Background(), targetBroker, mc.grpcDialOption) + } + return nil, fmt.Errorf("no broker found for %+v", tp) +} |
