daemon: Split host operations into ManagedService

This commit is contained in:
世界
2026-06-11 08:11:06 +08:00
parent c2da94f252
commit 65aeb3c8f4
11 changed files with 1016 additions and 831 deletions
+38 -14
View File
@@ -23,14 +23,15 @@ import (
)
type CommandClient struct {
handler CommandClientHandler
grpcConn *grpc.ClientConn
grpcClient daemon.StartedServiceClient
options CommandClientOptions
ctx context.Context
cancel context.CancelFunc
clientMutex sync.RWMutex
standalone bool
handler CommandClientHandler
grpcConn *grpc.ClientConn
grpcClient daemon.StartedServiceClient
grpcManagedClient daemon.ManagedServiceClient
options CommandClientOptions
ctx context.Context
cancel context.CancelFunc
clientMutex sync.RWMutex
standalone bool
}
type CommandClientOptions struct {
@@ -201,6 +202,7 @@ func (c *CommandClient) Connect() error {
}
c.grpcConn = connection
c.grpcClient = client
c.grpcManagedClient = daemon.NewManagedServiceClient(connection)
c.ctx, c.cancel = context.WithCancel(context.Background())
c.clientMutex.Unlock()
@@ -227,6 +229,7 @@ func (c *CommandClient) ConnectWithFD(fd int32) error {
}
c.grpcConn = connection
c.grpcClient = client
c.grpcManagedClient = daemon.NewManagedServiceClient(connection)
c.ctx, c.cancel = context.WithCancel(context.Background())
c.clientMutex.Unlock()
@@ -287,6 +290,7 @@ func (c *CommandClient) getClientForCall() (daemon.StartedServiceClient, context
}
c.grpcConn = connection
c.grpcClient = client
c.grpcManagedClient = daemon.NewManagedServiceClient(connection)
if c.ctx == nil {
c.ctx, c.cancel = context.WithCancel(context.Background())
}
@@ -300,6 +304,7 @@ func (c *CommandClient) closeConnection() {
c.grpcConn.Close()
c.grpcConn = nil
c.grpcClient = nil
c.grpcManagedClient = nil
}
}
@@ -315,6 +320,25 @@ func callWithResult[T any](c *CommandClient, call func(ctx context.Context, clie
return call(ctx, client)
}
func callManagedWithResult[T any](c *CommandClient, call func(ctx context.Context, client daemon.ManagedServiceClient) (T, error)) (T, error) {
_, ctx, err := c.getClientForCall()
if err != nil {
var zero T
return zero, err
}
if c.standalone {
defer c.closeConnection()
}
c.clientMutex.RLock()
client := c.grpcManagedClient
c.clientMutex.RUnlock()
if client == nil {
var zero T
return zero, os.ErrClosed
}
return call(ctx, client)
}
func (c *CommandClient) getStreamContext() (daemon.StartedServiceClient, context.Context) {
c.clientMutex.RLock()
defer c.clientMutex.RUnlock()
@@ -541,7 +565,7 @@ func (c *CommandClient) CloseConnections() error {
}
func (c *CommandClient) ServiceReload() error {
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
_, err := callManagedWithResult(c, func(ctx context.Context, client daemon.ManagedServiceClient) (*emptypb.Empty, error) {
return client.ReloadService(ctx, &emptypb.Empty{})
})
if err != nil {
@@ -551,7 +575,7 @@ func (c *CommandClient) ServiceReload() error {
}
func (c *CommandClient) ServiceClose() error {
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
_, err := callManagedWithResult(c, func(ctx context.Context, client daemon.ManagedServiceClient) (*emptypb.Empty, error) {
return client.StopService(ctx, &emptypb.Empty{})
})
if err != nil {
@@ -571,7 +595,7 @@ func (c *CommandClient) ClearLogs() error {
}
func (c *CommandClient) GetSystemProxyStatus() (*SystemProxyStatus, error) {
return callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*SystemProxyStatus, error) {
return callManagedWithResult(c, func(ctx context.Context, client daemon.ManagedServiceClient) (*SystemProxyStatus, error) {
status, err := client.GetSystemProxyStatus(ctx, &emptypb.Empty{})
if err != nil {
return nil, E.Cause(err, "get system proxy status")
@@ -581,7 +605,7 @@ func (c *CommandClient) GetSystemProxyStatus() (*SystemProxyStatus, error) {
}
func (c *CommandClient) SetSystemProxyEnabled(isEnabled bool) error {
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
_, err := callManagedWithResult(c, func(ctx context.Context, client daemon.ManagedServiceClient) (*emptypb.Empty, error) {
return client.SetSystemProxyEnabled(ctx, &daemon.SetSystemProxyEnabledRequest{
Enabled: isEnabled,
})
@@ -593,7 +617,7 @@ func (c *CommandClient) SetSystemProxyEnabled(isEnabled bool) error {
}
func (c *CommandClient) TriggerGoCrash() error {
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
_, err := callManagedWithResult(c, func(ctx context.Context, client daemon.ManagedServiceClient) (*emptypb.Empty, error) {
return client.TriggerDebugCrash(ctx, &daemon.DebugCrashRequest{
Type: daemon.DebugCrashRequest_GO,
})
@@ -605,7 +629,7 @@ func (c *CommandClient) TriggerGoCrash() error {
}
func (c *CommandClient) TriggerNativeCrash() error {
_, err := callWithResult(c, func(ctx context.Context, client daemon.StartedServiceClient) (*emptypb.Empty, error) {
_, err := callManagedWithResult(c, func(ctx context.Context, client daemon.ManagedServiceClient) (*emptypb.Empty, error) {
return client.TriggerDebugCrash(ctx, &daemon.DebugCrashRequest{
Type: daemon.DebugCrashRequest_NATIVE,
})
+12
View File
@@ -20,12 +20,15 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type CommandServer struct {
*daemon.StartedService
managedService *daemon.ManagedService
handler CommandServerHandler
platformInterface PlatformInterface
platformWrapper *platformInterfaceWrapper
@@ -71,6 +74,10 @@ func NewCommandServer(handler CommandServerHandler, platformInterface PlatformIn
// GroupID: sGroupID,
// SystemProxyEnabled: false,
})
server.managedService = daemon.NewManagedService(daemon.ManagedServiceOptions{
Handler: (*platformHandler)(server),
Debug: sDebug,
})
return server, nil
}
@@ -155,6 +162,11 @@ func (s *CommandServer) Start() error {
}
s.grpcServer = grpc.NewServer(serverOptions...)
daemon.RegisterStartedServiceServer(s.grpcServer, s.StartedService)
daemon.RegisterManagedServiceServer(s.grpcServer, s.managedService)
healthServer := health.NewServer()
healthServer.SetServingStatus(daemon.StartedService_ServiceDesc.ServiceName, grpc_health_v1.HealthCheckResponse_SERVING)
healthServer.SetServingStatus(daemon.ManagedService_ServiceDesc.ServiceName, grpc_health_v1.HealthCheckResponse_SERVING)
grpc_health_v1.RegisterHealthServer(s.grpcServer, healthServer)
go s.grpcServer.Serve(listener)
return nil
}