Skip to content

Add signals/stop.go #915

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

Merged
merged 1 commit into from
Apr 24, 2025
Merged
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
1 change: 1 addition & 0 deletions examples/basic/signals/README.md
Original file line number Diff line number Diff line change
@@ -6,3 +6,4 @@
| ---------------- | --------------------- | ---------------------------------------------------------- |
| notify.go | signal_notify | signal.Notify のサンプルです |
| notifycontext.go | signal_notify_context | Go 1.16 から追加された signal.NotifyContext のサンプルです |
| stop.go | signal_stop | signal.Stop()のサンプルです |
1 change: 1 addition & 0 deletions examples/basic/signals/examples.go
Original file line number Diff line number Diff line change
@@ -15,4 +15,5 @@ func NewRegister() mapping.Register {
func (r *register) Regist(m mapping.ExampleMapping) {
m["signal_notify"] = Notify
m["signal_notify_context"] = NotifyContext
m["signal_stop"] = Stop
}
75 changes: 75 additions & 0 deletions examples/basic/signals/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package signals

import (
"log"
"os"
"os/signal"
"syscall"
"time"
)

// Stop は、signal.Stop()のサンプルです.
//
// signal.Notify()などでハンドル処理を追加した場合
// ファイルの場合と同様に defer で signal.Stop() を呼ぶべき。
//
// > Stop causes package signal to stop relaying incoming signals to c.
// It undoes the effect of all prior calls to Notify using c. When Stop returns, it is guaranteed that c will receive no more signals.
//
// > Stopは、パッケージ・シグナルがcへの受信シグナルのリレーを停止させる。
// ストップが戻れば、cはそれ以上シグナルを受け取らないことが保証される。
//
// # REFERENCES
// - https://pkg.go.dev/os/[email protected]#Stop
func Stop() error {
var (
l = log.New(os.Stderr, "", log.Lmicroseconds)
pid = os.Getpid()
sigs = make(chan os.Signal, 1)

sendSig = func() {
l.Println(">> SIGUSR1")
_ = syscall.Kill(pid, syscall.SIGUSR1)
}
stopSig = func() {
l.Println(">> signal.Stop")
signal.Stop(sigs)
}
)
defer close(sigs)

signal.Notify(sigs, syscall.SIGUSR1, syscall.SIGUSR2)
go func() {
l.Printf("Signal recv: %v\n", <-sigs)
}()

l.Println("<START>")
{
sendSig()
stopSig()
sendSig()
}
time.Sleep(1 * time.Second)
l.Println("< END >")

/*
$ task
task: [build] go build -o "/workspaces/try-golang/try-golang" .
task: [run] ./try-golang -onetime
ENTER EXAMPLE NAME: signal_stop
[Name] "signal_stop"
06:03:32.489167 <START>
06:03:32.489314 >> SIGUSR1
06:03:32.489326 >> signal.Stop
06:03:32.489444 >> SIGUSR1
06:03:32.489442 Signal recv: user defined signal 1
06:03:33.489539 < END >
[Elapsed] 1.000858245s
*/

return nil
}