diff --git a/auth_request.go b/auth_request.go index a68bbd8..27b267f 100644 --- a/auth_request.go +++ b/auth_request.go @@ -1,9 +1,14 @@ package main +import "time" + type AuthRequest struct { Client *AuthRequestClient Instance string ID string + + // Indicates when the request was queued for processing + Queued time.Time } func (r *AuthRequest) SetId(ID string) error { @@ -13,3 +18,7 @@ func (r *AuthRequest) SetId(ID string) error { return err } + +func (r *AuthRequest) Cancel() { + r.Client.Close() +} diff --git a/auth_request_client.go b/auth_request_client.go index 07b3292..955ef3d 100644 --- a/auth_request_client.go +++ b/auth_request_client.go @@ -88,3 +88,7 @@ func (c *AuthRequestClient) PropagateID(ID string) error { return err } + +func (c *AuthRequestClient) Close() { + c.conn.Close() +} diff --git a/authentication_queue.go b/authentication_queue.go index 98c414b..0348670 100644 --- a/authentication_queue.go +++ b/authentication_queue.go @@ -35,6 +35,8 @@ func (q *AuthenticationQueue) queueRequest(r *AuthRequest) error { return err } + r.Queued = time.Now() + q.requestMap[ID] = r (*q.app.Logger).Info("Queued request by id: ", ID) @@ -47,6 +49,25 @@ func (q *AuthenticationQueue) Init() { 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() {