You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.7 KiB

6 years ago
6 years ago
6 years ago
  1. package operation
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. "google.golang.org/grpc"
  10. "strconv"
  11. "strings"
  12. )
  13. func WithVolumeServerClient(volumeServer string, grpcDialOption grpc.DialOption, fn func(volume_server_pb.VolumeServerClient) error) error {
  14. ctx := context.Background()
  15. grpcAddress, err := toVolumeServerGrpcAddress(volumeServer)
  16. if err != nil {
  17. return err
  18. }
  19. return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
  20. client := volume_server_pb.NewVolumeServerClient(grpcConnection)
  21. return fn(client)
  22. }, grpcAddress, grpcDialOption)
  23. }
  24. func toVolumeServerGrpcAddress(volumeServer string) (grpcAddress string, err error) {
  25. sepIndex := strings.LastIndex(volumeServer, ":")
  26. port, err := strconv.Atoi(volumeServer[sepIndex+1:])
  27. if err != nil {
  28. glog.Errorf("failed to parse volume server address: %v", volumeServer)
  29. return "", err
  30. }
  31. return fmt.Sprintf("%s:%d", volumeServer[0:sepIndex], port+10000), nil
  32. }
  33. func withMasterServerClient(masterServer string, grpcDialOption grpc.DialOption, fn func(masterClient master_pb.SeaweedClient) error) error {
  34. ctx := context.Background()
  35. masterGrpcAddress, parseErr := util.ParseServerToGrpcAddress(masterServer)
  36. if parseErr != nil {
  37. return fmt.Errorf("failed to parse master grpc %v: %v", masterServer, parseErr)
  38. }
  39. return util.WithCachedGrpcClient(ctx, func(grpcConnection *grpc.ClientConn) error {
  40. client := master_pb.NewSeaweedClient(grpcConnection)
  41. return fn(client)
  42. }, masterGrpcAddress, grpcDialOption)
  43. }