You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2017/12/28 09:22:00 UTC

[GitHub] little-cui closed pull request #236: SCB-127 Optimize util.go

little-cui closed pull request #236: SCB-127 Optimize util.go
URL: https://github.com/apache/incubator-servicecomb-service-center/pull/236
 
 
   

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/pkg/util/util.go b/pkg/util/util.go
index fc950a4..17997a6 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -185,6 +185,30 @@ func ParseTargetProject(ctx context.Context) string {
 	return v
 }
 
+func SetDomain(ctx context.Context, domain string) context.Context {
+	return SetContext(ctx, "domain", domain)
+}
+
+func SetProject(ctx context.Context, project string) context.Context {
+	return SetContext(ctx, "project", project)
+}
+
+func SetTargetDomain(ctx context.Context, domain string) context.Context {
+	return SetContext(ctx, "target-domain", domain)
+}
+
+func SetTargetProject(ctx context.Context, project string) context.Context {
+	return SetContext(ctx, "target-project", project)
+}
+
+func SetDomainProject(ctx context.Context, domain string, project string) context.Context {
+	return SetProject(SetDomain(ctx, domain), project)
+}
+
+func SetTargetDomainProject(ctx context.Context, domain string, project string) context.Context {
+	return SetTargetProject(SetTargetDomain(ctx, domain), project)
+}
+
 func GetIPFromContext(ctx context.Context) string {
 	v, ok := FromContext(ctx, "x-remote-ip").(string)
 	if !ok {
diff --git a/server/core/microservice.go b/server/core/microservice.go
index d42ec00..3cd8af6 100644
--- a/server/core/microservice.go
+++ b/server/core/microservice.go
@@ -70,10 +70,9 @@ func init() {
 }
 
 func AddDefaultContextValue(ctx context.Context) context.Context {
-	ctx = util.SetContext(ctx, "domain", REGISTRY_DOMAIN)
-	ctx = util.SetContext(ctx, "project", REGISTRY_PROJECT)
-	ctx = util.SetContext(ctx, IS_SC_SELF, true)
-	return ctx
+	return util.SetContext(
+		util.SetDomainProject(ctx, REGISTRY_DOMAIN, REGISTRY_PROJECT),
+		IS_SC_SELF, true)
 }
 
 func IsDefaultDomainProject(domainProject string) bool {
diff --git a/server/govern/govern_suite_test.go b/server/govern/govern_suite_test.go
index e300716..b8063c3 100644
--- a/server/govern/govern_suite_test.go
+++ b/server/govern/govern_suite_test.go
@@ -48,9 +48,7 @@ var _ = BeforeSuite(func() {
 })
 
 func getContext() context.Context {
-	ctx := context.TODO()
-	ctx = util.SetContext(ctx, "domain", "default")
-	ctx = util.SetContext(ctx, "project", "default")
-	ctx = util.SetContext(ctx, "noCache", "1")
-	return ctx
+	return util.SetContext(
+		util.SetDomainProject(context.Background(), "default", "default"),
+		"noCache", "1")
 }
diff --git a/server/handler/context/v3.go b/server/handler/context/v3.go
index 2be4e91..c4c10d9 100644
--- a/server/handler/context/v3.go
+++ b/server/handler/context/v3.go
@@ -34,8 +34,10 @@ func (v *v3Context) IsMatch(r *http.Request) bool {
 func (v *v3Context) Do(r *http.Request) error {
 	ctx := r.Context()
 
-	if len(util.ParseDomain(ctx)) == 0 {
-		domain := r.Header.Get("X-Tenant-Name")
+	domain, project := util.ParseDomain(ctx), util.ParseProject(ctx)
+
+	if len(domain) == 0 {
+		domain = r.Header.Get("X-Tenant-Name")
 		if len(domain) == 0 {
 			domain = r.Header.Get("X-Domain-Name")
 		}
@@ -45,11 +47,12 @@ func (v *v3Context) Do(r *http.Request) error {
 			util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
 			return err
 		}
-		util.SetRequestContext(r, "domain", domain)
+		util.SetDomain(r.Context(), domain)
 	}
 
-	if len(util.ParseProject(ctx)) == 0 {
-		util.SetRequestContext(r, "project", core.REGISTRY_PROJECT)
+	if len(project) == 0 {
+		util.SetProject(r.Context(), core.REGISTRY_PROJECT)
 	}
+
 	return nil
 }
diff --git a/server/handler/context/v4.go b/server/handler/context/v4.go
index 5b13495..7fea2c2 100644
--- a/server/handler/context/v4.go
+++ b/server/handler/context/v4.go
@@ -21,7 +21,6 @@ import (
 	"github.com/apache/incubator-servicecomb-service-center/pkg/util"
 	"github.com/apache/incubator-servicecomb-service-center/server/core"
 	"net/http"
-	"net/url"
 	"strings"
 )
 
@@ -35,35 +34,25 @@ func (v *v4Context) IsMatch(r *http.Request) bool {
 func (v *v4Context) Do(r *http.Request) error {
 	ctx := r.Context()
 
-	if len(util.ParseProject(ctx)) == 0 {
-		path, err := url.PathUnescape(r.RequestURI)
-		if err != nil {
-			util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
-			return err
-		}
+	domain, project := util.ParseDomain(ctx), util.ParseProject(ctx)
 
-		util.SetRequestContext(r, "project", v.parseProjectFromPath(path))
-	}
-
-	if len(util.ParseDomain(ctx)) == 0 {
-		domain := r.Header.Get("X-Domain-Name")
+	if len(domain) == 0 {
+		domain = r.Header.Get("X-Domain-Name")
 		if len(domain) == 0 {
 			err := errors.New("Header does not contain domain.")
 			util.Logger().Errorf(err, "Invalid Request URI %s", r.RequestURI)
 			return err
 		}
-		util.SetRequestContext(r, "domain", domain)
+		util.SetDomain(r.Context(), domain)
 	}
-	return nil
-}
-
-func (v *v4Context) parseProjectFromPath(path string) string {
-	start := len("/v4/")
-	end := start + strings.Index(path[start:], "/")
 
-	project := strings.TrimSpace(path[start:end])
 	if len(project) == 0 {
-		project = core.REGISTRY_PROJECT
+		project = r.URL.Query().Get(":project")
+		if len(project) == 0 {
+			project = core.REGISTRY_PROJECT
+		}
+		util.SetProject(r.Context(), project)
 	}
-	return project
+
+	return nil
 }
diff --git a/server/rest/controller/v4/instance_controller.go b/server/rest/controller/v4/instance_controller.go
index 0f23320..92631e5 100644
--- a/server/rest/controller/v4/instance_controller.go
+++ b/server/rest/controller/v4/instance_controller.go
@@ -131,13 +131,7 @@ func (this *MicroServiceInstanceService) FindInstances(w http.ResponseWriter, r
 		Tags:              ids,
 	}
 
-	targetDomain := r.Header.Get("X-Domain-Name")
-	targetProject := r.URL.Query().Get(":project")
-	if len(targetProject) == 0 {
-		targetProject = core.REGISTRY_PROJECT
-	}
-	util.SetRequestContext(r, "target-domain", targetDomain)
-	util.SetRequestContext(r, "target-project", targetProject)
+	util.SetTargetDomainProject(r.Context(), r.Header.Get("X-Domain-Name"), r.URL.Query().Get(":project"))
 
 	resp, _ := core.InstanceAPI.Find(r.Context(), request)
 	respInternal := resp.Response
diff --git a/server/service/event/instance_event_handler.go b/server/service/event/instance_event_handler.go
index 4d44e7c..0e0c02c 100644
--- a/server/service/event/instance_event_handler.go
+++ b/server/service/event/instance_event_handler.go
@@ -54,9 +54,7 @@ func (h *InstanceEventHandler) OnEvent(evt *store.KvEvent) {
 		if len(splited) == 2 && !apt.IsDefaultDomainProject(domainProject) {
 			domainName := splited[0]
 			projectName := splited[1]
-			ctx := context.TODO()
-			ctx = util.SetContext(ctx, "domain", domainName)
-			ctx = util.SetContext(ctx, "project", projectName)
+			ctx := util.SetDomainProject(context.Background(), domainName, projectName)
 			serviceUtil.RemandInstanceQuota(ctx)
 		}
 	}
diff --git a/server/service/instances.go b/server/service/instances.go
index 8160f7e..6a52625 100644
--- a/server/service/instances.go
+++ b/server/service/instances.go
@@ -434,11 +434,9 @@ func (s *InstanceService) GetOneInstance(ctx context.Context, in *pb.GetOneInsta
 	}
 	conPro := util.StringJoin([]string{in.ConsumerServiceId, in.ProviderServiceId, in.ProviderInstanceId}, "/")
 
-	targetDomainProject := util.ParseTargetDomainProject(ctx)
-
 	serviceId := in.ProviderServiceId
 	instanceId := in.ProviderInstanceId
-	instance, err := serviceUtil.GetInstance(ctx, targetDomainProject, serviceId, instanceId)
+	instance, err := serviceUtil.GetInstance(ctx, util.ParseTargetDomainProject(ctx), serviceId, instanceId)
 	if err != nil {
 		util.Logger().Errorf(err, "get instance failed, %s(consumer/provider): get instance failed.", conPro)
 		return &pb.GetOneInstanceResponse{
@@ -522,9 +520,7 @@ func (s *InstanceService) GetInstances(ctx context.Context, in *pb.GetInstancesR
 	}
 	conPro := util.StringJoin([]string{in.ConsumerServiceId, in.ProviderServiceId}, "/")
 
-	targetDomainProject := util.ParseTargetDomainProject(ctx)
-
-	instances, err := serviceUtil.GetAllInstancesOfOneService(ctx, targetDomainProject, in.ProviderServiceId)
+	instances, err := serviceUtil.GetAllInstancesOfOneService(ctx, util.ParseTargetDomainProject(ctx), in.ProviderServiceId)
 	if err != nil {
 		util.Logger().Errorf(err, "get instances failed, %s(consumer/provider): get instances from etcd failed.", conPro)
 		return &pb.GetInstancesResponse{
@@ -577,8 +573,7 @@ func (s *InstanceService) Find(ctx context.Context, in *pb.FindInstancesRequest)
 	} else {
 		// provider is not a shared micro-service,
 		// only allow shared micro-service instances found in different domains.
-		util.SetContext(ctx, "target-domain", util.ParseDomain(ctx))
-		util.SetContext(ctx, "target-project", util.ParseProject(ctx))
+		util.SetTargetDomainProject(ctx, util.ParseDomain(ctx), util.ParseProject(ctx))
 		provider.Tenant = util.ParseTargetDomainProject(ctx)
 		findFlag += "(" + provider.Environment + " services of the same domain)"
 	}
diff --git a/server/service/service_suite_test.go b/server/service/service_suite_test.go
index 8bce1f0..e23ad93 100644
--- a/server/service/service_suite_test.go
+++ b/server/service/service_suite_test.go
@@ -39,11 +39,9 @@ var _ = BeforeSuite(func() {
 })
 
 func getContext() context.Context {
-	ctx := context.TODO()
-	ctx = util.SetContext(ctx, "domain", "default")
-	ctx = util.SetContext(ctx, "project", "default")
-	ctx = util.SetContext(ctx, "noCache", "1")
-	return ctx
+	return util.SetContext(
+		util.SetDomainProject(context.Background(), "default", "default"),
+		"noCache", "1")
 }
 
 func TestGrpc(t *testing.T) {


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services