Have got asynchronous publishing working
This commit is contained in:
parent
3ea5823ca0
commit
4f7058bf36
9 changed files with 100 additions and 31 deletions
44
services/publisher/pqueue.go
Normal file
44
services/publisher/pqueue.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package publisher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
type Queue struct {
|
||||
publisher *Publisher
|
||||
pending chan models.Site
|
||||
}
|
||||
|
||||
func NewQueue(publisher *Publisher) *Queue {
|
||||
return &Queue{
|
||||
publisher: publisher,
|
||||
pending: make(chan models.Site, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Queue(site models.Site) bool {
|
||||
select {
|
||||
case q.pending <- site:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (q *Queue) Start(ctx context.Context) {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case site := <-q.pending:
|
||||
if err := q.publisher.Publish(ctx, site); err != nil {
|
||||
log.Printf("error publishing site: %v", err)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue