Skip to content

Commit 9480318

Browse files
authored
Merge pull request #794 from devlights/add-cancallable-sleep
2 parents e867142 + 88427d8 commit 9480318

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

examples/basic/times/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
| time_format_microsecond.go | time_format_microsecond | time.Format() にてマイクロ秒を出力するサンプルす。 |
2727
| time_calc_nextmonth.go | time_calc_nextmonth | 翌月の日付を求めるサンプルです |
2828
| time_daysinmonth.go | time_daysinmonth | 月の日数を求めるサンプルです |
29+
| time_sleep.go | time_sleep | time.Sleep() のサンプルです。 |
30+
| time_cancellable_sleep.go | time_cancellable_sleep | キャンセル可能なスリープ処理のサンプルです。 |

examples/basic/times/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ func (r *register) Regist(m mapping.ExampleMapping) {
3636
m["time_format_microsecond"] = FormatMicrosecond
3737
m["time_calc_nextmonth"] = CalcNextMonth
3838
m["time_daysinmonth"] = DaysInMonth
39+
m["time_sleep"] = Sleep
40+
m["time_cancellable_sleep"] = CancellableSleep
3941
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package times
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/devlights/gomy/output"
8+
)
9+
10+
// CancellableSleep は、キャンセル可能なスリープ処理のサンプルです。
11+
//
12+
// time.Sleep() は、ブロックしてしまうためキャンセル可能な状態で
13+
// スリープ処理を行いたい場合は、time.Tickerを利用して処理する。
14+
//
15+
// # REFERENCES
16+
// - https://pkg.go.dev/time@go1.22.2#NewTicker
17+
func CancellableSleep() error {
18+
const (
19+
timeFormat = time.TimeOnly + ".000"
20+
)
21+
22+
var (
23+
ctx, cxl = context.WithTimeout(context.Background(), 3500*time.Millisecond)
24+
ticker = time.NewTimer(5 * time.Second)
25+
)
26+
defer cxl()
27+
defer ticker.Stop()
28+
29+
output.Stdoutl("[begin]", time.Now().Format(timeFormat))
30+
defer func() { output.Stdoutl("[end ]", time.Now().Format(timeFormat)) }()
31+
32+
//
33+
// 以下は5秒間スリープする処理があるとして、3.5秒でキャンセルします。
34+
// time.Sleep(5 * time.Second) とすると 5秒間 ブロックされますが
35+
// context.Context と *time.Ticker.C を利用してselectで待機するようにしているため
36+
// 先に完了した方で抜けていきます。
37+
//
38+
select {
39+
case <-ctx.Done():
40+
output.Stdoutl("[cancel]", ctx.Err())
41+
case <-ticker.C:
42+
output.Stdoutl("[sleep]", "done")
43+
}
44+
45+
return nil
46+
47+
/*
48+
$ task
49+
task: [build] go build .
50+
task: [run] ./try-golang -onetime
51+
52+
ENTER EXAMPLE NAME: time_cancellable_sleep
53+
54+
[Name] "time_cancellable_sleep"
55+
[begin] 07:33:29.449
56+
[cancel] context deadline exceeded
57+
[end ] 07:33:32.951
58+
59+
60+
[Elapsed] 3.502395005s
61+
*/
62+
63+
}

examples/basic/times/time_sleep.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package times
2+
3+
import (
4+
"time"
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
// Sleep は、time.Sleep() のサンプルです。
10+
//
11+
// > Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.
12+
//
13+
// > スリープは、少なくとも継続時間dの間、現在のゴルーチンを一時停止します。継続時間が負またはゼロの場合、スリープは即座に戻ります。
14+
//
15+
// # REFERENCES
16+
// - https://pkg.go.dev/time@go1.22.2#Sleep
17+
func Sleep() error {
18+
const (
19+
timeFormat = time.TimeOnly + ".000"
20+
)
21+
22+
//
23+
// time.Sleep() は、他の言語のSleep関数と同じ挙動。
24+
// 指定した時間分、現在のスレッドをブロックする。
25+
//
26+
output.Stdoutl("[begin]", time.Now().Format(timeFormat))
27+
defer func() { output.Stdoutl("[end]", time.Now().Format(timeFormat)) }()
28+
29+
time.Sleep(3 * time.Second)
30+
31+
return nil
32+
33+
/*
34+
$ task
35+
task: [build] go build .
36+
task: [run] ./try-golang -onetime
37+
38+
ENTER EXAMPLE NAME: time_sleep
39+
40+
[Name] "time_sleep"
41+
[begin] 07:32:51.959
42+
[end] 07:32:54.960
43+
44+
45+
[Elapsed] 3.000651935s
46+
*/
47+
48+
}

0 commit comments

Comments
 (0)