Timeout channel list updates

This commit is contained in:
Ken-Håvard Lieng 2019-01-25 11:24:57 +01:00
parent f8e12f5938
commit 9267c661dc

View File

@ -100,6 +100,7 @@ func (c chanList) Swap(i, j int) {
} }
const ChannelListUpdateInterval = time.Hour * 24 const ChannelListUpdateInterval = time.Hour * 24
const ChannelListUpdateTimeout = time.Minute * 5
type ChannelIndexManager struct { type ChannelIndexManager struct {
indexes map[string]*managedChannelIndex indexes map[string]*managedChannelIndex
@ -127,11 +128,13 @@ func (m *ChannelIndexManager) Get(server string) (ChannelListIndex, bool) {
m.indexes[server] = &managedChannelIndex{ m.indexes[server] = &managedChannelIndex{
updating: true, updating: true,
} }
go m.timeoutUpdate(server)
return nil, true return nil, true
} }
if !idx.updating && time.Since(idx.updatedAt) > ChannelListUpdateInterval { if !idx.updating && time.Since(idx.updatedAt) > ChannelListUpdateInterval {
idx.updating = true idx.updating = true
go m.timeoutUpdate(server)
return idx.index, true return idx.index, true
} }
@ -146,3 +149,13 @@ func (m *ChannelIndexManager) Set(server string, index ChannelListIndex) {
} }
m.lock.Unlock() m.lock.Unlock()
} }
func (m *ChannelIndexManager) timeoutUpdate(server string) {
time.Sleep(ChannelListUpdateTimeout)
m.lock.Lock()
if m.indexes[server].updating {
m.indexes[server].updating = false
}
m.lock.Unlock()
}