[ 
https://issues.apache.org/jira/browse/SCB-155?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16308141#comment-16308141
 ] 

ASF GitHub Bot commented on SCB-155:
------------------------------------

asifdxtreme closed pull request #239: SCB-155 SC panic when report metrics 
concurrently.
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/239
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/server/bootstrap/bootstrap.go b/server/bootstrap/bootstrap.go
index f7d705c..c32acb4 100644
--- a/server/bootstrap/bootstrap.go
+++ b/server/bootstrap/bootstrap.go
@@ -48,6 +48,7 @@ import (
        
"github.com/apache/incubator-servicecomb-service-center/server/handler/auth"
        
"github.com/apache/incubator-servicecomb-service-center/server/handler/cache"
        
"github.com/apache/incubator-servicecomb-service-center/server/handler/context"
+       
"github.com/apache/incubator-servicecomb-service-center/server/handler/metric"
        
"github.com/apache/incubator-servicecomb-service-center/server/interceptor"
        
"github.com/apache/incubator-servicecomb-service-center/server/interceptor/access"
        
"github.com/apache/incubator-servicecomb-service-center/server/interceptor/cors"
@@ -57,10 +58,13 @@ import (
 func init() {
        util.Logger().Info("BootStrap ServiceComb.io Edition")
 
+       // intercept requests before routing.
        interceptor.RegisterInterceptFunc(access.Intercept)
        interceptor.RegisterInterceptFunc(ratelimiter.Intercept)
        interceptor.RegisterInterceptFunc(cors.Intercept)
 
+       // handle requests after routing.
+       metric.RegisterHandlers()
        auth.RegisterHandlers()
        context.RegisterHandlers()
        cache.RegisterHandlers()
diff --git a/server/handler/auth/handler.go b/server/handler/auth/auth.go
similarity index 100%
rename from server/handler/auth/handler.go
rename to server/handler/auth/auth.go
diff --git a/server/handler/metric/handler.go b/server/handler/metric/handler.go
new file mode 100644
index 0000000..5b54d2f
--- /dev/null
+++ b/server/handler/metric/handler.go
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package metric
+
+import (
+       "github.com/apache/incubator-servicecomb-service-center/pkg/chain"
+       "github.com/apache/incubator-servicecomb-service-center/pkg/rest"
+       "github.com/apache/incubator-servicecomb-service-center/pkg/util"
+       svr "github.com/apache/incubator-servicecomb-service-center/server/rest"
+       "net/http"
+       "time"
+)
+
+type MetricsHandler struct {
+}
+
+func (h *MetricsHandler) Handle(i *chain.Invocation) {
+       w, r := i.Context().Value(rest.CTX_RESPONSE).(http.ResponseWriter),
+               i.Context().Value(rest.CTX_REQUEST).(*http.Request)
+       cb := i.Func
+       i.Invoke(func(ret chain.Result) {
+               cb(ret)
+
+               start, ok := 
i.Context().Value(svr.CTX_START_TIMESTAMP).(time.Time)
+               if !ok {
+                       return
+               }
+               ReportRequestCompleted(w, r, start)
+               util.LogNilOrWarnf(start, "%s %s", r.Method, r.RequestURI)
+       })
+}
+
+func RegisterHandlers() {
+       chain.RegisterHandler(rest.SERVER_CHAIN_NAME, &MetricsHandler{})
+}
diff --git a/server/rest/metrics.go b/server/handler/metric/metric.go
similarity index 99%
rename from server/rest/metrics.go
rename to server/handler/metric/metric.go
index 3cdd09f..ff46d32 100644
--- a/server/rest/metrics.go
+++ b/server/handler/metric/metric.go
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package rest
+package metric
 
 import (
        "fmt"
diff --git a/server/rest/handler.go b/server/rest/handler.go
index 19ccb12..154ee8f 100644
--- a/server/rest/handler.go
+++ b/server/rest/handler.go
@@ -25,6 +25,8 @@ import (
        "time"
 )
 
+const CTX_START_TIMESTAMP = "x-start-timestamp"
+
 func init() {
        // api
        http.Handle("/", &ServerHandler{})
@@ -34,7 +36,7 @@ type ServerHandler struct {
 }
 
 func (s *ServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
-       start := time.Now()
+       util.SetRequestContext(r, CTX_START_TIMESTAMP, time.Now())
 
        err := interceptor.InvokeInterceptors(w, r)
        if err != nil {
@@ -43,7 +45,6 @@ func (s *ServerHandler) ServeHTTP(w http.ResponseWriter, r 
*http.Request) {
 
        roa.GetRouter().ServeHTTP(w, r)
 
-       ReportRequestCompleted(w, r, start)
-
-       util.LogNilOrWarnf(start, "%s %s", r.Method, r.RequestURI)
+       // CAUTION: There will be cause a concurrent problem,
+       // if here get/set the HTTP request headers.
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> SC panic when report metrics concurrently
> -----------------------------------------
>
>                 Key: SCB-155
>                 URL: https://issues.apache.org/jira/browse/SCB-155
>             Project: Apache ServiceComb
>          Issue Type: Bug
>          Components: Service-Center
>            Reporter: little-cui
>




--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to