59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
|
package core
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
pb "git.wntrmute.dev/kyle/overpush/proto"
|
||
|
"github.com/gregdel/pushover"
|
||
|
)
|
||
|
|
||
|
// MessageBuffer is how many messages to back up. This represents a
|
||
|
// balance between expected usage and anticipated spikes. It is
|
||
|
// entirely empirical as I don't have data to drive this decision.
|
||
|
const MessageBuffer = 16
|
||
|
|
||
|
type Account struct {
|
||
|
p *pushover.Pushover
|
||
|
nextTransmit time.Time
|
||
|
q chan pb.Message
|
||
|
lock *sync.Mutex
|
||
|
}
|
||
|
|
||
|
func NewAccount(token string) *Account {
|
||
|
return &Account{
|
||
|
p: pushover.New(token),
|
||
|
q: make(chan pb.Message, MessageBuffer),
|
||
|
lock: &sync.Mutex{},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (a *Account) UpdateLimit(limit *pushover.Limit) {
|
||
|
a.lock.Lock()
|
||
|
if limit.Remaining > 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
a.nextTransmit = limit.NextReset.Add(time.Second)
|
||
|
}
|
||
|
|
||
|
func (a *Account) Receive() {
|
||
|
for {
|
||
|
for {
|
||
|
if a.nextTransmit.After(time.Now()) {
|
||
|
sleep := time.Until(a.nextTransmit)
|
||
|
time.Sleep(sleep)
|
||
|
} else {
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
m, closed := <-a.q
|
||
|
if closed {
|
||
|
panic("channel closed")
|
||
|
}
|
||
|
|
||
|
fmt.Println(m.String())
|
||
|
}
|
||
|
}
|