mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
chore: reduce memory usage of docker container info
This commit is contained in:
parent
49ee9c908a
commit
5d2b700cb2
3 changed files with 25 additions and 15 deletions
|
@ -15,7 +15,7 @@ import (
|
|||
)
|
||||
|
||||
type (
|
||||
PortMapping = map[int]container.Port
|
||||
PortMapping = map[int]*container.Port
|
||||
Container struct {
|
||||
_ U.NoCopy
|
||||
|
||||
|
@ -58,21 +58,12 @@ var DummyContainer = new(Container)
|
|||
func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
|
||||
isExplicit := false
|
||||
helper := containerHelper{c}
|
||||
for lbl := range c.Labels {
|
||||
if strings.HasPrefix(lbl, NSProxy+".") {
|
||||
isExplicit = true
|
||||
} else {
|
||||
delete(c.Labels, lbl)
|
||||
}
|
||||
}
|
||||
res = &Container{
|
||||
DockerHost: dockerHost,
|
||||
Image: helper.parseImage(),
|
||||
ContainerName: helper.getName(),
|
||||
ContainerID: c.ID,
|
||||
|
||||
Labels: c.Labels,
|
||||
|
||||
Mounts: helper.getMounts(),
|
||||
|
||||
PublicPortMapping: helper.getPublicPortMapping(),
|
||||
|
@ -100,11 +91,20 @@ func FromDocker(c *container.Summary, dockerHost string) (res *Container) {
|
|||
|
||||
res.setPrivateHostname(helper)
|
||||
res.setPublicHostname()
|
||||
|
||||
for lbl := range c.Labels {
|
||||
if strings.HasPrefix(lbl, NSProxy+".") {
|
||||
isExplicit = true
|
||||
} else {
|
||||
delete(c.Labels, lbl)
|
||||
}
|
||||
}
|
||||
res.RouteConfig = utils.FitMap(c.Labels)
|
||||
return
|
||||
}
|
||||
|
||||
func FromInspectResponse(json container.InspectResponse, dockerHost string) *Container {
|
||||
ports := make([]container.Port, 0)
|
||||
ports := make([]container.Port, 0, len(json.NetworkSettings.Ports))
|
||||
for k, bindings := range json.NetworkSettings.Ports {
|
||||
proto, privPortStr := nat.SplitProtoPort(string(k))
|
||||
privPort, _ := strconv.ParseUint(privPortStr, 10, 16)
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/yusing/go-proxy/internal/utils"
|
||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||
)
|
||||
|
||||
|
@ -62,15 +63,15 @@ func (c containerHelper) getPublicPortMapping() PortMapping {
|
|||
if v.PublicPort == 0 {
|
||||
continue
|
||||
}
|
||||
res[int(v.PublicPort)] = v
|
||||
res[int(v.PublicPort)] = &v
|
||||
}
|
||||
return res
|
||||
return utils.FitMap(res)
|
||||
}
|
||||
|
||||
func (c containerHelper) getPrivatePortMapping() PortMapping {
|
||||
res := make(PortMapping)
|
||||
res := make(PortMapping, len(c.Ports))
|
||||
for _, v := range c.Ports {
|
||||
res[int(v.PrivatePort)] = v
|
||||
res[int(v.PrivatePort)] = &v
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
|
9
internal/utils/maps.go
Normal file
9
internal/utils/maps.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package utils
|
||||
|
||||
func FitMap[K comparable, V any](m map[K]V) map[K]V {
|
||||
res := make(map[K]V, len(m))
|
||||
for k, v := range m {
|
||||
res[k] = v
|
||||
}
|
||||
return res
|
||||
}
|
Loading…
Add table
Reference in a new issue