Clean the request map to prevent memory leaks

master
Hugo Thunnissen 4 years ago
parent b59a344286
commit 587d0123d6

@ -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()
}

@ -88,3 +88,7 @@ func (c *AuthRequestClient) PropagateID(ID string) error {
return err
}
func (c *AuthRequestClient) Close() {
c.conn.Close()
}

@ -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() {

Loading…
Cancel
Save