2 * Copyright 2020 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 "github.com/apache/servicecomb-service-center/pkg/util"
24 mgr "github.com/apache/servicecomb-service-center/server/plugin"
25 "github.com/apache/servicecomb-service-center/server/plugin/pkg/uuid"
26 "github.com/apache/servicecomb-service-center/server/plugin/pkg/uuid/buildin"
27 "golang.org/x/net/context"
31 mgr.RegisterPlugin(mgr.Plugin{PName: mgr.UUID, Name: "mp1context", New: New})
35 func New() mgr.PluginInstance {
36 return &Mp1ContextUUID{}
39 type Mp1ContextUUID struct {
43 func (cu *Mp1ContextUUID) fromContext(ctx context.Context) string {
44 key, ok := ctx.Value(uuid.ContextKey).(string)
51 func (cu *Mp1ContextUUID) GetServiceId(ctx context.Context) string {
52 content := cu.fromContext(ctx)
53 if len(content) == 0 {
54 return cu.BuildinUUID.GetServiceId(ctx)
57 shaSum := sha256.Sum256(util.StringToBytesWithNoCopy(content))
58 shaHalf := shaSum[0:8]
59 return fmt.Sprintf("%x", shaHalf)
62 func (cu *Mp1ContextUUID) GetInstanceId(_ context.Context) string {
63 shaSum := sha256.Sum256(util.StringToBytesWithNoCopy(util.GenerateUuid()))
64 shaHalf := shaSum[0:8]
65 return fmt.Sprintf("%x", shaHalf)