Vasiliy Tolstov
5b0175c2e5
* reduce allocations in tunnel code Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * another allocation fix Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * allocate maps with len if it known Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org> * allocate key for send once Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
package memory
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
|
)
|
|
|
|
func serviceToRecord(s *registry.Service, ttl time.Duration) *record {
|
|
metadata := make(map[string]string, len(s.Metadata))
|
|
for k, v := range s.Metadata {
|
|
metadata[k] = v
|
|
}
|
|
|
|
nodes := make(map[string]*node, len(s.Nodes))
|
|
for _, n := range s.Nodes {
|
|
nodes[n.Id] = &node{
|
|
Node: n,
|
|
TTL: ttl,
|
|
LastSeen: time.Now(),
|
|
}
|
|
}
|
|
|
|
endpoints := make([]*registry.Endpoint, len(s.Endpoints))
|
|
for i, e := range s.Endpoints {
|
|
endpoints[i] = e
|
|
}
|
|
|
|
return &record{
|
|
Name: s.Name,
|
|
Version: s.Version,
|
|
Metadata: metadata,
|
|
Nodes: nodes,
|
|
Endpoints: endpoints,
|
|
}
|
|
}
|
|
|
|
func recordToService(r *record) *registry.Service {
|
|
metadata := make(map[string]string, len(r.Metadata))
|
|
for k, v := range r.Metadata {
|
|
metadata[k] = v
|
|
}
|
|
|
|
endpoints := make([]*registry.Endpoint, len(r.Endpoints))
|
|
for i, e := range r.Endpoints {
|
|
request := new(registry.Value)
|
|
if e.Request != nil {
|
|
*request = *e.Request
|
|
}
|
|
response := new(registry.Value)
|
|
if e.Response != nil {
|
|
*response = *e.Response
|
|
}
|
|
|
|
metadata := make(map[string]string, len(e.Metadata))
|
|
for k, v := range e.Metadata {
|
|
metadata[k] = v
|
|
}
|
|
|
|
endpoints[i] = ®istry.Endpoint{
|
|
Name: e.Name,
|
|
Request: request,
|
|
Response: response,
|
|
Metadata: metadata,
|
|
}
|
|
}
|
|
|
|
nodes := make([]*registry.Node, len(r.Nodes))
|
|
i := 0
|
|
for _, n := range r.Nodes {
|
|
metadata := make(map[string]string, len(n.Metadata))
|
|
for k, v := range n.Metadata {
|
|
metadata[k] = v
|
|
}
|
|
|
|
nodes[i] = ®istry.Node{
|
|
Id: n.Id,
|
|
Address: n.Address,
|
|
Metadata: metadata,
|
|
}
|
|
i++
|
|
}
|
|
|
|
return ®istry.Service{
|
|
Name: r.Name,
|
|
Version: r.Version,
|
|
Metadata: metadata,
|
|
Endpoints: endpoints,
|
|
Nodes: nodes,
|
|
}
|
|
}
|