Skip to content

Commit

Permalink
refactor: url encode (#527)
Browse files Browse the repository at this point in the history
* fix nacos connector.

Signed-off-by: Cybwan <[email protected]>

* refactor url encode.

Signed-off-by: Cybwan <[email protected]>

---------

Signed-off-by: Cybwan <[email protected]>
  • Loading branch information
cybwan authored and reaver-flomesh committed Dec 19, 2024
1 parent fa15fba commit a8b819a
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 151 deletions.
2 changes: 1 addition & 1 deletion pkg/connector/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (cdr *CatalogDeregistration) ToEureka() *eureka.Instance {

func (cdr *CatalogDeregistration) ToNacos() *vo.DeregisterInstanceParam {
svcInfoSegs := strings.Split(cdr.ServiceID, constant.SERVICE_INFO_SPLITER)
if len(svcInfoSegs) < 4 {
if len(svcInfoSegs) < 2 {
return nil
}
r := new(vo.DeregisterInstanceParam)
Expand Down
47 changes: 35 additions & 12 deletions pkg/zookeeper/discovery/nebula/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"strings"

"github.com/flomesh-io/fsm/pkg/zookeeper/discovery/nebula/urlenc"
"github.com/flomesh-io/fsm/pkg/zookeeper/urlenc"
)

const (
Expand Down Expand Up @@ -62,6 +62,8 @@ type ServiceInstance struct {
Side string `urlenc:"side"`
Version string `urlenc:"version"`

Extends map[string]string `urlenc:"-"`

FsmConnectorServiceClusterSet string `urlenc:"fsm.connector.service.cluster.set,omitempty"`
FsmConnectorServiceConnectorUid string `urlenc:"fsm.connector.service.connector.uid,omitempty"`
FsmConnectorServiceGRPCViaGateway string `urlenc:"fsm.connector.service.grpc.via.gateway"`
Expand Down Expand Up @@ -117,16 +119,33 @@ func (ins *ServiceInstance) Marshal() ([]byte, error) {
if bytes, err := urlenc.Encode(ins); err != nil {
return nil, err
} else {
instanceUrl := url.URL{
Scheme: ins.Schema,
Host: ins.Addr,
RawQuery: string(bytes),
}
if len(instanceUrl.Host) == 0 {
instanceUrl.Host = fmt.Sprintf("%s:%d", ins.IP, ins.Port)
if rawValues, err := url.ParseQuery(string(bytes)); err != nil {
return nil, err
} else {
if len(ins.Extends) > 0 {
if bytes, err = urlenc.Encode(ins.Extends); err != nil {
return nil, err
} else {
if extValues, err := url.ParseQuery(string(bytes)); err == nil {
for k, v := range extValues {
rawValues[k] = v
}
} else {
return nil, err
}
}
}
instanceUrl := url.URL{
Scheme: ins.Schema,
Host: ins.Addr,
RawQuery: rawValues.Encode(),
}
if len(instanceUrl.Host) == 0 {
instanceUrl.Host = fmt.Sprintf("%s:%d", ins.IP, ins.Port)
}
ins.instanceId = url.QueryEscape(instanceUrl.String())
return []byte(ins.instanceId), nil
}
ins.instanceId = url.QueryEscape(instanceUrl.String())
return []byte(ins.instanceId), nil
}
}

Expand All @@ -149,9 +168,13 @@ func (ins *ServiceInstance) Unmarshal(instanceId string, data []byte) error {
return err
}
if len(instanceUrl.RawQuery) > 0 {
if err = urlenc.Decode([]byte(instanceUrl.RawQuery), ins); err != nil {
fmt.Println(err.Error())
if extQuery, err := urlenc.Decode([]byte(instanceUrl.RawQuery), ins); err != nil {
return err
} else if len(extQuery) > 0 {
ins.Extends = make(map[string]string)
if _, err = urlenc.Decode(extQuery, &ins.Extends); err != nil {
return err
}
}
}
ins.Schema = instanceUrl.Scheme
Expand Down
43 changes: 36 additions & 7 deletions pkg/zookeeper/discovery/nebula/mapfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package nebula

import (
"fmt"
"math"
"strconv"

"github.com/flomesh-io/fsm/pkg/connector"
Expand Down Expand Up @@ -105,7 +106,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if pid, err := strconv.Atoi(value); err == nil {
ins.PID = uint32(pid)
if pid >= 0 && pid < math.MaxUint32 {
ins.PID = uint32(pid)
} else {
ins.PID = 0
}
return nil
} else {
return err
Expand All @@ -131,7 +136,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if weight, err := strconv.Atoi(value); err == nil {
ins.Weight = uint32(weight)
if weight >= 0 && weight < math.MaxUint32 {
ins.Weight = uint32(weight)
} else {
ins.Weight = 0
}
return nil
} else {
return err
Expand Down Expand Up @@ -192,7 +201,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if connections, err := strconv.Atoi(value); err == nil {
ins.DefaultConnections = uint32(connections)
if connections >= 0 && connections < math.MaxUint32 {
ins.DefaultConnections = uint32(connections)
} else {
ins.DefaultConnections = 0
}
return nil
} else {
return err
Expand All @@ -214,7 +227,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if requests, err := strconv.Atoi(value); err == nil {
ins.DefaultRequests = uint32(requests)
if requests >= 0 && requests < math.MaxUint32 {
ins.DefaultRequests = uint32(requests)
} else {
ins.DefaultRequests = 0
}
return nil
} else {
return err
Expand All @@ -227,7 +244,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if reties, err := strconv.Atoi(value); err == nil {
ins.DefaultReties = uint32(reties)
if reties >= 0 && reties < math.MaxUint32 {
ins.DefaultReties = uint32(reties)
} else {
ins.DefaultReties = 0
}
return nil
} else {
return err
Expand All @@ -240,7 +261,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if timeout, err := strconv.Atoi(value); err == nil {
ins.DefaultTimeout = uint32(timeout)
if timeout >= 0 && timeout < math.MaxUint32 {
ins.DefaultTimeout = uint32(timeout)
} else {
ins.DefaultTimeout = 0
}
return nil
} else {
return err
Expand Down Expand Up @@ -271,7 +296,11 @@ var (
},
setter: func(ins *ServiceInstance, value string) error {
if port, err := strconv.Atoi(value); err == nil {
ins.RealPort = uint16(port)
if port >= 0 && port < math.MaxUint16 {
ins.RealPort = uint16(port)
} else {
ins.RealPort = 0
}
return nil
} else {
return err
Expand Down
Loading

0 comments on commit a8b819a

Please sign in to comment.