1
This commit is contained in:
parent
83692803ab
commit
71fae94866
78
dbpool.go
78
dbpool.go
@ -145,52 +145,9 @@ func (this *dbPool) LoopLoad(
|
||||
doCb func(*DataSet) bool) {
|
||||
newDataCond := sync.NewCond(new(sync.Mutex))
|
||||
chTimer := make(chan int64)
|
||||
go this.discoverNewData(dataSource, watchTable, watchTimeCb, newDataCond)
|
||||
go q5.CreateCondTimer(chTimer, newDataCond)
|
||||
|
||||
go func () {
|
||||
var lastMaxIdx int64
|
||||
for true {
|
||||
hasNewDdata := false
|
||||
this.RawQuery(
|
||||
dataSource,
|
||||
fmt.Sprintf("SELECT MAX(idx) AS max_idx FROM %s", watchTable),
|
||||
[]string{},
|
||||
func (err error, ds *DataSet) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if ds.Next() {
|
||||
idx := q5.ToInt64(ds.GetByName("max_idx"))
|
||||
if idx > lastMaxIdx {
|
||||
lastMaxIdx = idx
|
||||
hasNewDdata = true
|
||||
GetSysLog().Info("%s hasNewData max_idx:%d", watchTable, lastMaxIdx)
|
||||
}
|
||||
}
|
||||
})
|
||||
if hasNewDdata {
|
||||
newDataCond.Broadcast()
|
||||
} else {
|
||||
time.Sleep(time.Second * time.Duration(watchTimeCb()))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go func () {
|
||||
var waitSecond int64 = 10
|
||||
for {
|
||||
select {
|
||||
case waitSecond = <-chTimer:
|
||||
if waitSecond < 10 {
|
||||
waitSecond = 10
|
||||
}
|
||||
case <-time.After(time.Second * time.Duration(waitSecond)):
|
||||
waitSecond = 10
|
||||
newDataCond.Broadcast()
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
{
|
||||
var lastIdx int64
|
||||
for true {
|
||||
hasNextData := false
|
||||
@ -225,7 +182,6 @@ func (this *dbPool) LoopLoad(
|
||||
newDataCond.L.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *dbPool) SelectLike(
|
||||
dataSource string,
|
||||
@ -699,3 +655,33 @@ func (this *dbPool) queryOne(dataSource string, sql string, params []string,
|
||||
go this.internalQueryOne(dataSource, sql, params, cb)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *dbPool ) discoverNewData(dataSource string, watchTable string, watchTimeCb func() int64,
|
||||
cond *sync.Cond) {
|
||||
var lastMaxIdx int64
|
||||
for true {
|
||||
hasNewDdata := false
|
||||
this.RawQuery(
|
||||
dataSource,
|
||||
fmt.Sprintf("SELECT MAX(idx) AS max_idx FROM %s", watchTable),
|
||||
[]string{},
|
||||
func (err error, ds *DataSet) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if ds.Next() {
|
||||
idx := q5.ToInt64(ds.GetByName("max_idx"))
|
||||
if idx > lastMaxIdx {
|
||||
lastMaxIdx = idx
|
||||
hasNewDdata = true
|
||||
GetSysLog().Info("%s hasNewData max_idx:%d", watchTable, lastMaxIdx)
|
||||
}
|
||||
}
|
||||
})
|
||||
if hasNewDdata {
|
||||
cond.Broadcast()
|
||||
} else {
|
||||
time.Sleep(time.Second * time.Duration(watchTimeCb()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user