You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.8 KiB
Go

package main
// Queue for pending authentication requests
import (
"math/rand"
"time"
"github.com/oklog/ulid"
)
type AuthenticationQueue struct {
requestMap map[string]*AuthRequest
requestChan chan *AuthRequest
tokenChan chan *AuthToken
app *App
}
func (q *AuthenticationQueue) QueueRequest(r *AuthRequest) {
q.requestChan <- r
}
// Should be executed by the goroutine that watches
// AuthenticationQueue.requestChan to be thread safe
func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error {
t := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
ulidID := ulid.MustNew(ulid.Timestamp(t), entropy)
ID := ulidID.String()
err := r.SetId(ID)
if err != nil {
return err
}
r.Queued = time.Now()
q.requestMap[ID] = r
(*q.app.Logger).Info("Queued request by id: ", ID)
return nil
}
func (q *AuthenticationQueue) Init() {
q.requestMap = make(map[string]*AuthRequest)
q.requestChan = make(chan *AuthRequest)
q.tokenChan = make(chan *AuthToken)
q.startRequestChanListener()
q.startRequestMapCleaner()
}
func (q *AuthenticationQueue) startRequestMapCleaner() {
go func() {
for _ = range time.Tick(time.Second) {
for key, req := range q.requestMap {
// If the request has been queued for more than 30
// minutes, it's time to delete it.
if req.Queued.Add(30 * time.Minute).Before(time.Now()) {
(*q.app.Logger).Info("Deleting inactive request with id: ", req.ID)
req.Cancel()
delete(q.requestMap, key)
}
}
}
}()
}
func (q *AuthenticationQueue) startRequestChanListener() {
go func() {
(*q.app.Logger).Info("Starting chan listener")
for {
var req *AuthRequest = <-q.requestChan
(*q.app.Logger).Info("Received request to be queued: ")
q.queueRequest(req)
}
}()
}