Using golang's time.NewTicker, I am trying to run each record from a database (consisting of text and a timer, i.e the number of seconds before it repeats) but it is only repeating the first record it grabs
func LoadTimedCommands() map[string]time.Duration {
database := InitializeDB()
rows, _ := database.Query("SELECT TimedResponse, Timer from timedcommands")
com := make(map[string]time.Duration)
for rows.Next() {
var TimedResponse string
var Timer time.Duration
rows.Scan(&TimedResponse, &Timer)
com[TimedResponse] = Timer
}
return com
}
func TimedCommands(conn net.Conn, channel string, name string) {
timedcoms := LoadTimedCommands()
for k, v := range timedcoms {
for range time.NewTicker(v * time.Second).C {
BotSendMsg(conn, channel, k, name)
}
}
}
So if I have two records to use from: Sqlite data
Then the code should run the first record every 15 seconds, and the second every 10. But again, only the first one that is loaded into the map is run.
Your problem is in this function:
func TimedCommands(conn net.Conn, channel string, name string) {
timedcoms := LoadTimedCommands()
for k, v := range timedcoms {
for range time.NewTicker(v * time.Second).C {
BotSendMsg(conn, channel, k, name)
}
}
}
In this section, where you range
over the channel time.NewTicker(...).C
:
for range time.NewTicker(v * time.Second).C {
BotSendMsg(conn, channel, k, name)
}
This will try and read values from this one ticker until the channel C
is closed. This means that the outer loop only does one iteration, then we block on the inner loop until C
is closed. And we never create more than one Ticker
at once.
You could get around this by creating tickers in their own goroutines:
for k, v := range timedcoms {
go func(conn net.Conn, channel, name, k string, v time.Duration) {
for range time.NewTicker(v * time.Second).C {
BotSendMsg(conn, channel, k, name)
}
}(conn, channel, k, v)
}
But you will probably want to include some synchronisation to ensure that you can stop the goroutines that you start, or to ensure that BotSendMsg(...)
can be called from multiple goroutines at the same time etc.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments