Skip to content

Commit

Permalink
Merge pull request #369 from qaqhy/optimize_code
Browse files Browse the repository at this point in the history
style: Only do code optimization
  • Loading branch information
mreiferson authored Sep 1, 2024
2 parents c2c3842 + 4007ceb commit 8098697
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 43 deletions.
4 changes: 2 additions & 2 deletions api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nsq
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"log"
"math"
"math/rand"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
20 changes: 9 additions & 11 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand All @@ -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++
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func framedResponse(frameType int32, data []byte) []byte {
return nil
}

_, err = w.Write(data)
_, _ = w.Write(data)
return w.Bytes()
}

Expand Down
5 changes: 2 additions & 3 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package nsq
import (
"bytes"
"errors"
"io/ioutil"
"io"
"log"
"net"
"os"
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8098697

Please sign in to comment.