Skip to content

Commit

Permalink
more poll work
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Jul 26, 2023
1 parent 3fe75fb commit 0b4dc82
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 49 deletions.
34 changes: 26 additions & 8 deletions hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ type Mapper struct {
logtail bool
randomClientPort bool

seq uint64
uid string
created time.Time
seq uint64
}

func NewMapper(
Expand All @@ -70,6 +72,8 @@ func NewMapper(
Str("machine", machine.Hostname).
Msg("creating new mapper")

uid, _ := util.GenerateRandomStringDNSSafe(8)

Check failure on line 75 in hscontrol/mapper/mapper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

mnd: Magic number: 8, in <argument> detected (gomnd)

return &Mapper{
db: db,

Expand All @@ -81,9 +85,17 @@ func NewMapper(
dnsCfg: dnsCfg,
logtail: logtail,
randomClientPort: randomClientPort,

uid: uid,
created: time.Now(),
seq: 0,
}
}

func (m *Mapper) String() string {
return fmt.Sprintf("Mapper: { seq: %d, uid: %s, created: %s }", m.seq, m.created)

Check failure on line 96 in hscontrol/mapper/mapper.go

View workflow job for this annotation

GitHub Actions / golangci-lint

printf: fmt.Sprintf format %s reads arg #3, but call has 2 args (govet)
}

// TODO: Optimise
// As this work continues, the idea is that there will be one Mapper instance
// per node, attached to the open stream between the control and client.
Expand Down Expand Up @@ -279,7 +291,7 @@ func addNextDNSMetadata(resolvers []*dnstype.Resolver, machine types.Machine) {
}

// FullMapResponse returns a MapResponse for the given machine.
func (m Mapper) FullMapResponse(
func (m *Mapper) FullMapResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
pol *policy.ACLPolicy,
Expand Down Expand Up @@ -315,7 +327,7 @@ func (m Mapper) FullMapResponse(
return m.marshalMapResponse(mapResponse, machine, mapRequest.Compress)
}

func (m Mapper) KeepAliveResponse(
func (m *Mapper) KeepAliveResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
) ([]byte, error) {
Expand All @@ -325,7 +337,7 @@ func (m Mapper) KeepAliveResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}

func (m Mapper) DERPMapResponse(
func (m *Mapper) DERPMapResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
derpMap tailcfg.DERPMap,
Expand All @@ -336,7 +348,7 @@ func (m Mapper) DERPMapResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}

func (m Mapper) PeerChangedResponse(
func (m *Mapper) PeerChangedResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
machineKeys []uint64,
Expand Down Expand Up @@ -394,7 +406,7 @@ func (m Mapper) PeerChangedResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}

func (m Mapper) PeerRemovedResponse(
func (m *Mapper) PeerRemovedResponse(
mapRequest tailcfg.MapRequest,
machine *types.Machine,
removed []tailcfg.NodeID,
Expand All @@ -405,7 +417,7 @@ func (m Mapper) PeerRemovedResponse(
return m.marshalMapResponse(&resp, machine, mapRequest.Compress)
}

func (m Mapper) marshalMapResponse(
func (m *Mapper) marshalMapResponse(
resp *tailcfg.MapResponse,
machine *types.Machine,
compression string,
Expand Down Expand Up @@ -441,7 +453,13 @@ func (m Mapper) marshalMapResponse(

now := time.Now().Unix()

err = os.WriteFile(path.Join(mPath, fmt.Sprintf("%d-%d.json", m.seq, now)), jsonBody, perms)
mapResponsePath := path.Join(
mPath,
fmt.Sprintf("%d-%s-%d.json", atomic.LoadUint64(&m.seq), m.uid, now),
)

log.Trace().Msgf("Writing MapResponse to %s", mapResponsePath)
err = os.WriteFile(mapResponsePath, jsonBody, perms)
if err != nil {
panic(err)
}
Expand Down
39 changes: 0 additions & 39 deletions hscontrol/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func (h *Headscale) handlePoll(
Bool("noise", isNoise).
Str("machine", machine.Hostname).
Msg("Ignoring request, don't know how to handle it")
http.Error(writer, "", http.StatusBadRequest)

return
}
Expand Down Expand Up @@ -222,44 +221,6 @@ func (h *Headscale) pollNetMapStream(
) {
logInfo, logErr := logPollFunc(mapRequest, machine, isNoise)

// -------------------------------------------------------------------
// Try to send initial full map response
// logInfo("Sending Initial Full MapResponse")
// data, err := mapp.FullMapResponse(mapRequest, machine, h.ACLPolicy)
// if err != nil {
// logErr(err, "Could not get the create map update")
//
// return
// }
//
// _, err = writer.Write(data)
// if err != nil {
// logErr(err, "Could not write the map response")
//
// updateRequestsSentToNode.WithLabelValues(machine.User.Name, machine.Hostname, "failed").
// Inc()
//
// return
// }
//
// if flusher, ok := writer.(http.Flusher); ok {
// flusher.Flush()
// } else {
// return
// }
//
// // Keep track of the last successful update,
// // we sometimes end in a state were the update
// // is not picked up by a client and we use this
// // to determine if we should "force" an update.
// err = h.db.TouchMachine(machine)
// if err != nil {
// logErr(err, "Cannot update machine LastSuccessfulUpdate")
//
// return
// }
// -------------------------------------------------------------------

keepAliveTicker := time.NewTicker(keepAliveInterval)

h.pollNetMapStreamWG.Add(1)
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/poll_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (h *Headscale) PollNetMapHandler(
Str("handler", "PollNetMap").
Str("id", machineKeyStr).
Str("machine", machine.Hostname).
Msg("A machine is entering polling via the legacy protocol")
Msg("A machine is sending a MapRequest via legacy protocol")

h.handlePoll(writer, req.Context(), machine, mapRequest, false)
}
2 changes: 1 addition & 1 deletion hscontrol/poll_noise.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (ns *noiseServer) NoisePollNetMapHandler(
log.Debug().
Str("handler", "NoisePollNetMap").
Str("machine", machine.Hostname).
Msg("A machine is entering polling via the Noise protocol")
Msg("A machine sending a MapRequest with Noise protocol")

ns.headscale.handlePoll(writer, req.Context(), machine, mapRequest, true)
}

0 comments on commit 0b4dc82

Please sign in to comment.