Skip to content
go-libfossil Artifact [975f80ebc7]
Login

Artifact [975f80ebc7]

Artifact 975f80ebc79e41bedbe8296a4394c3dfbb2b09182a1260c718ffa2736e96b574:


//go:build !libfossil_nohttp

package sync

// This file holds the net/http peer address trace. A build with the libfossil_nohttp tag
// leaves it out, and with it net/http, for a host that supplies its own
// transport (docs: Install, TinyGo).

import (
	"context"
	"net/http/httptrace"
)

// Trace returns ctx with an HTTP trace that records the address of the
// connection a request made with it goes out on.
func (p *PeerAddr) Trace(ctx context.Context) context.Context {
	if p == nil {
		panic("sync.PeerAddr.Trace: receiver must not be nil")
	}
	return httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
		GotConn: func(info httptrace.GotConnInfo) {
			if info.Conn == nil {
				return
			}
			ip := hostOnly(info.Conn.RemoteAddr().String())
			p.mu.Lock()
			p.ip = ip
			p.mu.Unlock()
		},
	})
}