Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix waiting forever problem #429

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func serve(c *cli.Context) error {
defer cluster.Finalizer()
cluster.DisasterRecover()

rpcch := make(chan struct{}, 1)
vibranium := rpc.New(cluster, config, rpcch)
stop := make(chan struct{}, 1)
vibranium := rpc.New(cluster, config, stop)
s, err := net.Listen("tcp", config.Bind)
if err != nil {
log.Errorf(context.TODO(), "[main] %v", err)
Expand Down Expand Up @@ -129,7 +129,7 @@ func serve(c *cli.Context) error {
<-ctx.Done()

log.Info("[main] Interrupt by signal")
close(rpcch)
close(stop)
unregisterService()
grpcServer.GracefulStop()
log.Info("[main] gRPC server gracefully stopped.")
Expand Down
28 changes: 16 additions & 12 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Vibranium struct {
cluster cluster.Cluster
config types.Config
counter sync.WaitGroup
rpcch chan struct{}
stop chan struct{}
TaskNum int
}

Expand All @@ -50,14 +50,18 @@ func (v *Vibranium) WatchServiceStatus(_ *pb.Empty, stream pb.CoreRPC_WatchServi
if err != nil {
return grpcstatus.Error(WatchServiceStatus, err.Error())
}
for status := range ch {
s := toRPCServiceStatus(status)
if err = stream.Send(s); err != nil {
v.logUnsentMessages(ctx, "WatchServicesStatus", err, s)
return grpcstatus.Error(WatchServiceStatus, err.Error())
for {
select {
case status := <-ch:
s := toRPCServiceStatus(status)
if err = stream.Send(s); err != nil {
v.logUnsentMessages(ctx, "WatchServicesStatus", err, s)
return grpcstatus.Error(WatchServiceStatus, err.Error())
}
case <-v.stop:
return nil
}
}
return nil
}

// ListNetworks list networks for pod
Expand Down Expand Up @@ -246,7 +250,7 @@ func (v *Vibranium) NodeStatusStream(_ *pb.Empty, stream pb.CoreRPC_NodeStatusSt
if err := stream.Send(r); err != nil {
v.logUnsentMessages(ctx, "NodeStatusStream", err, m)
}
case <-v.rpcch:
case <-v.stop:
return nil
}
}
Expand Down Expand Up @@ -407,7 +411,7 @@ func (v *Vibranium) WorkloadStatusStream(opts *pb.WorkloadStatusStreamOptions, s
if err := stream.Send(r); err != nil {
v.logUnsentMessages(ctx, "WorkloadStatusStream", err, m)
}
case <-v.rpcch:
case <-v.stop:
return nil
}
}
Expand Down Expand Up @@ -813,7 +817,7 @@ func (v *Vibranium) LogStream(opts *pb.LogStreamOptions, stream pb.CoreRPC_LogSt
if err = stream.Send(toRPCLogStreamMessage(m)); err != nil {
v.logUnsentMessages(ctx, "LogStream", err, m)
}
case <-v.rpcch:
case <-v.stop:
return nil
}
}
Expand Down Expand Up @@ -941,6 +945,6 @@ func (v *Vibranium) logUnsentMessages(ctx context.Context, msgType string, err e
}

// New will new a new cluster instance
func New(cluster cluster.Cluster, config types.Config, rpcch chan struct{}) *Vibranium {
return &Vibranium{cluster: cluster, config: config, counter: sync.WaitGroup{}, rpcch: rpcch}
func New(cluster cluster.Cluster, config types.Config, stop chan struct{}) *Vibranium {
return &Vibranium{cluster: cluster, config: config, counter: sync.WaitGroup{}, stop: stop}
}