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.
64 lines
1.3 KiB
Go
64 lines
1.3 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
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
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)
|
|
}
|
|
}()
|
|
}
|