|
|
@ -4,25 +4,34 @@ package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/oklog/ulid"
|
|
|
|
"github.com/oklog/ulid"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type AuthenticationQueue struct {
|
|
|
|
type AuthenticationMap struct {
|
|
|
|
requestMap map[string]*AuthRequest
|
|
|
|
requestMap map[string]*AuthRequest
|
|
|
|
requestChan chan *AuthRequest
|
|
|
|
app *App
|
|
|
|
tokenChan chan *AuthToken
|
|
|
|
requestMapMutex *sync.RWMutex
|
|
|
|
app *App
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (q *AuthenticationQueue) QueueRequest(r *AuthRequest) {
|
|
|
|
// Returns empty string when no request by that id is present
|
|
|
|
q.requestChan <- r
|
|
|
|
func (q *AuthenticationMap) getRequestByID(ID string) *AuthRequest {
|
|
|
|
|
|
|
|
q.requestMapMutex.RLock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
req, ok := q.requestMap[ID]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
q.requestMapMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Should be executed by the goroutine that watches
|
|
|
|
func (q *AuthenticationMap) QueueRequest(r *AuthRequest) error {
|
|
|
|
// AuthenticationQueue.requestChan to be thread safe
|
|
|
|
|
|
|
|
func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error {
|
|
|
|
|
|
|
|
t := time.Unix(1000000, 0)
|
|
|
|
t := time.Unix(1000000, 0)
|
|
|
|
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
|
|
|
|
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
|
|
|
|
|
|
|
|
|
|
|
@ -37,24 +46,25 @@ func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error {
|
|
|
|
|
|
|
|
|
|
|
|
r.Queued = time.Now()
|
|
|
|
r.Queued = time.Now()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
q.requestMapMutex.Lock()
|
|
|
|
q.requestMap[ID] = r
|
|
|
|
q.requestMap[ID] = r
|
|
|
|
|
|
|
|
q.requestMapMutex.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
(*q.app.Logger).Info("Queued request by id: ", ID)
|
|
|
|
(*q.app.Logger).Info("Queued request by id: ", ID)
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (q *AuthenticationQueue) Init() {
|
|
|
|
func (q *AuthenticationMap) Init() {
|
|
|
|
q.requestMap = make(map[string]*AuthRequest)
|
|
|
|
q.requestMap = make(map[string]*AuthRequest)
|
|
|
|
q.requestChan = make(chan *AuthRequest)
|
|
|
|
|
|
|
|
q.tokenChan = make(chan *AuthToken)
|
|
|
|
|
|
|
|
q.startRequestChanListener()
|
|
|
|
|
|
|
|
q.startRequestMapCleaner()
|
|
|
|
q.startRequestMapCleaner()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (q *AuthenticationQueue) startRequestMapCleaner() {
|
|
|
|
func (q *AuthenticationMap) startRequestMapCleaner() {
|
|
|
|
go func() {
|
|
|
|
go func() {
|
|
|
|
for _ = range time.Tick(time.Second) {
|
|
|
|
for _ = range time.Tick(10 * time.Minute) {
|
|
|
|
|
|
|
|
q.requestMapMutex.Lock()
|
|
|
|
|
|
|
|
|
|
|
|
for key, req := range q.requestMap {
|
|
|
|
for key, req := range q.requestMap {
|
|
|
|
// If the request has been queued for more than 30
|
|
|
|
// If the request has been queued for more than 30
|
|
|
|
// minutes, it's time to delete it.
|
|
|
|
// minutes, it's time to delete it.
|
|
|
@ -66,19 +76,8 @@ func (q *AuthenticationQueue) startRequestMapCleaner() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
q.requestMapMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|