Commit c9591a2 1 parent f75dfb0 commit c9591a2 Copy full SHA for c9591a2
File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ package helpisu
2
+
3
+ import (
4
+ "database/sql"
5
+ "log"
6
+ "time"
7
+ )
8
+
9
+ // WaitDBStartUp DBの起動を待機
10
+ func WaitDBStartUp (db * sql.DB ) {
11
+ for {
12
+ err := db .Ping ()
13
+ if err == nil {
14
+ break
15
+ }
16
+
17
+ time .Sleep (time .Second * 2 )
18
+ }
19
+ }
20
+
21
+ // DBDisconnectDetector DBから切断されるとアプリを強制終了する検出器
22
+ type DBDisconnectDetector struct {
23
+ t * Ticker
24
+ }
25
+
26
+ /*
27
+ NewDBDisconnectDetector 新たなDBDisconnectDetectorを作成
28
+ durationSecは接続確認の実行間隔をs単位で指定して下さい
29
+ */
30
+ func NewDBDisconnectDetector (durationSec int , db * sql.DB ) * DBDisconnectDetector {
31
+ return & DBDisconnectDetector {
32
+ t : NewTicker (durationSec * 1000 , func () {
33
+ err := db .Ping ()
34
+ if err != nil {
35
+ log .Panic ("DB disconnected" )
36
+ }
37
+ }),
38
+ }
39
+ }
40
+
41
+ /*
42
+ Start DBからの切断の検出を開始
43
+ 必ずGoroutineとして実行して下さい
44
+ */
45
+ func (d * DBDisconnectDetector ) Start () {
46
+ d .t .Start ()
47
+ }
48
+
49
+ // Stop DBからの切断の検出を停止
50
+ func (d * DBDisconnectDetector ) Stop () {
51
+ d .t .Stop ()
52
+ }
53
+
54
+ // Reset 確認タイミングをリセット
55
+ func (d * DBDisconnectDetector ) Reset () {
56
+ d .t .Reset ()
57
+ }
You can’t perform that action at this time.
0 commit comments