Skip to content

Commit

Permalink
Merge pull request #447 from lesismal/norace
Browse files Browse the repository at this point in the history
add //go:norace for all funcs
  • Loading branch information
lesismal authored Oct 10, 2024
2 parents e9d641b + 2d038d6 commit f4cca21
Show file tree
Hide file tree
Showing 45 changed files with 877 additions and 0 deletions.
9 changes: 9 additions & 0 deletions autobahn/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
statusFailed = "FAILED"
)

//go:norace
func failing(behavior string) bool {
switch behavior {
// case statusUnclean, statusFailed, statusNonStrict: // we should probably fix the nonstrict as well at some point
Expand All @@ -49,6 +50,7 @@ type statusCounter struct {
Failed int
}

//go:norace
func (c *statusCounter) Inc(s string) {
c.Total++
switch s {
Expand All @@ -69,6 +71,7 @@ func (c *statusCounter) Inc(s string) {
}
}

//go:norace
func main() {
log.SetFlags(0)
flag.Parse()
Expand Down Expand Up @@ -196,6 +199,7 @@ type entryReport struct {
Duration int `json:"duration"`
}

//go:norace
func decodeFile(path string, x interface{}) error {
f, err := os.Open(path)
if err != nil {
Expand All @@ -207,6 +211,7 @@ func decodeFile(path string, x interface{}) error {
return d.Decode(x)
}

//go:norace
func compareBySegment(a, b string) int {
as := strings.Split(a, ".")
bs := strings.Split(b, ".")
Expand All @@ -221,6 +226,7 @@ func compareBySegment(a, b string) int {
return len(b) - len(a)
}

//go:norace
func mustInt(s string) int64 {
const bits = 32 << (^uint(0) >> 63)
x, err := strconv.ParseInt(s, 10, bits)
Expand All @@ -230,13 +236,15 @@ func mustInt(s string) int64 {
return x
}

//go:norace
func min(a, b int) int {
if a < b {
return a
}
return b
}

//go:norace
func handlerIndex() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
Expand All @@ -262,6 +270,7 @@ var index = template.Must(template.New("").Parse(`
</html>
`))

//go:norace
func sortBySegment(s []string) {
sort.Slice(s, func(i, j int) bool {
return compareBySegment(s[i], s[j]) < 0
Expand Down
4 changes: 4 additions & 0 deletions autobahn/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/lesismal/nbio/taskpool"
)

//go:norace
func newUpgrader(isDataFrame bool) *websocket.Upgrader {
u := websocket.NewUpgrader()
u.EnableCompression(true)
Expand All @@ -40,6 +41,7 @@ func newUpgrader(isDataFrame bool) *websocket.Upgrader {
return u
}

//go:norace
func onWebsocketFrame(w http.ResponseWriter, r *http.Request) {
upgrader := newUpgrader(true)
conn, err := upgrader.Upgrade(w, r, nil)
Expand All @@ -49,6 +51,7 @@ func onWebsocketFrame(w http.ResponseWriter, r *http.Request) {
conn.SetDeadline(time.Time{})
}

//go:norace
func onWebsocketMessage(w http.ResponseWriter, r *http.Request) {
upgrader := newUpgrader(false)
conn, err := upgrader.Upgrade(w, r, nil)
Expand All @@ -58,6 +61,7 @@ func onWebsocketMessage(w http.ResponseWriter, r *http.Request) {
conn.SetDeadline(time.Time{})
}

//go:norace
func main() {
cert, err := tls.X509KeyPair(rsaCertPEM, rsaKeyPEM)
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,22 @@ const (
)

// Type .
//
//go:norace
func (c *Conn) Type() ConnType {
return c.typ
}

// IsTCP returns whether this Conn is a TCP Conn.
//
//go:norace
func (c *Conn) IsTCP() bool {
return c.typ == ConnTypeTCP
}

// IsUDP returns whether this Conn is a UDP Conn.
//
//go:norace
func (c *Conn) IsUDP() bool {
switch c.typ {
case ConnTypeUDPServer, ConnTypeUDPClientFromDial, ConnTypeUDPClientFromRead:
Expand All @@ -51,16 +57,22 @@ func (c *Conn) IsUDP() bool {
}

// IsUnix returns whether this Conn is a Unix Conn.
//
//go:norace
func (c *Conn) IsUnix() bool {
return c.typ == ConnTypeUnix
}

// Session returns user session.
//
//go:norace
func (c *Conn) Session() interface{} {
return c.session
}

// SetSession sets user session.
//
//go:norace
func (c *Conn) SetSession(session interface{}) {
c.session = session
}
Expand All @@ -80,16 +92,22 @@ func (c *Conn) SetSession(session interface{}) {
// })
// conn1.OnData(yourDatahandler1)
// conn2.OnData(yourDatahandler2)
//
//go:norace
func (c *Conn) OnData(h func(conn *Conn, data []byte)) {
c.dataHandler = h
}

// DataHandler returns Conn's data handler.
//
//go:norace
func (c *Conn) DataHandler() func(conn *Conn, data []byte) {
return c.dataHandler
}

// Dial calls net.Dial to make a net.Conn and convert it to *nbio.Conn.
//
//go:norace
func Dial(network string, address string) (*Conn, error) {
conn, err := net.Dial(network, address)
if err != nil {
Expand All @@ -99,6 +117,8 @@ func Dial(network string, address string) (*Conn, error) {
}

// Dial calls net.DialTimeout to make a net.Conn and convert it to *nbio.Conn.
//
//go:norace
func DialTimeout(network string, address string, timeout time.Duration) (*Conn, error) {
conn, err := net.DialTimeout(network, address, timeout)
if err != nil {
Expand All @@ -108,21 +128,29 @@ func DialTimeout(network string, address string, timeout time.Duration) (*Conn,
}

// Lock .
//
//go:norace
func (c *Conn) Lock() {
c.mux.Lock()
}

// Unlock .
//
//go:norace
func (c *Conn) Unlock() {
c.mux.Unlock()
}

// IsClosed returns whether the Conn is closed.
//
//go:norace
func (c *Conn) IsClosed() (bool, error) {
return c.closed, c.closeErr
}

// ExecuteLen returns the length of the Conn's job list.
//
//go:norace
func (c *Conn) ExecuteLen() int {
c.mux.Lock()
n := len(c.jobList)
Expand Down Expand Up @@ -152,6 +180,8 @@ func (c *Conn) ExecuteLen() int {
// connection is closed.
// 2. nbio.Engine.Execute is handled by a goroutine pool by default, users
// can customize it.
//
//go:norace
func (c *Conn) Execute(job func()) bool {
c.mux.Lock()
if c.closed {
Expand All @@ -176,6 +206,8 @@ func (c *Conn) Execute(job func()) bool {
// back of the job list no matter whether Conn has been closed,
// it guarantees the job to be executed.
// This is used to handle the close event in nbio/nbhttp.
//
//go:norace
func (c *Conn) MustExecute(job func()) {
c.mux.Lock()
isHead := (len(c.jobList) == 0)
Expand All @@ -189,6 +221,7 @@ func (c *Conn) MustExecute(job func()) {
}
}

//go:norace
func (c *Conn) execute(job func()) {
c.p.g.Execute(func() {
i := 0
Expand Down
Loading

0 comments on commit f4cca21

Please sign in to comment.