From 4007ceb6bd754f2c375cf417ec4b14f2181eb129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=AF=E5=B0=A7?= Date: Tue, 30 Jul 2024 14:56:40 +0800 Subject: [PATCH] style: Only do code optimization --- api_request.go | 4 ++-- config.go | 15 +++++++-------- conn.go | 5 ++--- consumer.go | 20 +++++++++----------- consumer_test.go | 6 +++--- mock_test.go | 2 +- producer.go | 5 ++--- producer_test.go | 4 ++-- protocol.go | 20 ++++++++++---------- 9 files changed, 38 insertions(+), 43 deletions(-) diff --git a/api_request.go b/api_request.go index e565e7cf..b88984df 100644 --- a/api_request.go +++ b/api_request.go @@ -3,7 +3,7 @@ package nsq import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net" "net/http" "time" @@ -47,7 +47,7 @@ func apiRequestNegotiateV1(httpclient *http.Client, method string, endpoint stri return err } - respBody, err := ioutil.ReadAll(resp.Body) + respBody, err := io.ReadAll(resp.Body) resp.Body.Close() if err != nil { return err diff --git a/config.go b/config.go index 38fffe45..b7d877ee 100644 --- a/config.go +++ b/config.go @@ -5,7 +5,6 @@ import ( "crypto/x509" "errors" "fmt" - "io/ioutil" "log" "math" "math/rand" @@ -208,15 +207,15 @@ func NewConfig() *Config { // // Calls to Set() that take a time.Duration as an argument can be input as: // -// "1000ms" (a string parsed by time.ParseDuration()) -// 1000 (an integer interpreted as milliseconds) -// 1000*time.Millisecond (a literal time.Duration value) +// "1000ms" (a string parsed by time.ParseDuration()) +// 1000 (an integer interpreted as milliseconds) +// 1000*time.Millisecond (a literal time.Duration value) // // Calls to Set() that take bool can be input as: // -// "true" (a string parsed by strconv.ParseBool()) -// true (a boolean) -// 1 (an int where 1 == true and 0 == false) +// "true" (a string parsed by strconv.ParseBool()) +// true (a boolean) +// 1 (an int where 1 == true and 0 == false) // // It returns an error for an invalid option or value. func (c *Config) Set(option string, value interface{}) error { @@ -434,7 +433,7 @@ func (t *tlsConfig) Set(c *Config, option string, value interface{}) error { return fmt.Errorf("ERROR: %v is not a string", value) } tlsCertPool := x509.NewCertPool() - caCertFile, err := ioutil.ReadFile(filename) + caCertFile, err := os.ReadFile(filename) if err != nil { return fmt.Errorf("ERROR: failed to read custom Certificate Authority file %s", err) } diff --git a/conn.go b/conn.go index 321da690..8ec8a4ab 100644 --- a/conn.go +++ b/conn.go @@ -119,8 +119,7 @@ func NewConn(addr string, config *Config, delegate ConnDelegate) *Conn { // The logger parameter is an interface that requires the following // method to be implemented (such as the the stdlib log.Logger): // -// Output(calldepth int, s string) -// +// Output(calldepth int, s string) func (c *Conn) SetLogger(l logger, lvl LogLevel, format string) { c.logGuard.Lock() defer c.logGuard.Unlock() @@ -468,7 +467,7 @@ func (c *Conn) upgradeSnappy() error { conn = c.tlsConn } c.r = snappy.NewReader(conn) - c.w = snappy.NewWriter(conn) + c.w = snappy.NewBufferedWriter(conn) frameType, data, err := ReadUnpackedResponse(c, c.config.MaxMsgSize) if err != nil { return err diff --git a/consumer.go b/consumer.go index 390002a6..77d0acdc 100644 --- a/consumer.go +++ b/consumer.go @@ -33,9 +33,9 @@ type Handler interface { // HandlerFunc is a convenience type to avoid having to declare a struct // to implement the Handler interface, it can be used like this: // -// consumer.AddHandler(nsq.HandlerFunc(func(m *Message) error { -// // handle the message -// })) +// consumer.AddHandler(nsq.HandlerFunc(func(m *Message) error { +// // handle the message +// })) type HandlerFunc func(message *Message) error // HandleMessage implements the Handler interface @@ -220,8 +220,7 @@ func (r *Consumer) conns() []*Conn { // The logger parameter is an interface that requires the following // method to be implemented (such as the the stdlib log.Logger): // -// Output(calldepth int, s string) error -// +// Output(calldepth int, s string) error func (r *Consumer) SetLogger(l logger, lvl LogLevel) { r.logGuard.Lock() defer r.logGuard.Unlock() @@ -266,8 +265,7 @@ func (r *Consumer) getLogLevel() LogLevel { // of the following interfaces that modify the behavior // of the `Consumer`: // -// DiscoveryFilter -// +// DiscoveryFilter func (r *Consumer) SetBehaviorDelegate(cb interface{}) { matched := false @@ -312,7 +310,7 @@ func (r *Consumer) getMaxInFlight() int32 { // ChangeMaxInFlight sets a new maximum number of messages this comsumer instance // will allow in-flight, and updates all existing connections as appropriate. // -// For example, ChangeMaxInFlight(0) would pause message flow +// # For example, ChangeMaxInFlight(0) would pause message flow // // If already connected, it updates the reader RDY state for each connection. func (r *Consumer) ChangeMaxInFlight(maxInFlight int) { @@ -1109,7 +1107,7 @@ func (r *Consumer) stopHandlers() { // AddHandler sets the Handler for messages received by this Consumer. This can be called // multiple times to add additional handlers. Handler will have a 1:1 ratio to message handling goroutines. // -// This panics if called after connecting to NSQD or NSQ Lookupd +// # This panics if called after connecting to NSQD or NSQ Lookupd // // (see Handler or HandlerFunc for details on implementing this interface) func (r *Consumer) AddHandler(handler Handler) { @@ -1120,7 +1118,7 @@ func (r *Consumer) AddHandler(handler Handler) { // takes a second argument which indicates the number of goroutines to spawn for // message handling. // -// This panics if called after connecting to NSQD or NSQ Lookupd +// # This panics if called after connecting to NSQD or NSQ Lookupd // // (see Handler or HandlerFunc for details on implementing this interface) func (r *Consumer) AddConcurrentHandlers(handler Handler, concurrency int) { @@ -1228,7 +1226,7 @@ func buildLookupAddr(addr, topic string) (string, error) { u.Path = "/lookup" } - v, err := url.ParseQuery(u.RawQuery) + v, _ := url.ParseQuery(u.RawQuery) v.Add("topic", topic) u.RawQuery = v.Encode() return u.String(), nil diff --git a/consumer_test.go b/consumer_test.go index dcb34df0..945f5c0c 100644 --- a/consumer_test.go +++ b/consumer_test.go @@ -6,7 +6,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "log" "net" "net/http" @@ -25,7 +25,7 @@ type MyTestHandler struct { messagesFailed int } -var nullLogger = log.New(ioutil.Discard, "", log.LstdFlags) +var nullLogger = log.New(io.Discard, "", log.LstdFlags) func (h *MyTestHandler) LogFailedMessage(message *Message) { h.messagesFailed++ @@ -58,7 +58,7 @@ func (h *MyTestHandler) HandleMessage(message *Message) error { func SendMessage(t *testing.T, port int, topic string, method string, body []byte) { httpclient := &http.Client{} endpoint := fmt.Sprintf("http://127.0.0.1:%d/%s?topic=%s", port, method, topic) - req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(body)) + req, _ := http.NewRequest("POST", endpoint, bytes.NewBuffer(body)) resp, err := httpclient.Do(req) if err != nil { t.Fatalf(err.Error()) diff --git a/mock_test.go b/mock_test.go index 057442a3..44656630 100644 --- a/mock_test.go +++ b/mock_test.go @@ -184,7 +184,7 @@ func framedResponse(frameType int32, data []byte) []byte { return nil } - _, err = w.Write(data) + _, _ = w.Write(data) return w.Bytes() } diff --git a/producer.go b/producer.go index 853714f4..4019fefd 100644 --- a/producer.go +++ b/producer.go @@ -92,7 +92,7 @@ func NewProducer(addr string, config *Config) (*Producer, error) { // Set default logger for all log levels l := log.New(os.Stderr, "", log.Flags()) - for index, _ := range p.logger { + for index := range p.logger { p.logger[index] = l } return p, nil @@ -120,8 +120,7 @@ func (w *Producer) Ping() error { // The logger parameter is an interface that requires the following // method to be implemented (such as the the stdlib log.Logger): // -// Output(calldepth int, s string) -// +// Output(calldepth int, s string) func (w *Producer) SetLogger(l logger, lvl LogLevel) { w.logGuard.Lock() defer w.logGuard.Unlock() diff --git a/producer_test.go b/producer_test.go index e0cf7bf1..de895ad7 100755 --- a/producer_test.go +++ b/producer_test.go @@ -3,7 +3,7 @@ package nsq import ( "bytes" "errors" - "io/ioutil" + "io" "log" "net" "os" @@ -63,7 +63,7 @@ func TestProducerConnection(t *testing.T) { } func TestProducerPing(t *testing.T) { - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) defer log.SetOutput(os.Stdout) config := NewConfig() diff --git a/protocol.go b/protocol.go index 356c4d25..1d0e1a9d 100644 --- a/protocol.go +++ b/protocol.go @@ -46,11 +46,11 @@ func isValidName(name string) bool { // ReadResponse is a client-side utility function to read from the supplied Reader // according to the NSQ protocol spec: // -// [x][x][x][x][x][x][x][x]... -// | (int32) || (binary) -// | 4-byte || N-byte -// ------------------------... -// size data +// [x][x][x][x][x][x][x][x]... +// | (int32) || (binary) +// | 4-byte || N-byte +// ------------------------... +// size data func ReadResponse(r io.Reader, maxMsgSize int32) ([]byte, error) { var msgSize int32 @@ -84,11 +84,11 @@ func ReadResponse(r io.Reader, maxMsgSize int32) ([]byte, error) { // UnpackResponse is a client-side utility function that unpacks serialized data // according to NSQ protocol spec: // -// [x][x][x][x][x][x][x][x]... -// | (int32) || (binary) -// | 4-byte || N-byte -// ------------------------... -// frame ID data +// [x][x][x][x][x][x][x][x]... +// | (int32) || (binary) +// | 4-byte || N-byte +// ------------------------... +// frame ID data // // Returns a triplicate of: frame type, data ([]byte), error func UnpackResponse(response []byte) (int32, []byte, error) {