2026-04-25 01:18:30 +08:00

59 lines
1.2 KiB
Go

package app
import (
"errors"
"net"
"sync"
"google.golang.org/grpc"
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
"hyapp/pkg/grpchealth"
"hyapp/services/activity-service/internal/config"
)
// App 只提供 activity-service 的占位进程骨架。
type App struct {
server *grpc.Server
listener net.Listener
health *grpchealth.ServingChecker
closeOnce sync.Once
}
// New 初始化 activity-service 占位应用。
func New(cfg config.Config) (*App, error) {
listener, err := net.Listen("tcp", cfg.GRPCAddr)
if err != nil {
return nil, err
}
server := grpc.NewServer()
health := grpchealth.NewServingChecker("activity-service")
healthgrpc.RegisterHealthServer(server, grpchealth.NewServer(health))
return &App{
server: server,
listener: listener,
health: health,
}, nil
}
// Run 启动占位 gRPC 服务。
func (a *App) Run() error {
a.health.MarkServing()
err := a.server.Serve(a.listener)
a.health.MarkStopped()
if errors.Is(err, grpc.ErrServerStopped) {
return nil
}
return err
}
// Close 关闭占位 gRPC 服务。
func (a *App) Close() {
a.closeOnce.Do(func() {
a.health.MarkDraining()
a.server.GracefulStop()
})
}