C O M P U T E R
This commit is contained in:
58
core/core.go
Normal file
58
core/core.go
Normal file
@@ -0,0 +1,58 @@
|
||||
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())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user