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

程序退出的疑问 #85

Open
yaobo-lab opened this issue Jul 24, 2020 · 0 comments
Open

程序退出的疑问 #85

yaobo-lab opened this issue Jul 24, 2020 · 0 comments

Comments

@yaobo-lab
Copy link

yaobo-lab commented Jul 24, 2020

程序优雅的退时。使用了原子操作atomic.CompareAndSwapInt32 。atomic.LoadInt32;
原子操作在这里起到的作用是?或者说:为什么退出这里引入了 atomic
期待你的指导。谢谢

`func Run(ctx context.Context, opts ...Option) error {
var state int32 = 1
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
cleanFunc, err := Init(ctx, opts...)
if err != nil {
return err
}

EXIT:
for {
sig := <-sc
logger.Printf(ctx, "接收到信号[%s]", sig.String())
switch sig {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
atomic.CompareAndSwapInt32(&state, 1, 0)
break EXIT
case syscall.SIGHUP:
default:
break EXIT
}
}

cleanFunc()
logger.Printf(ctx, "服务退出")
time.Sleep(time.Second)
os.Exit(int(atomic.LoadInt32(&state)))
return nil

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant