diff --git a/authentication_queue.go b/authentication_queue.go index 0348670..39813bd 100644 --- a/authentication_queue.go +++ b/authentication_queue.go @@ -4,25 +4,34 @@ package main import ( "math/rand" + "sync" "time" "github.com/oklog/ulid" ) -type AuthenticationQueue struct { - requestMap map[string]*AuthRequest - requestChan chan *AuthRequest - tokenChan chan *AuthToken - app *App +type AuthenticationMap struct { + requestMap map[string]*AuthRequest + app *App + requestMapMutex *sync.RWMutex } -func (q *AuthenticationQueue) QueueRequest(r *AuthRequest) { - q.requestChan <- r +// Returns empty string when no request by that id is present +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 -// AuthenticationQueue.requestChan to be thread safe -func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error { +func (q *AuthenticationMap) QueueRequest(r *AuthRequest) error { t := time.Unix(1000000, 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() + q.requestMapMutex.Lock() q.requestMap[ID] = r + q.requestMapMutex.Unlock() (*q.app.Logger).Info("Queued request by id: ", ID) return nil } -func (q *AuthenticationQueue) Init() { +func (q *AuthenticationMap) 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() { +func (q *AuthenticationMap) startRequestMapCleaner() { go func() { - for _ = range time.Tick(time.Second) { + for _ = range time.Tick(10 * time.Minute) { + q.requestMapMutex.Lock() + for key, req := range q.requestMap { // If the request has been queued for more than 30 // 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() } }() } diff --git a/main.go b/main.go index 27d4e0c..aa7616b 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,8 @@ var ( ) type App struct { - AuthQueue *AuthenticationQueue - Logger *echo.Logger + AuthMap *AuthenticationMap + Logger *echo.Logger } func (a *App) authRequestWebSocket(c echo.Context) error { @@ -39,7 +39,7 @@ func (a *App) authRequestWebSocket(c echo.Context) error { (*a.Logger).Info("Queueing AuthRequest") - a.AuthQueue.QueueRequest(req) + a.AuthMap.QueueRequest(req) return nil } @@ -55,13 +55,13 @@ func main() { Logger: &e.Logger, } - q := &AuthenticationQueue{ + m := &AuthenticationMap{ app: a, } - q.Init() + m.Init() - a.AuthQueue = q + a.AuthMap = m e.GET("/ws", a.authRequestWebSocket) e.Logger.Fatal(e.Start(":1323"))