Compare commits
4 Commits
32c61a46b7
...
f78f649648
Author | SHA1 | Date |
---|---|---|
nom3ad | f78f649648 | |
Brad Fitzpatrick | 1a963342c7 | |
Will Norris | 80decd83c1 | |
nom3ad | 3e50addb2d |
|
@ -35,6 +35,9 @@ func (d *Dialer) Dial(ctx context.Context) (*ClientConn, error) {
|
|||
wsScheme = "ws"
|
||||
host = net.JoinHostPort(host, d.HTTPPort)
|
||||
}
|
||||
if d.HTTPSPort != "" && d.HTTPSPort != "443" {
|
||||
host = net.JoinHostPort(host, d.HTTPSPort)
|
||||
}
|
||||
wsURL := &url.URL{
|
||||
Scheme: wsScheme,
|
||||
Host: host,
|
||||
|
|
|
@ -312,7 +312,6 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
var bucket string
|
||||
bumpStartIfNeeded := func() {}
|
||||
var startRecorded bool
|
||||
if bs := h.opts.BucketedStats; bs != nil {
|
||||
bucket = bs.bucketForRequest(r)
|
||||
|
@ -320,13 +319,11 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
switch v := bs.Started.Map.Get(bucket).(type) {
|
||||
case *expvar.Int:
|
||||
// If we've already seen this bucket for, count it immediately.
|
||||
v.Add(1)
|
||||
startRecorded = true
|
||||
case nil:
|
||||
// Otherwise, for newly seen paths, only count retroactively
|
||||
// (so started-finished doesn't go negative) so we don't fill
|
||||
// this LabelMap up with internet scanning spam.
|
||||
bumpStartIfNeeded = func() { bs.Started.Add(bucket, 1) }
|
||||
v.Add(1)
|
||||
startRecorded = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -445,8 +442,12 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// gets most of the way there but there are also plenty of URLs that are
|
||||
// almost right but result in 400s too. Seem easier to just only ignore
|
||||
// all 4xx and 5xx.
|
||||
if startRecorded || msg.Code < 400 {
|
||||
bumpStartIfNeeded()
|
||||
if startRecorded {
|
||||
bs.Finished.Add(bucket, 1)
|
||||
} else if msg.Code < 400 {
|
||||
// This is the first non-error request for this bucket,
|
||||
// so count it now retroactively.
|
||||
bs.Started.Add(bucket, 1)
|
||||
bs.Finished.Add(bucket, 1)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,11 @@ type Set[T comparable] map[T]struct{}
|
|||
|
||||
// SetOf returns a new set constructed from the elements in slice.
|
||||
func SetOf[T comparable](slice []T) Set[T] {
|
||||
return Of(slice...)
|
||||
}
|
||||
|
||||
// Of returns a new set constructed from the elements in slice.
|
||||
func Of[T comparable](slice ...T) Set[T] {
|
||||
s := make(Set[T])
|
||||
s.AddSlice(slice)
|
||||
return s
|
||||
|
@ -41,6 +46,13 @@ func (s Set[T]) AddSet(es Set[T]) {
|
|||
}
|
||||
}
|
||||
|
||||
// Make lazily initializes the map pointed to by s to be non-nil.
|
||||
func (s *Set[T]) Make() {
|
||||
if *s == nil {
|
||||
*s = make(Set[T])
|
||||
}
|
||||
}
|
||||
|
||||
// Slice returns the elements of the set as a slice. The elements will not be
|
||||
// in any particular order.
|
||||
func (s Set[T]) Slice() []T {
|
||||
|
|
|
@ -53,7 +53,7 @@ func TestSet(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSetOf(t *testing.T) {
|
||||
s := SetOf[int]([]int{1, 2, 3, 4, 4, 1})
|
||||
s := Of(1, 2, 3, 4, 4, 1)
|
||||
if s.Len() != 4 {
|
||||
t.Errorf("wrong len %d; want 4", s.Len())
|
||||
}
|
||||
|
@ -74,20 +74,20 @@ func TestEqual(t *testing.T) {
|
|||
tests := []test{
|
||||
{
|
||||
"equal",
|
||||
SetOf([]int{1, 2, 3, 4}),
|
||||
SetOf([]int{1, 2, 3, 4}),
|
||||
Of(1, 2, 3, 4),
|
||||
Of(1, 2, 3, 4),
|
||||
true,
|
||||
},
|
||||
{
|
||||
"not equal",
|
||||
SetOf([]int{1, 2, 3, 4}),
|
||||
SetOf([]int{1, 2, 3, 5}),
|
||||
Of(1, 2, 3, 4),
|
||||
Of(1, 2, 3, 5),
|
||||
false,
|
||||
},
|
||||
{
|
||||
"different lengths",
|
||||
SetOf([]int{1, 2, 3, 4, 5}),
|
||||
SetOf([]int{1, 2, 3, 5}),
|
||||
Of(1, 2, 3, 4, 5),
|
||||
Of(1, 2, 3, 5),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
@ -100,7 +100,7 @@ func TestEqual(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestClone(t *testing.T) {
|
||||
s := SetOf[int]([]int{1, 2, 3, 4, 4, 1})
|
||||
s := Of(1, 2, 3, 4, 4, 1)
|
||||
if s.Len() != 4 {
|
||||
t.Errorf("wrong len %d; want 4", s.Len())
|
||||
}
|
||||
|
@ -122,8 +122,8 @@ func TestSetJSONRoundTrip(t *testing.T) {
|
|||
}{
|
||||
{"empty", make(Set[string]), make(Set[int])},
|
||||
{"nil", nil, nil},
|
||||
{"one-item", SetOf([]string{"one"}), SetOf([]int{1})},
|
||||
{"multiple-items", SetOf([]string{"one", "two", "three"}), SetOf([]int{1, 2, 3})},
|
||||
{"one-item", Of("one"), Of(1)},
|
||||
{"multiple-items", Of("one", "two", "three"), Of(1, 2, 3)},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, func(t *testing.T) {
|
||||
|
@ -158,3 +158,12 @@ func TestSetJSONRoundTrip(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMake(t *testing.T) {
|
||||
var s Set[int]
|
||||
s.Make()
|
||||
s.Add(1)
|
||||
if !s.Contains(1) {
|
||||
t.Error("missing 1")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue