From 434a0ccf2aff4a3b67693682f87b4411f688276c Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 24 Feb 2014 13:29:30 +0100 Subject: [PATCH] golint --- discover/discover.go | 2 +- main.go | 8 ++++---- protocol/common_test.go | 3 +-- protocol/protocol.go | 35 +++++++++++++++++------------------ protocol/protocol_test.go | 4 ++-- suppressor.go | 6 +++--- suppressor_test.go | 10 +++++----- tls.go | 2 +- xdr/reader.go | 2 +- 9 files changed, 35 insertions(+), 37 deletions(-) diff --git a/discover/discover.go b/discover/discover.go index e4902c85d..c7e4ffdf1 100644 --- a/discover/discover.go +++ b/discover/discover.go @@ -34,7 +34,7 @@ type Discoverer struct { } var ( - ErrIncorrectMagic = errors.New("Incorrect magic number") + ErrIncorrectMagic = errors.New("incorrect magic number") ) // We tolerate a certain amount of errors because we might be running on diff --git a/main.go b/main.go index 320374abf..853cb89f8 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ import ( ) var cfg Configuration -var Version string = "unknown-dev" +var Version = "unknown-dev" var ( myID string @@ -83,7 +83,7 @@ func main() { fatalErr(err) } - myID = string(certId(cert.Certificate[0])) + myID = string(certID(cert.Certificate[0])) log.SetPrefix("[" + myID[0:5] + "] ") logger.SetPrefix("[" + myID[0:5] + "] ") @@ -388,7 +388,7 @@ listen: continue } - remoteID := certId(tc.ConnectionState().PeerCertificates[0].Raw) + remoteID := certID(tc.ConnectionState().PeerCertificates[0].Raw) if remoteID == myID { warnf("Connect from myself (%s) - should not happen", remoteID) @@ -469,7 +469,7 @@ func connect(myID string, disc *discover.Discoverer, m *Model, tlsCfg *tls.Confi continue } - remoteID := certId(conn.ConnectionState().PeerCertificates[0].Raw) + remoteID := certID(conn.ConnectionState().PeerCertificates[0].Raw) if remoteID != nodeCfg.NodeID { warnln("Unexpected nodeID", remoteID, "!=", nodeCfg.NodeID) conn.Close() diff --git a/protocol/common_test.go b/protocol/common_test.go index 8912fc946..469c271ef 100644 --- a/protocol/common_test.go +++ b/protocol/common_test.go @@ -46,7 +46,6 @@ func (e *ErrPipe) Write(data []byte) (int, error) { e.PipeWriter.CloseWithError(e.err) e.closed = true return n, e.err - } else { - return e.PipeWriter.Write(data) } + return e.PipeWriter.Write(data) } diff --git a/protocol/protocol.go b/protocol/protocol.go index 2e9920e7a..c375b501f 100644 --- a/protocol/protocol.go +++ b/protocol/protocol.go @@ -31,7 +31,8 @@ const ( ) var ( - ErrClusterHash = fmt.Errorf("Configuration error: mismatched cluster hash") + ErrClusterHash = fmt.Errorf("configuration error: mismatched cluster hash") + ErrClosed = errors.New("connection closed") ) type Model interface { @@ -56,7 +57,7 @@ type Connection struct { xw *xdr.Writer closed bool awaiting map[int]chan asyncResult - nextId int + nextID int indexSent map[string]map[string][2]int64 peerOptions map[string]string myOptions map[string]string @@ -68,8 +69,6 @@ type Connection struct { statisticsLock sync.Mutex } -var ErrClosed = errors.New("Connection closed") - type asyncResult struct { val []byte err error @@ -105,7 +104,7 @@ func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver M c.myOptions = options go func() { c.Lock() - header{0, c.nextId, messageTypeOptions}.encodeXDR(c.xw) + header{0, c.nextID, messageTypeOptions}.encodeXDR(c.xw) var om OptionsMessage for k, v := range options { om.Options = append(om.Options, Option{k, v}) @@ -118,7 +117,7 @@ func NewConnection(nodeID string, reader io.Reader, writer io.Writer, receiver M if err != nil { log.Println("Warning: Write error during initial handshake:", err) } - c.nextId++ + c.nextID++ c.Unlock() }() } @@ -155,12 +154,12 @@ func (c *Connection) Index(repo string, idx []FileInfo) { idx = diff } - header{0, c.nextId, msgType}.encodeXDR(c.xw) + header{0, c.nextID, msgType}.encodeXDR(c.xw) _, err := IndexMessage{repo, idx}.encodeXDR(c.xw) if err == nil { err = c.flush() } - c.nextId = (c.nextId + 1) & 0xfff + c.nextID = (c.nextID + 1) & 0xfff c.hasSentIndex = true c.Unlock() @@ -178,8 +177,8 @@ func (c *Connection) Request(repo string, name string, offset int64, size int) ( return nil, ErrClosed } rc := make(chan asyncResult) - c.awaiting[c.nextId] = rc - header{0, c.nextId, messageTypeRequest}.encodeXDR(c.xw) + c.awaiting[c.nextID] = rc + header{0, c.nextID, messageTypeRequest}.encodeXDR(c.xw) _, err := RequestMessage{repo, name, uint64(offset), uint32(size)}.encodeXDR(c.xw) if err == nil { err = c.flush() @@ -189,7 +188,7 @@ func (c *Connection) Request(repo string, name string, offset int64, size int) ( c.close(err) return nil, err } - c.nextId = (c.nextId + 1) & 0xfff + c.nextID = (c.nextID + 1) & 0xfff c.Unlock() res, ok := <-rc @@ -206,8 +205,8 @@ func (c *Connection) ping() bool { return false } rc := make(chan asyncResult, 1) - c.awaiting[c.nextId] = rc - header{0, c.nextId, messageTypePing}.encodeXDR(c.xw) + c.awaiting[c.nextID] = rc + header{0, c.nextID, messageTypePing}.encodeXDR(c.xw) err := c.flush() if err != nil { c.Unlock() @@ -218,7 +217,7 @@ func (c *Connection) ping() bool { c.close(c.xw.Error()) return false } - c.nextId = (c.nextId + 1) & 0xfff + c.nextID = (c.nextID + 1) & 0xfff c.Unlock() res, ok := <-rc @@ -268,7 +267,7 @@ loop: break loop } if hdr.version != 0 { - c.close(fmt.Errorf("Protocol error: %s: unknown message version %#x", c.id, hdr.version)) + c.close(fmt.Errorf("protocol error: %s: unknown message version %#x", c.id, hdr.version)) break loop } @@ -371,7 +370,7 @@ loop: } default: - c.close(fmt.Errorf("Protocol error: %s: unknown message type %#x", c.id, hdr.msgType)) + c.close(fmt.Errorf("protocol error: %s: unknown message type %#x", c.id, hdr.msgType)) break loop } } @@ -410,10 +409,10 @@ func (c *Connection) pingerLoop() { select { case ok := <-rc: if !ok { - c.close(fmt.Errorf("Ping failure")) + c.close(fmt.Errorf("ping failure")) } case <-time.After(pingTimeout): - c.close(fmt.Errorf("Ping timeout")) + c.close(fmt.Errorf("ping timeout")) } } } diff --git a/protocol/protocol_test.go b/protocol/protocol_test.go index c7abb4397..c8f9cd3fb 100644 --- a/protocol/protocol_test.go +++ b/protocol/protocol_test.go @@ -38,7 +38,7 @@ func TestPing(t *testing.T) { } func TestPingErr(t *testing.T) { - e := errors.New("Something broke") + e := errors.New("something broke") for i := 0; i < 12; i++ { for j := 0; j < 12; j++ { @@ -64,7 +64,7 @@ func TestPingErr(t *testing.T) { } func TestRequestResponseErr(t *testing.T) { - e := errors.New("Something broke") + e := errors.New("something broke") var pass bool for i := 0; i < 48; i++ { diff --git a/suppressor.go b/suppressor.go index 5690207f8..0b1bdc5ea 100644 --- a/suppressor.go +++ b/suppressor.go @@ -6,7 +6,7 @@ import ( ) const ( - MAX_CHANGE_HISTORY = 4 + MaxChangeHistory = 4 ) type change struct { @@ -45,8 +45,8 @@ func (h changeHistory) bandwidth(t time.Time) int64 { func (h *changeHistory) append(size int64, t time.Time) { c := change{size, t} - if len(h.changes) == MAX_CHANGE_HISTORY { - h.changes = h.changes[1:MAX_CHANGE_HISTORY] + if len(h.changes) == MaxChangeHistory { + h.changes = h.changes[1:MaxChangeHistory] } h.changes = append(h.changes, c) } diff --git a/suppressor_test.go b/suppressor_test.go index 87eec2165..80f3d8241 100644 --- a/suppressor_test.go +++ b/suppressor_test.go @@ -84,29 +84,29 @@ func TestHistory(t *testing.T) { t.Errorf("Incorrect first record size %d", s) } - for i := 1; i < MAX_CHANGE_HISTORY; i++ { + for i := 1; i < MaxChangeHistory; i++ { h.append(int64(40+i), t0.Add(time.Duration(i)*time.Second)) } - if l := len(h.changes); l != MAX_CHANGE_HISTORY { + if l := len(h.changes); l != MaxChangeHistory { t.Errorf("Incorrect history length %d", l) } if s := h.changes[0].size; s != 40 { t.Errorf("Incorrect first record size %d", s) } - if s := h.changes[MAX_CHANGE_HISTORY-1].size; s != 40+MAX_CHANGE_HISTORY-1 { + if s := h.changes[MaxChangeHistory-1].size; s != 40+MaxChangeHistory-1 { t.Errorf("Incorrect last record size %d", s) } h.append(999, t0.Add(time.Duration(999)*time.Second)) - if l := len(h.changes); l != MAX_CHANGE_HISTORY { + if l := len(h.changes); l != MaxChangeHistory { t.Errorf("Incorrect history length %d", l) } if s := h.changes[0].size; s != 41 { t.Errorf("Incorrect first record size %d", s) } - if s := h.changes[MAX_CHANGE_HISTORY-1].size; s != 999 { + if s := h.changes[MaxChangeHistory-1].size; s != 999 { t.Errorf("Incorrect last record size %d", s) } diff --git a/tls.go b/tls.go index a8a0e12a6..7a848f063 100644 --- a/tls.go +++ b/tls.go @@ -25,7 +25,7 @@ func loadCert(dir string) (tls.Certificate, error) { return tls.LoadX509KeyPair(path.Join(dir, "cert.pem"), path.Join(dir, "key.pem")) } -func certId(bs []byte) string { +func certID(bs []byte) string { hf := sha256.New() hf.Write(bs) id := hf.Sum(nil) diff --git a/xdr/reader.go b/xdr/reader.go index 64be77f58..c29b7c4b9 100644 --- a/xdr/reader.go +++ b/xdr/reader.go @@ -5,7 +5,7 @@ import ( "io" ) -var ErrElementSizeExceeded = errors.New("Element size exceeded") +var ErrElementSizeExceeded = errors.New("element size exceeded") type Reader struct { r io.Reader