-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(minipipeline): include probe/control resolved addrs (#1405)
This information is useful for debugging. Also, I noticed an issue in how I compute DNS consistency, which suggests that I need to refactor how to do that. Having the control/probe resolved addrs is required to do that. Part of ooni/probe#2634
- Loading branch information
1 parent
cc808b5
commit 2a8aeac
Showing
72 changed files
with
6,007 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package minipipeline | ||
|
||
import ( | ||
"encoding/json" | ||
"sort" | ||
) | ||
|
||
// Set is a set containing keys with pretty JSON serialization | ||
// and deserialization rules and a valid zero value. | ||
type Set[T ~string | ~int64] struct { | ||
state map[T]bool | ||
} | ||
|
||
var ( | ||
_ json.Marshaler = Set[int64]{} | ||
_ json.Unmarshaler = &Set[int64]{} | ||
) | ||
|
||
// NewSet creates a new set containing the given keys. | ||
func NewSet[T ~string | ~int64](keys ...T) Set[T] { | ||
var sx Set[T] | ||
sx.Add(keys...) | ||
return sx | ||
} | ||
|
||
// Add adds the given key to the set. | ||
func (sx *Set[T]) Add(keys ...T) { | ||
if sx.state == nil { | ||
sx.state = make(map[T]bool) | ||
} | ||
for _, key := range keys { | ||
sx.state[key] = true | ||
} | ||
} | ||
|
||
// Len returns the number of keys inside the set. | ||
func (sx Set[T]) Len() int { | ||
return len(sx.state) | ||
} | ||
|
||
// Remove removes the given key from the set. | ||
func (sx Set[T]) Remove(keys ...T) { | ||
for _, key := range keys { | ||
delete(sx.state, key) | ||
} | ||
} | ||
|
||
// Keys returns the keys. | ||
func (sx Set[T]) Keys() []T { | ||
keys := []T{} | ||
for entry := range sx.state { | ||
keys = append(keys, entry) | ||
} | ||
sort.Slice(keys, func(i, j int) bool { | ||
return keys[i] < keys[j] | ||
}) | ||
return keys | ||
} | ||
|
||
// MarshalJSON implements json.Marshaler. | ||
func (sx Set[T]) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(sx.Keys()) | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler. | ||
func (sx *Set[T]) UnmarshalJSON(data []byte) error { | ||
var keys []T | ||
if err := json.Unmarshal(data, &keys); err != nil { | ||
return err | ||
} | ||
sx.Add(keys...) | ||
return nil | ||
} | ||
|
||
// Contains returns whether the set contains a key. | ||
func (sx *Set[T]) Contains(key T) bool { | ||
_, found := sx.state[key] | ||
return found | ||
} |
Oops, something went wrong.