Skip to content

Commit

Permalink
Embed noCopy struct into structs, which mustn't be copied
Browse files Browse the repository at this point in the history
This should help `go vet` detecting invalid structs' copyings.
See golang/go#8005 (comment) for details.
  • Loading branch information
valyala committed Mar 4, 2016
1 parent 15e4645 commit 9fa69c7
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions args.go
Expand Up @@ -39,6 +39,8 @@ type Args struct {
args []argsKV
bufKV argsKV
buf []byte

noCopy
}

type argsKV struct {
Expand Down
4 changes: 4 additions & 0 deletions client.go
Expand Up @@ -201,6 +201,8 @@ type Client struct {
mLock sync.Mutex
m map[string]*HostClient
ms map[string]*HostClient

noCopy
}

// Get appends url contents to dst and returns it as body.
Expand Down Expand Up @@ -511,6 +513,8 @@ type HostClient struct {

readerPool sync.Pool
writerPool sync.Pool

noCopy
}

type clientConn struct {
Expand Down
2 changes: 2 additions & 0 deletions cookie.go
Expand Up @@ -55,6 +55,8 @@ type Cookie struct {

bufKV argsKV
buf []byte

noCopy
}

// CopyTo copies src cookie to c.
Expand Down
4 changes: 4 additions & 0 deletions header.go
Expand Up @@ -33,6 +33,8 @@ type ResponseHeader struct {
bufKV argsKV

cookies []argsKV

noCopy
}

// RequestHeader represents HTTP request header.
Expand Down Expand Up @@ -68,6 +70,8 @@ type RequestHeader struct {
cookies []argsKV

rawHeaders []byte

noCopy
}

// SetContentRange sets 'Content-Range: bytes startPos-endPos/contentLength'
Expand Down
4 changes: 4 additions & 0 deletions http.go
Expand Up @@ -37,6 +37,8 @@ type Request struct {
// Group bool members in order to reduce Request object size.
parsedURI bool
parsedPostArgs bool

noCopy
}

// Response represents HTTP response.
Expand All @@ -62,6 +64,8 @@ type Response struct {
// Response.Write() skips writing body if set to true.
// Use it for writing HEAD responses.
SkipBody bool

noCopy
}

// SetRequestURI sets RequestURI.
Expand Down
8 changes: 8 additions & 0 deletions nocopy.go
@@ -0,0 +1,8 @@
package fasthttp

// Embed this type into a struct, which mustn't be copied,
// so `go vet` gives a warning if this struct is copied.
//
// See https://github.com/golang/go/issues/8005#issuecomment-190753527 for details.
type noCopy struct {}
func (*noCopy) Lock() {}
4 changes: 4 additions & 0 deletions server.go
Expand Up @@ -270,6 +270,8 @@ type Server struct {
writerPool sync.Pool
hijackConnPool sync.Pool
bytePool sync.Pool

noCopy
}

// TimeoutHandler creates RequestHandler, which returns StatusRequestTimeout
Expand Down Expand Up @@ -374,6 +376,8 @@ type RequestCtx struct {
timeoutTimer *time.Timer

hijackHandler HijackHandler

noCopy
}

// HijackHandler must process the hijacked connection c.
Expand Down
2 changes: 2 additions & 0 deletions uri.go
Expand Up @@ -50,6 +50,8 @@ type URI struct {
requestURI []byte

h *RequestHeader

noCopy
}

// CopyTo copies uri contents to dst.
Expand Down

4 comments on commit 9fa69c7

@stemar94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you place the empty struct at the end of another struct it can happen that you increase the size of it.
You should place it at the first position, AFAIK.

@valyala
Copy link
Owner Author

@valyala valyala commented on 9fa69c7 Mar 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, @stemar94 !

The corresponding Go issue. Will move it at the first position.

@valyala
Copy link
Owner Author

@valyala valyala commented on 9fa69c7 Mar 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed at 8280b7a

@stemar94
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not providing the issue in first place.

Please sign in to comment.