forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqldb.go
99 lines (82 loc) · 1.63 KB
/
sqldb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package db
import (
"fmt"
"time"
)
type SQLDB struct {
conn Conn
lockFactory LockFactory
bus *notificationsBus
buildFactory *buildFactory
}
func NewSQL(
sqldbConnection Conn,
bus *notificationsBus,
lockFactory LockFactory,
) *SQLDB {
return &SQLDB{
conn: sqldbConnection,
lockFactory: lockFactory,
bus: bus,
buildFactory: newBuildFactory(sqldbConnection, bus, lockFactory),
}
}
type nonOneRowAffectedError struct {
RowsAffected int64
}
func (err nonOneRowAffectedError) Error() string {
return fmt.Sprintf("expected 1 row to be updated; got %d", err.RowsAffected)
}
type scannable interface {
Scan(destinations ...interface{}) error
}
type conditionNotifier struct {
cond func() (bool, error)
bus *notificationsBus
channel string
notified chan bool
notify chan struct{}
stop chan struct{}
}
func (notifier *conditionNotifier) Notify() <-chan struct{} {
return notifier.notify
}
func (notifier *conditionNotifier) Close() error {
close(notifier.stop)
return notifier.bus.Unlisten(notifier.channel, notifier.notified)
}
func (notifier *conditionNotifier) watch() {
for {
c, err := notifier.cond()
if err != nil {
select {
case <-time.After(5 * time.Second):
continue
case <-notifier.stop:
return
}
}
if c {
notifier.sendNotification()
}
dance:
for {
select {
case <-notifier.stop:
return
case ok := <-notifier.notified:
if ok {
notifier.sendNotification()
} else {
break dance
}
}
}
}
}
func (notifier *conditionNotifier) sendNotification() {
select {
case notifier.notify <- struct{}{}:
default:
}
}