Skip to content

Commit 792da0e

Browse files
committed
Fixed the memory leak while using gorilla/context.Set. Clear the global map object with current request after tasks api return or manually release all the expired objects in context using api of gc.
1 parent 4106939 commit 792da0e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

api/debug/gc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package debug
33
import (
44
"net/http"
55
"runtime"
6+
7+
"github.com/gorilla/context"
68
)
79

810
func GC(w http.ResponseWriter, r *http.Request) {
11+
context.Purge(600)
912
runtime.GC()
1013
w.WriteHeader(http.StatusNoContent)
1114
}

api/projects/tasks.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package projects
33
import (
44
"bytes"
55
"errors"
6+
"net/http"
7+
"strconv"
8+
"time"
9+
610
"github.com/gorilla/context"
711
"github.com/semaphoreui/semaphore/api/helpers"
812
"github.com/semaphoreui/semaphore/db"
913
"github.com/semaphoreui/semaphore/services/tasks"
1014
"github.com/semaphoreui/semaphore/util"
1115
log "github.com/sirupsen/logrus"
12-
"net/http"
13-
"strconv"
14-
"time"
1516
)
1617

1718
// AddTask inserts a task into the database and returns a header or returns error
@@ -102,6 +103,7 @@ func GetTask(w http.ResponseWriter, r *http.Request) {
102103
// GetTaskMiddleware is middleware that gets a task by id and sets the context to it or panics
103104
func GetTaskMiddleware(next http.Handler) http.Handler {
104105
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106+
defer context.Clear(r)
105107
project := context.Get(r, "project").(db.Project)
106108
taskID, err := helpers.GetIntParam("task_id", w, r)
107109

0 commit comments

Comments
 (0)