mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
Other: Single instance
This commit is contained in:
@ -6,7 +6,9 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/proton-bridge/v2/internal/focus/proto"
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
@ -27,6 +29,12 @@ func TryRaise() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := cc.Close(); err != nil {
|
||||
logrus.WithError(err).Warn("Failed to close focus connection")
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := proto.NewFocusClient(cc).Raise(ctx, &emptypb.Empty{}); err != nil {
|
||||
return false
|
||||
}
|
||||
@ -37,3 +45,37 @@ func TryRaise() bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// TryRaise tries to raise the application by dialing the focus service.
|
||||
// It returns true if the service is running and the application was told to raise.
|
||||
func TryVersion() (*semver.Version, bool) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
cc, err := grpc.DialContext(
|
||||
ctx,
|
||||
net.JoinHostPort(Host, fmt.Sprint(Port)),
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := cc.Close(); err != nil {
|
||||
logrus.WithError(err).Warn("Failed to close focus connection")
|
||||
}
|
||||
}()
|
||||
|
||||
raw, err := proto.NewFocusClient(cc).Version(ctx, &emptypb.Empty{})
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
version, err := semver.NewVersion(raw.GetVersion())
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return version, true
|
||||
}
|
||||
|
||||
@ -3,12 +3,13 @@ package focus
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFocusRaise(t *testing.T) {
|
||||
func TestFocus_Raise(t *testing.T) {
|
||||
// Start the focus service.
|
||||
service, err := NewService()
|
||||
service, err := NewService(semver.MustParse("1.2.3"))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to dial it, it should succeed.
|
||||
@ -23,3 +24,14 @@ func TestFocusRaise(t *testing.T) {
|
||||
// Try to dial it, it should fail.
|
||||
require.False(t, TryRaise())
|
||||
}
|
||||
|
||||
func TestFocus_Version(t *testing.T) {
|
||||
// Start the focus service.
|
||||
_, err := NewService(semver.MustParse("1.2.3"))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to dial it, it should succeed.
|
||||
version, ok := TryVersion()
|
||||
require.True(t, ok)
|
||||
require.Equal(t, "1.2.3", version.String())
|
||||
}
|
||||
|
||||
@ -18,17 +18,17 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.0
|
||||
// protoc v3.21.3
|
||||
// protoc v3.21.7
|
||||
// source: focus.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
emptypb "google.golang.org/protobuf/types/known/emptypb"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -38,31 +38,101 @@ const (
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type VersionResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Version string `protobuf:"bytes,1,opt,name=version,proto3" json:"version,omitempty"`
|
||||
}
|
||||
|
||||
func (x *VersionResponse) Reset() {
|
||||
*x = VersionResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_focus_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *VersionResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*VersionResponse) ProtoMessage() {}
|
||||
|
||||
func (x *VersionResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_focus_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use VersionResponse.ProtoReflect.Descriptor instead.
|
||||
func (*VersionResponse) Descriptor() ([]byte, []int) {
|
||||
return file_focus_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *VersionResponse) GetVersion() string {
|
||||
if x != nil {
|
||||
return x.Version
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_focus_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_focus_proto_rawDesc = []byte{
|
||||
0x0a, 0x0b, 0x66, 0x6f, 0x63, 0x75, 0x73, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x70,
|
||||
0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x32, 0x40, 0x0a, 0x05, 0x46, 0x6f, 0x63, 0x75, 0x73, 0x12, 0x37, 0x0a, 0x05, 0x52, 0x61,
|
||||
0x69, 0x73, 0x65, 0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x16, 0x2e, 0x67, 0x6f,
|
||||
0x6f, 0x22, 0x2b, 0x0a, 0x0f, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70,
|
||||
0x6f, 0x6e, 0x73, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x32, 0x7b,
|
||||
0x0a, 0x05, 0x46, 0x6f, 0x63, 0x75, 0x73, 0x12, 0x37, 0x0a, 0x05, 0x52, 0x61, 0x69, 0x73, 0x65,
|
||||
0x12, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
|
||||
0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
|
||||
0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79,
|
||||
0x12, 0x39, 0x0a, 0x07, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x2e, 0x67, 0x6f,
|
||||
0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d,
|
||||
0x70, 0x74, 0x79, 0x42, 0x3d, 0x5a, 0x3b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
|
||||
0x6d, 0x2f, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x6e, 0x4d, 0x61, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x6e, 0x2d, 0x62, 0x72, 0x69, 0x64, 0x67, 0x65, 0x2f, 0x76, 0x32, 0x2f, 0x69, 0x6e,
|
||||
0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x66, 0x6f, 0x63, 0x75, 0x73, 0x2f, 0x70, 0x72, 0x6f,
|
||||
0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x70, 0x74, 0x79, 0x1a, 0x16, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x56, 0x65, 0x72, 0x73,
|
||||
0x69, 0x6f, 0x6e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x3d, 0x5a, 0x3b, 0x67,
|
||||
0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x6e,
|
||||
0x4d, 0x61, 0x69, 0x6c, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x6e, 0x2d, 0x62, 0x72, 0x69, 0x64,
|
||||
0x67, 0x65, 0x2f, 0x76, 0x32, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x66,
|
||||
0x6f, 0x63, 0x75, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_focus_proto_rawDescOnce sync.Once
|
||||
file_focus_proto_rawDescData = file_focus_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_focus_proto_rawDescGZIP() []byte {
|
||||
file_focus_proto_rawDescOnce.Do(func() {
|
||||
file_focus_proto_rawDescData = protoimpl.X.CompressGZIP(file_focus_proto_rawDescData)
|
||||
})
|
||||
return file_focus_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_focus_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
|
||||
var file_focus_proto_goTypes = []interface{}{
|
||||
(*emptypb.Empty)(nil), // 0: google.protobuf.Empty
|
||||
(*VersionResponse)(nil), // 0: proto.VersionResponse
|
||||
(*emptypb.Empty)(nil), // 1: google.protobuf.Empty
|
||||
}
|
||||
var file_focus_proto_depIdxs = []int32{
|
||||
0, // 0: proto.Focus.Raise:input_type -> google.protobuf.Empty
|
||||
0, // 1: proto.Focus.Raise:output_type -> google.protobuf.Empty
|
||||
1, // [1:2] is the sub-list for method output_type
|
||||
0, // [0:1] is the sub-list for method input_type
|
||||
1, // 0: proto.Focus.Raise:input_type -> google.protobuf.Empty
|
||||
1, // 1: proto.Focus.Version:input_type -> google.protobuf.Empty
|
||||
1, // 2: proto.Focus.Raise:output_type -> google.protobuf.Empty
|
||||
0, // 3: proto.Focus.Version:output_type -> proto.VersionResponse
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
@ -73,18 +143,33 @@ func file_focus_proto_init() {
|
||||
if File_focus_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_focus_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*VersionResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_focus_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 0,
|
||||
NumMessages: 1,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_focus_proto_goTypes,
|
||||
DependencyIndexes: file_focus_proto_depIdxs,
|
||||
MessageInfos: file_focus_proto_msgTypes,
|
||||
}.Build()
|
||||
File_focus_proto = out.File
|
||||
file_focus_proto_rawDesc = nil
|
||||
|
||||
@ -28,4 +28,11 @@ package proto;
|
||||
//**********************************************************************************************************************≠––
|
||||
service Focus {
|
||||
rpc Raise(google.protobuf.Empty) returns (google.protobuf.Empty);
|
||||
rpc Version(google.protobuf.Empty) returns (VersionResponse);
|
||||
}
|
||||
|
||||
//**********************************************************************************************************************
|
||||
|
||||
message VersionResponse {
|
||||
string version = 1;
|
||||
}
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.21.3
|
||||
// - protoc v3.21.7
|
||||
// source: focus.proto
|
||||
|
||||
package proto
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
@ -25,6 +24,7 @@ const _ = grpc.SupportPackageIsVersion7
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type FocusClient interface {
|
||||
Raise(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error)
|
||||
Version(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*VersionResponse, error)
|
||||
}
|
||||
|
||||
type focusClient struct {
|
||||
@ -44,11 +44,21 @@ func (c *focusClient) Raise(ctx context.Context, in *emptypb.Empty, opts ...grpc
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *focusClient) Version(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*VersionResponse, error) {
|
||||
out := new(VersionResponse)
|
||||
err := c.cc.Invoke(ctx, "/proto.Focus/Version", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FocusServer is the server API for Focus service.
|
||||
// All implementations must embed UnimplementedFocusServer
|
||||
// for forward compatibility
|
||||
type FocusServer interface {
|
||||
Raise(context.Context, *emptypb.Empty) (*emptypb.Empty, error)
|
||||
Version(context.Context, *emptypb.Empty) (*VersionResponse, error)
|
||||
mustEmbedUnimplementedFocusServer()
|
||||
}
|
||||
|
||||
@ -59,6 +69,9 @@ type UnimplementedFocusServer struct {
|
||||
func (UnimplementedFocusServer) Raise(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Raise not implemented")
|
||||
}
|
||||
func (UnimplementedFocusServer) Version(context.Context, *emptypb.Empty) (*VersionResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Version not implemented")
|
||||
}
|
||||
func (UnimplementedFocusServer) mustEmbedUnimplementedFocusServer() {}
|
||||
|
||||
// UnsafeFocusServer may be embedded to opt out of forward compatibility for this service.
|
||||
@ -90,6 +103,24 @@ func _Focus_Raise_Handler(srv interface{}, ctx context.Context, dec func(interfa
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _Focus_Version_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(emptypb.Empty)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(FocusServer).Version(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/proto.Focus/Version",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(FocusServer).Version(ctx, req.(*emptypb.Empty))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// Focus_ServiceDesc is the grpc.ServiceDesc for Focus service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
@ -101,6 +132,10 @@ var Focus_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "Raise",
|
||||
Handler: _Focus_Raise_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Version",
|
||||
Handler: _Focus_Version_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "focus.proto",
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/Masterminds/semver/v3"
|
||||
"github.com/ProtonMail/proton-bridge/v2/internal/focus/proto"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
@ -24,11 +25,12 @@ type Service struct {
|
||||
server *grpc.Server
|
||||
listener net.Listener
|
||||
raiseCh chan struct{}
|
||||
version *semver.Version
|
||||
}
|
||||
|
||||
// NewService creates a new focus service.
|
||||
// It listens on the local host and port 1042 (by default).
|
||||
func NewService() (*Service, error) {
|
||||
func NewService(version *semver.Version) (*Service, error) {
|
||||
listener, err := net.Listen("tcp", net.JoinHostPort(Host, fmt.Sprint(Port)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listen: %w", err)
|
||||
@ -38,6 +40,7 @@ func NewService() (*Service, error) {
|
||||
server: grpc.NewServer(),
|
||||
listener: listener,
|
||||
raiseCh: make(chan struct{}, 1),
|
||||
version: version,
|
||||
}
|
||||
|
||||
proto.RegisterFocusServer(service.server, service)
|
||||
@ -57,6 +60,13 @@ func (service *Service) Raise(context.Context, *emptypb.Empty) (*emptypb.Empty,
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
// Version implements the gRPC FocusService interface; it returns the version of the service.
|
||||
func (service *Service) Version(context.Context, *emptypb.Empty) (*proto.VersionResponse, error) {
|
||||
return &proto.VersionResponse{
|
||||
Version: service.version.Original(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetRaiseCh returns a channel on which events are sent when the application should be raised.
|
||||
func (service *Service) GetRaiseCh() <-chan struct{} {
|
||||
return service.raiseCh
|
||||
|
||||
Reference in New Issue
Block a user