Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
调整部分场景的兼容处理 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
MI-cool committed Aug 1, 2023
1 parent fa965e1 commit dce2b97
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func NoopSelectInstances(ctx *FilterContext, result *nacosmodel.ServiceInfo,
func SelectInstancesWithHealthyProtection(ctx *FilterContext, result *nacosmodel.ServiceInfo,
instances []*nacosmodel.Instance, healthCount int32) *nacosmodel.ServiceInfo {
protectThreshold := ctx.Service.ProtectionThreshold
if len(instances) > 0 && float64(healthCount)/float64(len(instances)) <= protectThreshold {
if len(instances) > 0 && float64(healthCount)/float64(len(instances)) >= protectThreshold {
ret := instances
if ctx.HealthyOnly {
healthyIns := make([]*nacosmodel.Instance, 0, len(instances))
Expand Down
20 changes: 10 additions & 10 deletions model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ import (
)

type Service struct {
CacheMillis uint64 `json:"cacheMillis"`
Hosts []Instance `json:"hosts"`
Checksum string `json:"checksum"`
LastRefTime uint64 `json:"lastRefTime"`
Clusters string `json:"clusters"`
Name string `json:"name"`
GroupName string `json:"groupName"`
Valid bool `json:"valid"`
AllIPs bool `json:"allIPs"`
ReachProtectionThreshold bool `json:"reachProtectionThreshold"`
CacheMillis int64 `json:"cacheMillis"`
Hosts []*Instance `json:"hosts"`
Checksum string `json:"checksum"`
LastRefTime int64 `json:"lastRefTime"`
Clusters string `json:"clusters"`
Name string `json:"name"`
GroupName string `json:"groupName"`
Valid bool `json:"valid"`
AllIPs bool `json:"allIPs"`
ReachProtectionThreshold bool `json:"reachProtectionThreshold"`
}

type SimpleServiceInfo struct {
Expand Down
2 changes: 1 addition & 1 deletion v1/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (n *NacosV1Server) ListInstances(req *restful.Request, rsp *restful.Respons
params := ParseQueryParams(req)

params[model.ParamNamespaceID] = model.ToPolarisNamespace(params[model.ParamNamespaceID])
data, err := n.handleQueryInstances(ctx, ParseQueryParams(req))
data, err := n.handleQueryInstances(ctx, params)
if err != nil {
core.WrirteNacosErrorResponse(err, rsp)
return
Expand Down
34 changes: 18 additions & 16 deletions v2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package v2

import (
"context"
"strings"

"github.com/polaris-contrib/apiserver-nacos/core"
"github.com/polaris-contrib/apiserver-nacos/model"
nacospb "github.com/polaris-contrib/apiserver-nacos/v2/pb"
)
Expand All @@ -36,25 +38,25 @@ func (h *NacosV2Server) handleServiceQueryRequest(ctx context.Context, req nacos
Success: true,
Message: "success",
},
ServiceInfo: model.Service{
Name: svcQueryReq.ServiceName,
GroupName: svcQueryReq.GroupName,
Hosts: make([]model.Instance, 0),
},
}
namespace := model.ToPolarisNamespace(svcQueryReq.Namespace)
svc := h.discoverSvr.Cache().Service().GetServiceByName(svcQueryReq.ServiceName, namespace)
if svc == nil {
return resp, nil
}
insts := h.discoverSvr.Cache().Instance().GetInstancesByServiceID(svc.ID)
if insts == nil {
return resp, nil
filterCtx := &core.FilterContext{
Service: core.ToNacosService(h.discoverSvr.Cache(), namespace, svcQueryReq.ServiceName, svcQueryReq.GroupName),
Clusters: strings.Split(svcQueryReq.Cluster, ","),
EnableOnly: true,
HealthyOnly: svcQueryReq.HealthyOnly,
}
for _, inst := range insts {
ins := model.Instance{}
ins.FromSpecInstance(inst)
resp.ServiceInfo.Hosts = append(resp.ServiceInfo.Hosts, ins)
// 默认只下发 enable 的实例
result := h.store.ListInstances(filterCtx, core.SelectInstancesWithHealthyProtection)
resp.ServiceInfo = model.Service{
Name: svcQueryReq.ServiceName,
GroupName: svcQueryReq.GroupName,
Clusters: result.Clusters,
CacheMillis: result.CacheMillis,
Hosts: result.Hosts,
Checksum: result.Checksum,
LastRefTime: result.LastRefTime,
ReachProtectionThreshold: result.ReachProtectionThreshold,
}
return resp, nil
}

0 comments on commit dce2b97

Please sign in to comment.