fixed middleware implementation, added middleware tracing for easier debug

This commit is contained in:
yusing
2024-10-02 13:55:41 +08:00
parent d172552fb0
commit ba13b81b0e
31 changed files with 561 additions and 196 deletions

View File

@@ -42,6 +42,10 @@ func FormatDuration(d time.Duration) string {
return strings.Join(parts[:len(parts)-1], ", ") + " and " + parts[len(parts)-1]
}
func FormatTime(t time.Time) string {
return t.Format("2006-01-02 15:04:05")
}
func ParseBool(s string) bool {
switch strings.ToLower(s) {
case "1", "true", "yes", "on":

View File

@@ -1,19 +1,25 @@
package functional
import (
"encoding/json"
"sync"
)
type Slice[T any] struct {
s []T
s []T
mu sync.Mutex
}
func NewSlice[T any]() *Slice[T] {
return &Slice[T]{make([]T, 0)}
return &Slice[T]{s: make([]T, 0)}
}
func NewSliceN[T any](n int) *Slice[T] {
return &Slice[T]{make([]T, n)}
return &Slice[T]{s: make([]T, n)}
}
func NewSliceFrom[T any](s []T) *Slice[T] {
return &Slice[T]{s}
return &Slice[T]{s: s}
}
func (s *Slice[T]) Size() int {
@@ -46,6 +52,30 @@ func (s *Slice[T]) AddRange(other *Slice[T]) *Slice[T] {
return s
}
func (s *Slice[T]) SafeAdd(e T) *Slice[T] {
s.mu.Lock()
defer s.mu.Unlock()
return s.Add(e)
}
func (s *Slice[T]) SafeAddRange(other *Slice[T]) *Slice[T] {
s.mu.Lock()
defer s.mu.Unlock()
return s.AddRange(other)
}
func (s *Slice[T]) Pop() T {
v := s.s[len(s.s)-1]
s.s = s.s[:len(s.s)-1]
return v
}
func (s *Slice[T]) SafePop() T {
s.mu.Lock()
defer s.mu.Unlock()
return s.Pop()
}
func (s *Slice[T]) ForEach(do func(T)) {
for _, v := range s.s {
do(v)
@@ -57,7 +87,7 @@ func (s *Slice[T]) Map(m func(T) T) *Slice[T] {
for i, v := range s.s {
n[i] = m(v)
}
return &Slice[T]{n}
return &Slice[T]{s: n}
}
func (s *Slice[T]) Filter(f func(T) bool) *Slice[T] {
@@ -67,5 +97,13 @@ func (s *Slice[T]) Filter(f func(T) bool) *Slice[T] {
n = append(n, v)
}
}
return &Slice[T]{n}
return &Slice[T]{s: n}
}
func (s *Slice[T]) String() string {
out, err := json.MarshalIndent(s.s, "", " ")
if err != nil {
panic(err)
}
return string(out)
}

View File

@@ -2,10 +2,23 @@ package utils
import (
"errors"
"os"
"reflect"
"testing"
"github.com/yusing/go-proxy/internal/common"
)
func init() {
if common.IsTest {
os.Args = append([]string{os.Args[0], "-test.v"}, os.Args[1:]...)
}
}
func IgnoreError[Result any](r Result, _ error) Result {
return r
}
func ExpectNoError(t *testing.T, err error) {
t.Helper()
if err != nil && !reflect.ValueOf(err).IsNil() {