1
0
Fork 0

Split sending into chained goroutines

This commit is contained in:
Michał Rudowicz 2024-02-18 01:44:10 +01:00
parent 8c8b7c4f9b
commit e776813230
3 changed files with 69 additions and 42 deletions

View File

@ -97,7 +97,8 @@ func main() {
logger.Print("Created Telegram Bot API client") logger.Print("Created Telegram Bot API client")
tgSender := TgSender{bot} tgSender := TgSender{bot}
go tgSenderWorker(tgEvents, tgSender, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)) Consume(SendToTg(tgSenderWorker(tgEvents, &wg, sleeper, log.New(os.Stderr, "TgSender", log.Lmicroseconds)),
tgSender, &wg, log.New(os.Stderr, "SendToTg", log.Lmicroseconds)))
for e := range FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)) { for e := range FilterByLastSeen(s.Events, "hs_wro_last_seen.bin", log.New(os.Stderr, "FilterByLastSeen", log.Lmicroseconds)) {
logger.Print("Received change from SATEL: ", e) logger.Print("Received change from SATEL: ", e)
sendTgMessage(tgEvents, fmt.Sprint("Change from SATEL: type:", e.Type, ", index:", e.Index, ", value:", e.Value), chatIds) sendTgMessage(tgEvents, fmt.Sprint("Change from SATEL: type:", e.Type, ", index:", e.Index, ", value:", e.Value), chatIds)

View File

@ -19,14 +19,44 @@ type Sleeper interface {
Sleep(ch chan<- interface{}) Sleep(ch chan<- interface{})
} }
func tgSenderWorker(tgEvents <-chan GenericMessage, s Sender, wg *sync.WaitGroup, sleeper Sleeper, logger *log.Logger) { func Consume(events <-chan GenericMessage) {
logger.Print("Starting") go func() {
for range events {
}
}()
}
func SendToTg(events <-chan GenericMessage, s Sender, wg *sync.WaitGroup, logger *log.Logger) <-chan GenericMessage {
returnEvents := make(chan GenericMessage)
go func() {
wg.Add(1) wg.Add(1)
defer wg.Done() defer wg.Done()
for e := range events {
returnEvents <- e
err := s.Send(e)
if err != nil {
// TODO: handle it better
panic(err)
}
}
close(returnEvents)
}()
return returnEvents
}
func tgSenderWorker(tgEvents <-chan GenericMessage, wg *sync.WaitGroup, sleeper Sleeper, logger *log.Logger) <-chan GenericMessage {
logger.Print("Starting")
messagesToSend := make(map[int64]*strings.Builder) messagesToSend := make(map[int64]*strings.Builder)
waitingStarted := false waitingStarted := false
timeoutEvents := make(chan interface{}) timeoutEvents := make(chan interface{})
returnEvents := make(chan GenericMessage)
go func() {
wg.Add(1)
defer wg.Done()
defer close(returnEvents)
loop: loop:
for { for {
select { select {
@ -50,11 +80,7 @@ loop:
logger.Print("Time's up, sending all messages we've got for now.") logger.Print("Time's up, sending all messages we've got for now.")
waitingStarted = false waitingStarted = false
for chatId, msgBuilder := range messagesToSend { for chatId, msgBuilder := range messagesToSend {
err := s.Send(GenericMessage{chatId, msgBuilder.String()}) returnEvents <- GenericMessage{chatId, msgBuilder.String()}
if err != nil {
// TODO: handle it better
panic(err)
}
delete(messagesToSend, chatId) delete(messagesToSend, chatId)
} }
} }
@ -63,10 +89,9 @@ loop:
logger.Print("Exiting") logger.Print("Exiting")
// If anything is left to be sent, send it now // If anything is left to be sent, send it now
for chatId, msgBuilder := range messagesToSend { for chatId, msgBuilder := range messagesToSend {
err := s.Send(GenericMessage{chatId, msgBuilder.String()}) returnEvents <- GenericMessage{chatId, msgBuilder.String()}
if err != nil {
// TODO: handle it better
panic(err)
}
} }
}()
return returnEvents
} }

View File

@ -35,7 +35,8 @@ func TestMessageThrottling(t *testing.T) {
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
mockSender := MockSender{make([]GenericMessage, 0)} mockSender := MockSender{make([]GenericMessage, 0)}
mockSleeper := MockSleeper{nil, 0} mockSleeper := MockSleeper{nil, 0}
go tgSenderWorker(testEvents, &mockSender, &wg, &mockSleeper, log.New(io.Discard, "", log.Ltime)) Consume(SendToTg(tgSenderWorker(testEvents, &wg, &mockSleeper, log.New(io.Discard, "", log.Ltime)),
&mockSender, &wg, log.New(io.Discard, "", log.Ltime)))
testEvents <- GenericMessage{123, "test1"} testEvents <- GenericMessage{123, "test1"}
testEvents <- GenericMessage{124, "test3"} testEvents <- GenericMessage{124, "test3"}
testEvents <- GenericMessage{123, "test2"} testEvents <- GenericMessage{123, "test2"}