Skip to content

Commit

Permalink
Clean up pointer usage consistency.
Browse files Browse the repository at this point in the history
This tries to make the same functions emit and consume the same type of
data all over the application.

If a function transform data, it should emit new data, not a pointer.
  • Loading branch information
kradalby committed Oct 4, 2021
1 parent 94ba518 commit 2eb57e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 28 deletions.
9 changes: 5 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (h *Headscale) RegistrationHandler(c *gin.Context) {
c.Data(200, "application/json; charset=utf-8", respBody)
}

func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m *Machine) (*[]byte, error) {
func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m *Machine) ([]byte, error) {
log.Trace().
Str("func", "getMapResponse").
Str("machine", req.Hostinfo.Hostname).
Expand Down Expand Up @@ -277,6 +277,7 @@ func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m *Ma
log.Trace().
Str("func", "getMapResponse").
Str("machine", req.Hostinfo.Hostname).
Interface("payload", resp).
Msgf("Generated map response: %s", tailMapResponseToString(resp))

var respBody []byte
Expand All @@ -299,10 +300,10 @@ func (h *Headscale) getMapResponse(mKey wgkey.Key, req tailcfg.MapRequest, m *Ma
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(len(respBody)))
data = append(data, respBody...)
return &data, nil
return data, nil
}

func (h *Headscale) getMapKeepAliveResponse(mKey wgkey.Key, req tailcfg.MapRequest, m *Machine) (*[]byte, error) {
func (h *Headscale) getMapKeepAliveResponse(mKey wgkey.Key, req tailcfg.MapRequest, m *Machine) ([]byte, error) {
resp := tailcfg.MapResponse{
KeepAlive: true,
}
Expand All @@ -325,7 +326,7 @@ func (h *Headscale) getMapKeepAliveResponse(mKey wgkey.Key, req tailcfg.MapReque
data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(len(respBody)))
data = append(data, respBody...)
return &data, nil
return data, nil
}

func (h *Headscale) handleAuthKey(c *gin.Context, db *gorm.DB, idKey wgkey.Key, req tailcfg.RegisterRequest, m Machine) {
Expand Down
41 changes: 22 additions & 19 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,29 @@ func (m Machine) isAlreadyRegistered() bool {
return m.Registered
}

func (h *Headscale) getDirectPeers(m *Machine) (MachinesP, error) {
func (h *Headscale) getDirectPeers(m *Machine) (Machines, error) {
log.Trace().
Str("func", "getDirectPeers").
Str("machine", m.Name).
Msg("Finding peers")
Msg("Finding direct peers")

machines := []Machine{}
machines := Machines{}
if err := h.db.Where("namespace_id = ? AND machine_key <> ? AND registered",
m.NamespaceID, m.MachineKey).Find(&machines).Error; err != nil {
log.Error().Err(err).Msg("Error accessing db")
return nil, err
}

peers := make(MachinesP, 0)
for _, peer := range machines {
peers = append(peers, &peer)
}

sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
sort.Slice(machines, func(i, j int) bool { return machines[i].ID < machines[j].ID })

log.Trace().
Str("func", "getDirectPeers").
Str("func", "getDirectmachines").
Str("machine", m.Name).
Msgf("Found peers: %s", peers.String())
return peers, nil
Msgf("Found direct machines: %s", machines.String())
return machines, nil
}

func (h *Headscale) getShared(m *Machine) (MachinesP, error) {
func (h *Headscale) getShared(m *Machine) (Machines, error) {
log.Trace().
Str("func", "getShared").
Str("machine", m.Name).
Expand All @@ -96,9 +91,9 @@ func (h *Headscale) getShared(m *Machine) (MachinesP, error) {
return nil, err
}

peers := make(MachinesP, 0)
peers := make(Machines, 0)
for _, sharedMachine := range sharedMachines {
peers = append(peers, &sharedMachine.Machine)
peers = append(peers, sharedMachine.Machine)
}

sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
Expand All @@ -110,7 +105,7 @@ func (h *Headscale) getShared(m *Machine) (MachinesP, error) {
return peers, nil
}

func (h *Headscale) getPeers(m *Machine) (MachinesP, error) {
func (h *Headscale) getPeers(m *Machine) (Machines, error) {
direct, err := h.getDirectPeers(m)
if err != nil {
log.Error().
Expand All @@ -129,7 +124,15 @@ func (h *Headscale) getPeers(m *Machine) (MachinesP, error) {
return nil, err
}

return append(direct, shared...), nil
peers := append(direct, shared...)
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })

log.Trace().
Str("func", "getShared").
Str("machine", m.Name).
Msgf("Found total peers: %s", peers.String())

return peers, nil
}

// GetMachine finds a Machine by name and namespace and returns the Machine struct
Expand Down Expand Up @@ -227,7 +230,7 @@ func (h *Headscale) notifyChangesToPeers(m *Machine) {
Str("peer", peer.Name).
Str("address", peer.IPAddress).
Msgf("Notifying peer %s (%s)", peer.Name, peer.IPAddress)
err := h.sendRequestOnUpdateChannel(peer)
err := h.sendRequestOnUpdateChannel(&peer)
if err != nil {
log.Info().
Str("func", "notifyChangesToPeers").
Expand Down Expand Up @@ -357,7 +360,7 @@ func (ms MachinesP) String() string {
return fmt.Sprintf("[ %s ](%d)", strings.Join(temp, ", "), len(temp))
}

func (ms MachinesP) toNodes(includeRoutes bool) ([]*tailcfg.Node, error) {
func (ms Machines) toNodes(includeRoutes bool) ([]*tailcfg.Node, error) {
nodes := make([]*tailcfg.Node, len(ms))

for index, machine := range ms {
Expand Down
10 changes: 5 additions & 5 deletions poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
Str("handler", "PollNetMap").
Str("machine", m.Name).
Msg("Client is starting up. Probably interested in a DERP map")
c.Data(200, "application/json; charset=utf-8", *data)
c.Data(200, "application/json; charset=utf-8", data)
return
}

Expand Down Expand Up @@ -155,7 +155,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
Str("handler", "PollNetMap").
Str("machine", m.Name).
Msg("Client sent endpoint update and is ok with a response without peer list")
c.Data(200, "application/json; charset=utf-8", *data)
c.Data(200, "application/json; charset=utf-8", data)

// It sounds like we should update the nodes when we have received a endpoint update
// even tho the comments in the tailscale code dont explicitly say so.
Expand All @@ -179,7 +179,7 @@ func (h *Headscale) PollNetMapHandler(c *gin.Context) {
Str("handler", "PollNetMap").
Str("machine", m.Name).
Msg("Sending initial map")
go func() { pollDataChan <- *data }()
go func() { pollDataChan <- data }()

log.Info().
Str("handler", "PollNetMap").
Expand Down Expand Up @@ -342,7 +342,7 @@ func (h *Headscale) PollNetMapStream(
Err(err).
Msg("Could not get the map update")
}
_, err = w.Write(*data)
_, err = w.Write(data)
if err != nil {
log.Error().
Str("handler", "PollNetMapStream").
Expand Down Expand Up @@ -473,7 +473,7 @@ func (h *Headscale) scheduledPollWorker(
Str("func", "keepAlive").
Str("machine", m.Name).
Msg("Sending keepalive")
keepAliveChan <- *data
keepAliveChan <- data

case <-updateCheckerTicker.C:
// Send an update request regardless of outdated or not, if data is sent
Expand Down

0 comments on commit 2eb57e6

Please sign in to comment.