Skip to content

Commit bdb9607

Browse files
authored
fixed lock problem in functions module (#1279)
1 parent a693a9c commit bdb9607

File tree

6 files changed

+26
-24
lines changed

6 files changed

+26
-24
lines changed

gateway/modules/functions/helpers.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (m *Module) handleCall(ctx context.Context, serviceID, endpointID, token st
4949
url = endpointPath
5050

5151
case config.EndpointKindPrepared:
52-
url = fmt.Sprintf("http://localhost:4122/v1/api/%s/graphql", m.project)
52+
url = fmt.Sprintf("http://localhost:4122/v1/api/%s/graphql", m.getProject())
5353

5454
default:
5555
return http.StatusBadRequest, nil, helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Invalid endpoint kind (%s) provided", endpoint.Kind), nil, nil)
@@ -126,6 +126,9 @@ func prepareHeaders(ctx context.Context, headers config.Headers, state map[strin
126126
}
127127

128128
func (m *Module) adjustReqBody(ctx context.Context, serviceID, endpointID, token string, endpoint *config.Endpoint, auth, params interface{}) (interface{}, error) {
129+
m.lock.RLock()
130+
defer m.lock.RUnlock()
131+
129132
var req, graph interface{}
130133
var err error
131134

@@ -162,6 +165,9 @@ func (m *Module) adjustReqBody(ctx context.Context, serviceID, endpointID, token
162165
}
163166

164167
func (m *Module) adjustResBody(ctx context.Context, serviceID, endpointID, token string, endpoint *config.Endpoint, auth, params interface{}) (interface{}, error) {
168+
m.lock.RLock()
169+
defer m.lock.RUnlock()
170+
165171
var res interface{}
166172
var err error
167173

@@ -222,13 +228,23 @@ func loadParam(ctx context.Context, key string, claims, params interface{}) (str
222228
}
223229

224230
func (m *Module) loadService(service string) *config.Service {
231+
m.lock.RLock()
232+
defer m.lock.RUnlock()
233+
225234
if s, p := m.config.InternalServices[service]; p {
226235
return s
227236
}
228237

229238
return m.config.Services[service]
230239
}
231240

241+
func (m *Module) getProject() string {
242+
m.lock.RLock()
243+
defer m.lock.RUnlock()
244+
245+
return m.project
246+
}
247+
232248
func (m *Module) createGoTemplate(kind, serviceID, endpointID, tmpl string) error {
233249
key := getGoTemplateKey(kind, serviceID, endpointID)
234250

gateway/modules/functions/operations.go

+1-20
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,19 @@ package functions
22

33
import (
44
"context"
5-
"time"
65

76
"github.com/spaceuptech/space-cloud/gateway/config"
87
"github.com/spaceuptech/space-cloud/gateway/model"
98
)
109

11-
// Call simply calls a function on a service
12-
func (m *Module) Call(service, function, token string, reqParams model.RequestParams, params interface{}, timeout int) (int, interface{}, error) {
13-
m.lock.RLock()
14-
defer m.lock.RUnlock()
15-
16-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
17-
defer cancel()
18-
19-
status, result, err := m.handleCall(ctx, service, function, token, reqParams.Claims, params)
20-
if err != nil {
21-
return status, result, err
22-
}
23-
m.metricHook(m.project, service, function)
24-
return status, result, nil
25-
}
26-
2710
// CallWithContext invokes function on a service. The response from the function is returned back along with
2811
// any errors if they occurred.
2912
func (m *Module) CallWithContext(ctx context.Context, service, function, token string, reqParams model.RequestParams, params interface{}) (int, interface{}, error) {
30-
m.lock.RLock()
31-
defer m.lock.RUnlock()
32-
3313
status, result, err := m.handleCall(ctx, service, function, token, reqParams.Claims, params)
3414
if err != nil {
3515
return status, result, err
3616
}
17+
3718
m.metricHook(m.project, service, function)
3819
return status, result, nil
3920
}

gateway/server/middleware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func loggerMiddleWare(next http.Handler) http.Handler {
2525
r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
2626
}
2727

28-
helpers.Logger.LogInfo(requestID, "Request", map[string]interface{}{"url": r.URL.Path, "queryVars": r.URL.Query(), "body": string(reqBody)})
28+
helpers.Logger.LogInfo(requestID, "Request", map[string]interface{}{"method": r.Method, "url": r.URL.Path, "queryVars": r.URL.Query(), "body": string(reqBody)})
2929
next.ServeHTTP(w, r.WithContext(helpers.CreateContext(r)))
3030

3131
})

runner/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
github.com/opencontainers/image-spec v1.0.1 // indirect
2424
github.com/rs/cors v1.7.0
2525
github.com/segmentio/ksuid v1.0.2
26-
github.com/sirupsen/logrus v1.4.2 // indirect
26+
github.com/sirupsen/logrus v1.4.2
2727
github.com/spaceuptech/helpers v0.1.1
2828
github.com/spaceuptech/space-api-go v0.17.3
2929
github.com/txn2/txeh v1.3.0

runner/server/middlware.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func loggerMiddleWare(next http.Handler) http.Handler {
2525
r.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
2626
}
2727

28-
helpers.Logger.LogInfo(requestID, "Request", map[string]interface{}{"url": r.URL.Path, "queryVars": r.URL.Query(), "body": string(reqBody)})
28+
helpers.Logger.LogInfo(requestID, "Request", map[string]interface{}{"method": r.Method, "url": r.URL.Path, "queryVars": r.URL.Query(), "body": string(reqBody)})
2929
next.ServeHTTP(w, r.WithContext(helpers.CreateContext(r)))
3030

3131
})

runner/server/server.go

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010
"time"
1111

12+
"github.com/sirupsen/logrus"
1213
"github.com/spaceuptech/helpers"
1314

1415
"github.com/spaceuptech/space-cloud/runner/metrics"
@@ -68,6 +69,10 @@ func New(c *Config) (*Server, error) {
6869
debounce := utils.NewDebounce()
6970

7071
opts := badger.DefaultOptions("/tmp/runner.db")
72+
badgerLogger := logrus.New()
73+
badgerLogger.SetOutput(ioutil.Discard)
74+
opts.Logger = badgerLogger
75+
7176
// The default logger used by badger is log, so we are disabling all the logs done by log package
7277
log.SetOutput(ioutil.Discard)
7378
db, err := badger.Open(opts)

0 commit comments

Comments
 (0)