mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 02:09:01 +02:00
refactor: remove unnecessary xsync.Map wrapper
- Updated agentPool, cachedAddr, fileContentMap, and lastSeenMap to use xsync.Map. - Removed functional package and its related tests.
This commit is contained in:
@@ -3,11 +3,11 @@ package agent
|
|||||||
import (
|
import (
|
||||||
"iter"
|
"iter"
|
||||||
|
|
||||||
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/utils/functional"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var agentPool = functional.NewMapOf[string, *AgentConfig]()
|
var agentPool = xsync.NewMap[string, *AgentConfig](xsync.WithPresize(10))
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if common.IsTest {
|
if common.IsTest {
|
||||||
|
|||||||
@@ -384,7 +384,8 @@ func UpdateSelfhstIcons() error {
|
|||||||
for _, item := range data {
|
for _, item := range data {
|
||||||
var tag string
|
var tag string
|
||||||
if item.Tags != "" {
|
if item.Tags != "" {
|
||||||
tag = strutils.CommaSeperatedList(item.Tags)[0]
|
tag, _, _ = strings.Cut(item.Tags, ",")
|
||||||
|
tag = strings.TrimSpace(tag)
|
||||||
}
|
}
|
||||||
icon := &IconMeta{
|
icon := &IconMeta{
|
||||||
DisplayName: item.Name,
|
DisplayName: item.Name,
|
||||||
|
|||||||
@@ -5,16 +5,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
gphttp "github.com/yusing/go-proxy/internal/net/gphttp"
|
||||||
nettypes "github.com/yusing/go-proxy/internal/net/types"
|
nettypes "github.com/yusing/go-proxy/internal/net/types"
|
||||||
"github.com/yusing/go-proxy/internal/serialization"
|
"github.com/yusing/go-proxy/internal/serialization"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
cidrWhitelist struct {
|
cidrWhitelist struct {
|
||||||
CIDRWhitelistOpts
|
CIDRWhitelistOpts
|
||||||
cachedAddr F.Map[string, bool] // cache for trusted IPs
|
cachedAddr *xsync.Map[string, bool] // cache for trusted IPs
|
||||||
}
|
}
|
||||||
CIDRWhitelistOpts struct {
|
CIDRWhitelistOpts struct {
|
||||||
Allow []*nettypes.CIDR `validate:"min=1"`
|
Allow []*nettypes.CIDR `validate:"min=1"`
|
||||||
@@ -42,7 +42,7 @@ func init() {
|
|||||||
// setup implements MiddlewareWithSetup.
|
// setup implements MiddlewareWithSetup.
|
||||||
func (wl *cidrWhitelist) setup() {
|
func (wl *cidrWhitelist) setup() {
|
||||||
wl.CIDRWhitelistOpts = cidrWhitelistDefaults
|
wl.CIDRWhitelistOpts = cidrWhitelistDefaults
|
||||||
wl.cachedAddr = F.NewMapOf[string, bool]()
|
wl.cachedAddr = xsync.NewMap[string, bool](xsync.WithPresize(100))
|
||||||
}
|
}
|
||||||
|
|
||||||
// before implements RequestModifier.
|
// before implements RequestModifier.
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/task"
|
"github.com/yusing/go-proxy/internal/task"
|
||||||
U "github.com/yusing/go-proxy/internal/utils"
|
U "github.com/yusing/go-proxy/internal/utils"
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
|
||||||
W "github.com/yusing/go-proxy/internal/watcher"
|
W "github.com/yusing/go-proxy/internal/watcher"
|
||||||
"github.com/yusing/go-proxy/internal/watcher/events"
|
"github.com/yusing/go-proxy/internal/watcher/events"
|
||||||
)
|
)
|
||||||
@@ -21,7 +21,7 @@ const errPagesBasePath = common.ErrorPagesBasePath
|
|||||||
var (
|
var (
|
||||||
setupOnce sync.Once
|
setupOnce sync.Once
|
||||||
dirWatcher W.Watcher
|
dirWatcher W.Watcher
|
||||||
fileContentMap = F.NewMapOf[string, []byte]()
|
fileContentMap = xsync.NewMap[string, []byte](xsync.WithGrowOnly())
|
||||||
)
|
)
|
||||||
|
|
||||||
func setup() {
|
func setup() {
|
||||||
@@ -52,7 +52,7 @@ func loadContent() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if fileContentMap.Has(file) {
|
if _, ok := fileContentMap.Load(file); ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
content, err := os.ReadFile(file)
|
content, err := os.ReadFile(file)
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import (
|
|||||||
"github.com/puzpuzpuz/xsync/v4"
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
"github.com/yusing/go-proxy/internal/gperr"
|
"github.com/yusing/go-proxy/internal/gperr"
|
||||||
"github.com/yusing/go-proxy/internal/utils"
|
"github.com/yusing/go-proxy/internal/utils"
|
||||||
"github.com/yusing/go-proxy/internal/utils/functional"
|
|
||||||
"github.com/yusing/go-proxy/internal/utils/strutils"
|
"github.com/yusing/go-proxy/internal/utils/strutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -536,7 +535,7 @@ func UnmarshalValidateYAMLIntercept[T any](data []byte, target *T, intercept fun
|
|||||||
return MapUnmarshalValidate(m, target)
|
return MapUnmarshalValidate(m, target)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UnmarshalValidateYAMLXSync[V any](data []byte) (_ functional.Map[string, V], err gperr.Error) {
|
func UnmarshalValidateYAMLXSync[V any](data []byte) (_ *xsync.Map[string, V], err gperr.Error) {
|
||||||
m := make(map[string]any)
|
m := make(map[string]any)
|
||||||
if err = gperr.Wrap(yaml.Unmarshal(data, &m)); err != nil {
|
if err = gperr.Wrap(yaml.Unmarshal(data, &m)); err != nil {
|
||||||
return
|
return
|
||||||
@@ -545,7 +544,11 @@ func UnmarshalValidateYAMLXSync[V any](data []byte) (_ functional.Map[string, V]
|
|||||||
if err = MapUnmarshalValidate(m, m2); err != nil {
|
if err = MapUnmarshalValidate(m, m2); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return functional.NewMapFrom(m2), nil
|
ret := xsync.NewMap[string, V](xsync.WithPresize(len(m)))
|
||||||
|
for k, v := range m2 {
|
||||||
|
ret.Store(k, v)
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadSerialized[T any](path string, dst *T, deserialize func(data []byte, dst any) error) error {
|
func loadSerialized[T any](path string, dst *T, deserialize func(data []byte, dst any) error) error {
|
||||||
|
|||||||
@@ -1,103 +0,0 @@
|
|||||||
package functional
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/goccy/go-yaml"
|
|
||||||
"github.com/puzpuzpuz/xsync/v4"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Map[KT comparable, VT any] struct {
|
|
||||||
*xsync.Map[KT, VT]
|
|
||||||
}
|
|
||||||
|
|
||||||
const minParallelSize = 4
|
|
||||||
|
|
||||||
func NewMapOf[KT comparable, VT any](options ...func(*xsync.MapConfig)) Map[KT, VT] {
|
|
||||||
return Map[KT, VT]{xsync.NewMap[KT, VT](options...)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMapFrom[KT comparable, VT any](m map[KT]VT) (res Map[KT, VT]) {
|
|
||||||
res = NewMapOf[KT, VT](xsync.WithPresize(len(m)))
|
|
||||||
for k, v := range m {
|
|
||||||
res.Store(k, v)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMap[MapType Map[KT, VT], KT comparable, VT any]() Map[KT, VT] {
|
|
||||||
return NewMapOf[KT, VT]()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RangeAll calls the given function for each key-value pair in the map.
|
|
||||||
//
|
|
||||||
// Parameters:
|
|
||||||
//
|
|
||||||
// do: function to call for each key-value pair
|
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
//
|
|
||||||
// nothing
|
|
||||||
func (m Map[KT, VT]) RangeAll(do func(k KT, v VT)) {
|
|
||||||
m.Range(func(k KT, v VT) bool {
|
|
||||||
do(k, v)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// RangeAllParallel calls the given function for each key-value pair in the map,
|
|
||||||
// in parallel. The map is not safe for modification from within the function.
|
|
||||||
//
|
|
||||||
// Parameters:
|
|
||||||
//
|
|
||||||
// do: function to call for each key-value pair
|
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
//
|
|
||||||
// nothing
|
|
||||||
func (m Map[KT, VT]) RangeAllParallel(do func(k KT, v VT)) {
|
|
||||||
if m.Size() < minParallelSize {
|
|
||||||
m.RangeAll(do)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for k, v := range m.Range {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(k KT, v VT) {
|
|
||||||
defer wg.Done()
|
|
||||||
do(k, v)
|
|
||||||
}(k, v)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
// CollectErrors calls the given function for each key-value pair in the map,
|
|
||||||
// then returns a slice of errors collected.
|
|
||||||
func (m Map[KT, VT]) CollectErrors(do func(k KT, v VT) error) []error {
|
|
||||||
errs := make([]error, 0)
|
|
||||||
m.Range(func(k KT, v VT) bool {
|
|
||||||
if err := do(k, v); err != nil {
|
|
||||||
errs = append(errs, err)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return errs
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m Map[KT, VT]) Has(k KT) bool {
|
|
||||||
_, ok := m.Load(k)
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m Map[KT, VT]) String() string {
|
|
||||||
tmp := make(map[KT]VT, m.Size())
|
|
||||||
m.RangeAll(func(k KT, v VT) {
|
|
||||||
tmp[k] = v
|
|
||||||
})
|
|
||||||
data, err := yaml.Marshal(&tmp)
|
|
||||||
if err != nil {
|
|
||||||
return err.Error()
|
|
||||||
}
|
|
||||||
return string(data)
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package functional_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
. "github.com/yusing/go-proxy/internal/utils/functional"
|
|
||||||
. "github.com/yusing/go-proxy/internal/utils/testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNewMapFrom(t *testing.T) {
|
|
||||||
m := NewMapFrom(map[string]int{
|
|
||||||
"a": 1,
|
|
||||||
"b": 2,
|
|
||||||
"c": 3,
|
|
||||||
})
|
|
||||||
ExpectEqual(t, m.Size(), 3)
|
|
||||||
ExpectTrue(t, m.Has("a"))
|
|
||||||
ExpectTrue(t, m.Has("b"))
|
|
||||||
ExpectTrue(t, m.Has("c"))
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,6 @@ module github.com/yusing/go-proxy/internal/utils
|
|||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/goccy/go-yaml v1.18.0
|
|
||||||
github.com/puzpuzpuz/xsync/v4 v4.1.0
|
github.com/puzpuzpuz/xsync/v4 v4.1.0
|
||||||
github.com/rs/zerolog v1.34.0
|
github.com/rs/zerolog v1.34.0
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
|
|
||||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
|
||||||
|
|||||||
@@ -3,10 +3,10 @@ package monitor
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
F "github.com/yusing/go-proxy/internal/utils/functional"
|
"github.com/puzpuzpuz/xsync/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var lastSeenMap = F.NewMapOf[string, time.Time]()
|
var lastSeenMap = xsync.NewMap[string, time.Time](xsync.WithPresize(50), xsync.WithGrowOnly())
|
||||||
|
|
||||||
func SetLastSeen(service string, lastSeen time.Time) {
|
func SetLastSeen(service string, lastSeen time.Time) {
|
||||||
lastSeenMap.Store(service, lastSeen)
|
lastSeenMap.Store(service, lastSeen)
|
||||||
|
|||||||
Reference in New Issue
Block a user