|
|
@ -3,6 +3,7 @@ package pb |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog" |
|
|
|
"net/http" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
@ -108,51 +109,51 @@ func WithCachedGrpcClient(fn func(*grpc.ClientConn) error, address string, opts |
|
|
|
} |
|
|
|
|
|
|
|
func ParseServerToGrpcAddress(server string) (serverGrpcAddress string, err error) { |
|
|
|
colonIndex := strings.LastIndex(server, ":") |
|
|
|
if colonIndex < 0 { |
|
|
|
return "", fmt.Errorf("server should have hostname:port format: %v", server) |
|
|
|
} |
|
|
|
|
|
|
|
port, parseErr := strconv.ParseUint(server[colonIndex+1:], 10, 64) |
|
|
|
host, port, parseErr := hostAndPort(server) |
|
|
|
if parseErr != nil { |
|
|
|
return "", fmt.Errorf("server port parse error: %v", parseErr) |
|
|
|
} |
|
|
|
|
|
|
|
grpcPort := int(port) + 10000 |
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%d", server[:colonIndex], grpcPort), nil |
|
|
|
return fmt.Sprintf("%s:%d", host, grpcPort), nil |
|
|
|
} |
|
|
|
|
|
|
|
func ServerToGrpcAddress(server string) (serverGrpcAddress string) { |
|
|
|
hostnameAndPort := strings.Split(server, ":") |
|
|
|
if len(hostnameAndPort) != 2 { |
|
|
|
return fmt.Sprintf("unexpected server address: %s", server) |
|
|
|
func hostAndPort(address string) (host string, port uint64, err error) { |
|
|
|
colonIndex := strings.LastIndex(address, ":") |
|
|
|
if colonIndex < 0 { |
|
|
|
return "", 0, fmt.Errorf("server should have hostname:port format: %v", address) |
|
|
|
} |
|
|
|
port, err = strconv.ParseUint(address[colonIndex+1:], 10, 64) |
|
|
|
if err != nil { |
|
|
|
return "", 0, fmt.Errorf("server port parse error: %v", err) |
|
|
|
} |
|
|
|
|
|
|
|
port, parseErr := strconv.ParseUint(hostnameAndPort[1], 10, 64) |
|
|
|
return address[:colonIndex], port, err |
|
|
|
} |
|
|
|
|
|
|
|
func ServerToGrpcAddress(server string) (serverGrpcAddress string) { |
|
|
|
|
|
|
|
host, port, parseErr := hostAndPort(server) |
|
|
|
if parseErr != nil { |
|
|
|
return fmt.Sprintf("failed to parse port for %s:%s", hostnameAndPort[0], hostnameAndPort[1]) |
|
|
|
glog.Fatalf("server address %s parse error: %v", server, parseErr) |
|
|
|
} |
|
|
|
|
|
|
|
grpcPort := int(port) + 10000 |
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%d", hostnameAndPort[0], grpcPort) |
|
|
|
return fmt.Sprintf("%s:%d", host, grpcPort) |
|
|
|
} |
|
|
|
|
|
|
|
func GrpcAddressToServerAddress(grpcAddress string) (serverAddress string) { |
|
|
|
hostnameAndPort := strings.Split(grpcAddress, ":") |
|
|
|
if len(hostnameAndPort) != 2 { |
|
|
|
return fmt.Sprintf("unexpected grpcAddress: %s", grpcAddress) |
|
|
|
} |
|
|
|
|
|
|
|
grpcPort, parseErr := strconv.ParseUint(hostnameAndPort[1], 10, 64) |
|
|
|
host, grpcPort, parseErr := hostAndPort(grpcAddress) |
|
|
|
if parseErr != nil { |
|
|
|
return fmt.Sprintf("failed to parse port for %s:%s", hostnameAndPort[0], hostnameAndPort[1]) |
|
|
|
glog.Fatalf("server grpc address %s parse error: %v", grpcAddress, parseErr) |
|
|
|
} |
|
|
|
|
|
|
|
port := int(grpcPort) - 10000 |
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%d", hostnameAndPort[0], port) |
|
|
|
return fmt.Sprintf("%s:%d", host, port) |
|
|
|
} |
|
|
|
|
|
|
|
func WithMasterClient(master string, grpcDialOption grpc.DialOption, fn func(client master_pb.SeaweedClient) error) error { |
|
|
@ -197,19 +198,3 @@ func WithGrpcFilerClient(filerGrpcAddress string, grpcDialOption grpc.DialOption |
|
|
|
}, filerGrpcAddress, grpcDialOption) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func ParseFilerGrpcAddress(filer string) (filerGrpcAddress string, err error) { |
|
|
|
hostnameAndPort := strings.Split(filer, ":") |
|
|
|
if len(hostnameAndPort) != 2 { |
|
|
|
return "", fmt.Errorf("filer should have hostname:port format: %v", hostnameAndPort) |
|
|
|
} |
|
|
|
|
|
|
|
filerPort, parseErr := strconv.ParseUint(hostnameAndPort[1], 10, 64) |
|
|
|
if parseErr != nil { |
|
|
|
return "", fmt.Errorf("filer port parse error: %v", parseErr) |
|
|
|
} |
|
|
|
|
|
|
|
filerGrpcPort := int(filerPort) + 10000 |
|
|
|
|
|
|
|
return fmt.Sprintf("%s:%d", hostnameAndPort[0], filerGrpcPort), nil |
|
|
|
} |