You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by as...@apache.org on 2018/01/02 14:37:59 UTC

[incubator-servicecomb-service-center] branch master updated: SCB-155 SC panic when report metrics concurrently. (#239)

This is an automated email from the ASF dual-hosted git repository.

asifdxtreme pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new ce5b2fc  SCB-155 SC panic when report metrics concurrently. (#239)
ce5b2fc is described below

commit ce5b2fc1fd31f4b8297562609990cdeb7c7b2d3f
Author: little-cui <su...@qq.com>
AuthorDate: Tue Jan 2 22:37:57 2018 +0800

    SCB-155 SC panic when report metrics concurrently. (#239)
    
    * SCB-155 SC panic when report metrics concurrently.
    
    * SCB-155 SC panic when report metrics concurrently.
---
 server/bootstrap/bootstrap.go                      |  4 +++
 server/handler/auth/{handler.go => auth.go}        |  0
 server/handler/{auth => metric}/handler.go         | 37 +++++++++++-----------
 .../{rest/metrics.go => handler/metric/metric.go}  |  2 +-
 server/rest/handler.go                             |  9 +++---
 5 files changed, 28 insertions(+), 24 deletions(-)

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%
copy from server/handler/auth/handler.go
copy to server/handler/auth/auth.go
diff --git a/server/handler/auth/handler.go b/server/handler/metric/handler.go
similarity index 58%
rename from server/handler/auth/handler.go
rename to server/handler/metric/handler.go
index c01217d..5b54d2f 100644
--- a/server/handler/auth/handler.go
+++ b/server/handler/metric/handler.go
@@ -14,37 +14,36 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package auth
+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"
-	scerr "github.com/apache/incubator-servicecomb-service-center/server/error"
-	"github.com/apache/incubator-servicecomb-service-center/server/plugin"
-	"github.com/apache/incubator-servicecomb-service-center/server/rest/controller"
+	svr "github.com/apache/incubator-servicecomb-service-center/server/rest"
 	"net/http"
+	"time"
 )
 
-type AuthRequest struct {
+type MetricsHandler struct {
 }
 
-func (h *AuthRequest) Handle(i *chain.Invocation) {
-	r := i.Context().Value(rest.CTX_REQUEST).(*http.Request)
-	err := plugin.Plugins().Auth().Identify(r)
-	if err == nil {
-		i.Next()
-		return
-	}
+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)
 
-	util.Logger().Errorf(err, "authenticate request failed, %s %s", r.Method, r.RequestURI)
-
-	w := i.Context().Value(rest.CTX_RESPONSE).(http.ResponseWriter)
-	controller.WriteError(w, scerr.ErrUnauthorized, err.Error())
-
-	i.Fail(nil)
+		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, &AuthRequest{})
+	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.
 }

-- 
To stop receiving notification emails like this one, please contact
['"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>'].