mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-30 02:43:05 +00:00
c8ebbede54
This PR removes the complicated session management introduced in https://github.com/juanfont/headscale/pull/1791 which kept track of the sessions in a map, in addition to the channel already kept track of in the notifier. Instead of trying to close the mapsession, it will now be replaced by the new one and closed after so all new updates goes to the right place. The map session serve function is also split into a streaming and a non-streaming version for better readability. RemoveNode in the notifier will not remove a node if the channel is not matching the one that has been passed (e.g. it has been replaced with a new one). A new tuning parameter has been added to added to set timeout before the notifier gives up to send an update to a node. Add a keep alive resetter so we wait with sending keep alives if a node has just received an update. In addition it adds a bunch of env debug flags that can be set: - `HEADSCALE_DEBUG_HIGH_CARDINALITY_METRICS`: make certain metrics include per node.id, not recommended to use in prod. - `HEADSCALE_DEBUG_PROFILING_ENABLED`: activate tracing - `HEADSCALE_DEBUG_PROFILING_PATH`: where to store traces - `HEADSCALE_DEBUG_DUMP_CONFIG`: calls `spew.Dump` on the config object startup - `HEADSCALE_DEBUG_DEADLOCK`: enable go-deadlock to dump goroutines if it looks like a deadlock has occured, enabled in integration tests. Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
249 lines
4.6 KiB
Go
249 lines
4.6 KiB
Go
package notifier
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
|
"tailscale.com/tailcfg"
|
|
)
|
|
|
|
func TestBatcher(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
updates []types.StateUpdate
|
|
want []types.StateUpdate
|
|
}{
|
|
{
|
|
name: "full-passthrough",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StateFullUpdate,
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StateFullUpdate,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "derp-passthrough",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StateDERPUpdated,
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StateDERPUpdated,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "single-node-update",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChanged,
|
|
ChangeNodes: []types.NodeID{
|
|
2,
|
|
},
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChanged,
|
|
ChangeNodes: []types.NodeID{
|
|
2,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "merge-node-update",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChanged,
|
|
ChangeNodes: []types.NodeID{
|
|
2, 4,
|
|
},
|
|
},
|
|
{
|
|
Type: types.StatePeerChanged,
|
|
ChangeNodes: []types.NodeID{
|
|
2, 3,
|
|
},
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChanged,
|
|
ChangeNodes: []types.NodeID{
|
|
2, 3, 4,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "single-patch-update",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 2,
|
|
DERPRegion: 5,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 2,
|
|
DERPRegion: 5,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "merge-patch-to-same-node-update",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 2,
|
|
DERPRegion: 5,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 2,
|
|
DERPRegion: 6,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 2,
|
|
DERPRegion: 6,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "merge-patch-to-multiple-node-update",
|
|
updates: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 3,
|
|
Endpoints: []netip.AddrPort{
|
|
netip.MustParseAddrPort("1.1.1.1:9090"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 3,
|
|
Endpoints: []netip.AddrPort{
|
|
netip.MustParseAddrPort("1.1.1.1:9090"),
|
|
netip.MustParseAddrPort("2.2.2.2:8080"),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 4,
|
|
DERPRegion: 6,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 4,
|
|
Cap: tailcfg.CapabilityVersion(54),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
want: []types.StateUpdate{
|
|
{
|
|
Type: types.StatePeerChangedPatch,
|
|
ChangePatches: []*tailcfg.PeerChange{
|
|
{
|
|
NodeID: 3,
|
|
Endpoints: []netip.AddrPort{
|
|
netip.MustParseAddrPort("1.1.1.1:9090"),
|
|
netip.MustParseAddrPort("2.2.2.2:8080"),
|
|
},
|
|
},
|
|
{
|
|
NodeID: 4,
|
|
DERPRegion: 6,
|
|
Cap: tailcfg.CapabilityVersion(54),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
n := NewNotifier(&types.Config{
|
|
Tuning: types.Tuning{
|
|
// We will call flush manually for the tests,
|
|
// so do not run the worker.
|
|
BatchChangeDelay: time.Hour,
|
|
},
|
|
})
|
|
|
|
ch := make(chan types.StateUpdate, 30)
|
|
defer close(ch)
|
|
n.AddNode(1, ch)
|
|
defer n.RemoveNode(1, ch)
|
|
|
|
for _, u := range tt.updates {
|
|
n.NotifyAll(context.Background(), u)
|
|
}
|
|
|
|
n.b.flush()
|
|
|
|
var got []types.StateUpdate
|
|
for len(ch) > 0 {
|
|
out := <-ch
|
|
got = append(got, out)
|
|
}
|
|
|
|
if diff := cmp.Diff(tt.want, got, util.Comparers...); diff != "" {
|
|
t.Errorf("batcher() unexpected result (-want +got):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|