Skip to content

Commit

Permalink
feat: enable unparam lint
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <fuweid89@gmail.com>
  • Loading branch information
fuweid committed Oct 9, 2023
1 parent 89983f4 commit 313be92
Show file tree
Hide file tree
Showing 41 changed files with 118 additions and 137 deletions.
2 changes: 1 addition & 1 deletion client/internal/v2/cancelreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package client

import "net/http"

func requestCanceler(tr CancelableTransport, req *http.Request) func() {
func requestCanceler(req *http.Request) func() {
ch := make(chan struct{})
req.Cancel = ch

Expand Down
2 changes: 1 addition & 1 deletion client/internal/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func (c *simpleHTTPClient) Do(ctx context.Context, act httpAction) (*http.Respon
}
defer hcancel()

reqcancel := requestCanceler(c.transport, req)
reqcancel := requestCanceler(req)

rtchan := make(chan roundTripResponse, 1)
go func() {
Expand Down
2 changes: 1 addition & 1 deletion client/internal/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ func (f fakeCancelContext) Done() <-chan struct{} {
func (f fakeCancelContext) Err() error { return errFakeCancelContext }
func (f fakeCancelContext) Value(key any) any { return 1 }

func withTimeout(parent context.Context, timeout time.Duration) (
func withTimeout(parent context.Context, _timeout time.Duration) (
ctx context.Context,
cancel context.CancelFunc) {
ctx = parent
Expand Down
2 changes: 1 addition & 1 deletion client/pkg/transport/keepalive_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestNewKeepAliveListener(t *testing.T) {
t.Fatalf("unable to create tmpfile: %v", err)
}
tlsInfo := TLSInfo{CertFile: tlsinfo.CertFile, KeyFile: tlsinfo.KeyFile}
tlsInfo.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
tlsInfo.parseFunc = fakeCertificateParserFunc(nil)
tlscfg, err := tlsInfo.ServerConfig()
if err != nil {
t.Fatalf("unexpected serverConfig error: %v", err)
Expand Down
10 changes: 3 additions & 7 deletions client/pkg/transport/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,7 @@ func newListener(addr, scheme string, opts ...ListenerOption) (net.Listener, err
switch {
case lnOpts.IsSocketOpts():
// new ListenConfig with socket options.
config, err := newListenConfig(lnOpts.socketOpts)
if err != nil {
return nil, err
}
lnOpts.ListenConfig = config
lnOpts.ListenConfig = newListenConfig(lnOpts.socketOpts)
// check for timeout
fallthrough
case lnOpts.IsTimeout(), lnOpts.IsSocketOpts():
Expand Down Expand Up @@ -129,15 +125,15 @@ func wrapTLS(scheme string, tlsinfo *TLSInfo, l net.Listener) (net.Listener, err
return newTLSListener(l, tlsinfo, checkSAN)
}

func newListenConfig(sopts *SocketOpts) (net.ListenConfig, error) {
func newListenConfig(sopts *SocketOpts) net.ListenConfig {
lc := net.ListenConfig{}
if sopts != nil {
ctls := getControls(sopts)
if len(ctls) > 0 {
lc.Control = ctls.Control
}
}
return lc, nil
return lc
}

type TLSInfo struct {
Expand Down
12 changes: 6 additions & 6 deletions client/pkg/transport/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"go.uber.org/zap/zaptest"
)

func createSelfCert(t *testing.T, hosts ...string) (*TLSInfo, error) {
func createSelfCert(t *testing.T) (*TLSInfo, error) {
return createSelfCertEx(t, "127.0.0.1")
}

Expand All @@ -41,9 +41,9 @@ func createSelfCertEx(t *testing.T, host string, additionalUsages ...x509.ExtKey
return &info, nil
}

func fakeCertificateParserFunc(cert tls.Certificate, err error) func(certPEMBlock, keyPEMBlock []byte) (tls.Certificate, error) {
func fakeCertificateParserFunc(err error) func(certPEMBlock, keyPEMBlock []byte) (tls.Certificate, error) {
return func(certPEMBlock, keyPEMBlock []byte) (tls.Certificate, error) {
return cert, err
return tls.Certificate{}, err
}
}

Expand Down Expand Up @@ -367,7 +367,7 @@ func TestNewTransportTLSInfo(t *testing.T) {
}

for i, tt := range tests {
tt.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
tt.parseFunc = fakeCertificateParserFunc(nil)
trans, err := NewTransport(tt, time.Second)
if err != nil {
t.Fatalf("Received unexpected error from NewTransport: %v", err)
Expand Down Expand Up @@ -458,7 +458,7 @@ func TestTLSInfoParseFuncError(t *testing.T) {
}

for i, tt := range tests {
tt.info.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, errors.New("fake"))
tt.info.parseFunc = fakeCertificateParserFunc(errors.New("fake"))

if _, err = tt.info.ServerConfig(); err == nil {
t.Errorf("#%d: expected non-nil error from ServerConfig()", i)
Expand Down Expand Up @@ -496,7 +496,7 @@ func TestTLSInfoConfigFuncs(t *testing.T) {
}

for i, tt := range tests {
tt.info.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
tt.info.parseFunc = fakeCertificateParserFunc(nil)

sCfg, err := tt.info.ServerConfig()
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions client/v3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ func (c *Client) autoSync() {
}

// dialSetupOpts gives the dial opts prior to any authentication.
func (c *Client) dialSetupOpts(creds grpccredentials.TransportCredentials, dopts ...grpc.DialOption) (opts []grpc.DialOption, err error) {
func (c *Client) dialSetupOpts(creds grpccredentials.TransportCredentials, dopts ...grpc.DialOption) []grpc.DialOption {
var opts []grpc.DialOption

if c.cfg.DialKeepAliveTime > 0 {
params := keepalive.ClientParameters{
Time: c.cfg.DialKeepAliveTime,
Expand Down Expand Up @@ -248,7 +250,7 @@ func (c *Client) dialSetupOpts(creds grpccredentials.TransportCredentials, dopts
grpc.WithUnaryInterceptor(c.unaryClientInterceptor(withMax(defaultUnaryMaxRetries), rrBackoff)),
)

return opts, nil
return opts
}

// Dial connects to a single endpoint using the client's config.
Expand Down Expand Up @@ -289,10 +291,8 @@ func (c *Client) dialWithBalancer(dopts ...grpc.DialOption) (*grpc.ClientConn, e

// dial configures and dials any grpc balancer target.
func (c *Client) dial(creds grpccredentials.TransportCredentials, dopts ...grpc.DialOption) (*grpc.ClientConn, error) {
opts, err := c.dialSetupOpts(creds, dopts...)
if err != nil {
return nil, fmt.Errorf("failed to configure dialer: %v", err)
}
opts := c.dialSetupOpts(creds, dopts...)

if c.authTokenBundle != nil {
opts = append(opts, grpc.WithPerRPCCredentials(c.authTokenBundle.PerRPCCredentials()))
}
Expand Down
2 changes: 1 addition & 1 deletion client/v3/concurrency/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (e *Election) Campaign(ctx context.Context, val string) error {
}
}

_, err = waitDeletes(ctx, client, e.keyPrefix, e.leaderRev-1)
err = waitDeletes(ctx, client, e.keyPrefix, e.leaderRev-1)
if err != nil {
// clean up in case of context cancel
select {
Expand Down
9 changes: 4 additions & 5 deletions client/v3/concurrency/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"errors"

pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/mvccpb"
v3 "go.etcd.io/etcd/client/v3"
)
Expand Down Expand Up @@ -47,19 +46,19 @@ func waitDelete(ctx context.Context, client *v3.Client, key string, rev int64) e

// waitDeletes efficiently waits until all keys matching the prefix and no greater
// than the create revision are deleted.
func waitDeletes(ctx context.Context, client *v3.Client, pfx string, maxCreateRev int64) (*pb.ResponseHeader, error) {
func waitDeletes(ctx context.Context, client *v3.Client, pfx string, maxCreateRev int64) error {
getOpts := append(v3.WithLastCreate(), v3.WithMaxCreateRev(maxCreateRev))
for {
resp, err := client.Get(ctx, pfx, getOpts...)
if err != nil {
return nil, err
return err
}
if len(resp.Kvs) == 0 {
return resp.Header, nil
return nil
}
lastKey := string(resp.Kvs[0].Key)
if err = waitDelete(ctx, client, lastKey, resp.Header.Revision); err != nil {
return nil, err
return err
}
}
}
2 changes: 1 addition & 1 deletion client/v3/concurrency/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func exampleEndpoints() []string { return nil }

func forUnitTestsRunInMockedContext(mocking func(), example func()) {
func forUnitTestsRunInMockedContext(mocking func(), _example func()) {
mocking()
// TODO: Call 'example' when mocking() provides realistic mocking of transport.

Expand Down
2 changes: 1 addition & 1 deletion client/v3/concurrency/mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (m *Mutex) Lock(ctx context.Context) error {
client := m.s.Client()
// wait for deletion revisions prior to myKey
// TODO: early termination if the session key is deleted before other session keys with smaller revisions.
_, werr := waitDeletes(ctx, client, m.pfx, m.myRev-1)
werr := waitDeletes(ctx, client, m.pfx, m.myRev-1)
// release lock key if wait failed
if werr != nil {
m.Unlock(client.Ctx())
Expand Down
2 changes: 1 addition & 1 deletion client/v3/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (

func exampleEndpoints() []string { return nil }

func forUnitTestsRunInMockedContext(mocking func(), example func()) {
func forUnitTestsRunInMockedContext(mocking func(), _example func()) {
mocking()
// TODO: Call 'example' when mocking() provides realistic mocking of transport.

Expand Down
34 changes: 17 additions & 17 deletions client/v3/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func RetryKVClient(c *Client) pb.KVClient {
}
}
func (rkv *retryKVClient) Range(ctx context.Context, in *pb.RangeRequest, opts ...grpc.CallOption) (resp *pb.RangeResponse, err error) {
return rkv.kc.Range(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rkv.kc.Range(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rkv *retryKVClient) Put(ctx context.Context, in *pb.PutRequest, opts ...grpc.CallOption) (resp *pb.PutResponse, err error) {
Expand Down Expand Up @@ -133,23 +133,23 @@ func RetryLeaseClient(c *Client) pb.LeaseClient {
}

func (rlc *retryLeaseClient) LeaseTimeToLive(ctx context.Context, in *pb.LeaseTimeToLiveRequest, opts ...grpc.CallOption) (resp *pb.LeaseTimeToLiveResponse, err error) {
return rlc.lc.LeaseTimeToLive(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rlc.lc.LeaseTimeToLive(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rlc *retryLeaseClient) LeaseLeases(ctx context.Context, in *pb.LeaseLeasesRequest, opts ...grpc.CallOption) (resp *pb.LeaseLeasesResponse, err error) {
return rlc.lc.LeaseLeases(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rlc.lc.LeaseLeases(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rlc *retryLeaseClient) LeaseGrant(ctx context.Context, in *pb.LeaseGrantRequest, opts ...grpc.CallOption) (resp *pb.LeaseGrantResponse, err error) {
return rlc.lc.LeaseGrant(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rlc.lc.LeaseGrant(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rlc *retryLeaseClient) LeaseRevoke(ctx context.Context, in *pb.LeaseRevokeRequest, opts ...grpc.CallOption) (resp *pb.LeaseRevokeResponse, err error) {
return rlc.lc.LeaseRevoke(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rlc.lc.LeaseRevoke(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rlc *retryLeaseClient) LeaseKeepAlive(ctx context.Context, opts ...grpc.CallOption) (stream pb.Lease_LeaseKeepAliveClient, err error) {
return rlc.lc.LeaseKeepAlive(ctx, append(opts, withRetryPolicy(repeatable))...)
return rlc.lc.LeaseKeepAlive(ctx, append(opts, withRepeatablePolicy())...)
}

type retryClusterClient struct {
Expand All @@ -164,7 +164,7 @@ func RetryClusterClient(c *Client) pb.ClusterClient {
}

func (rcc *retryClusterClient) MemberList(ctx context.Context, in *pb.MemberListRequest, opts ...grpc.CallOption) (resp *pb.MemberListResponse, err error) {
return rcc.cc.MemberList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rcc.cc.MemberList(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rcc *retryClusterClient) MemberAdd(ctx context.Context, in *pb.MemberAddRequest, opts ...grpc.CallOption) (resp *pb.MemberAddResponse, err error) {
Expand Down Expand Up @@ -195,27 +195,27 @@ func RetryMaintenanceClient(c *Client, conn *grpc.ClientConn) pb.MaintenanceClie
}

func (rmc *retryMaintenanceClient) Alarm(ctx context.Context, in *pb.AlarmRequest, opts ...grpc.CallOption) (resp *pb.AlarmResponse, err error) {
return rmc.mc.Alarm(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rmc.mc.Alarm(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rmc *retryMaintenanceClient) Status(ctx context.Context, in *pb.StatusRequest, opts ...grpc.CallOption) (resp *pb.StatusResponse, err error) {
return rmc.mc.Status(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rmc.mc.Status(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rmc *retryMaintenanceClient) Hash(ctx context.Context, in *pb.HashRequest, opts ...grpc.CallOption) (resp *pb.HashResponse, err error) {
return rmc.mc.Hash(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rmc.mc.Hash(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rmc *retryMaintenanceClient) HashKV(ctx context.Context, in *pb.HashKVRequest, opts ...grpc.CallOption) (resp *pb.HashKVResponse, err error) {
return rmc.mc.HashKV(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rmc.mc.HashKV(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rmc *retryMaintenanceClient) Snapshot(ctx context.Context, in *pb.SnapshotRequest, opts ...grpc.CallOption) (stream pb.Maintenance_SnapshotClient, err error) {
return rmc.mc.Snapshot(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rmc.mc.Snapshot(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rmc *retryMaintenanceClient) MoveLeader(ctx context.Context, in *pb.MoveLeaderRequest, opts ...grpc.CallOption) (resp *pb.MoveLeaderResponse, err error) {
return rmc.mc.MoveLeader(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rmc.mc.MoveLeader(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rmc *retryMaintenanceClient) Defragment(ctx context.Context, in *pb.DefragmentRequest, opts ...grpc.CallOption) (resp *pb.DefragmentResponse, err error) {
Expand All @@ -238,19 +238,19 @@ func RetryAuthClient(c *Client) pb.AuthClient {
}

func (rac *retryAuthClient) UserList(ctx context.Context, in *pb.AuthUserListRequest, opts ...grpc.CallOption) (resp *pb.AuthUserListResponse, err error) {
return rac.ac.UserList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rac.ac.UserList(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rac *retryAuthClient) UserGet(ctx context.Context, in *pb.AuthUserGetRequest, opts ...grpc.CallOption) (resp *pb.AuthUserGetResponse, err error) {
return rac.ac.UserGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rac.ac.UserGet(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rac *retryAuthClient) RoleGet(ctx context.Context, in *pb.AuthRoleGetRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleGetResponse, err error) {
return rac.ac.RoleGet(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rac.ac.RoleGet(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rac *retryAuthClient) RoleList(ctx context.Context, in *pb.AuthRoleListRequest, opts ...grpc.CallOption) (resp *pb.AuthRoleListResponse, err error) {
return rac.ac.RoleList(ctx, in, append(opts, withRetryPolicy(repeatable))...)
return rac.ac.RoleList(ctx, in, append(opts, withRepeatablePolicy())...)
}

func (rac *retryAuthClient) AuthEnable(ctx context.Context, in *pb.AuthEnableRequest, opts ...grpc.CallOption) (resp *pb.AuthEnableResponse, err error) {
Expand Down
6 changes: 3 additions & 3 deletions client/v3/retry_interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,10 @@ var (
// with the next iteration.
type backoffFunc func(attempt uint) time.Duration

// withRetryPolicy sets the retry policy of this call.
func withRetryPolicy(rp retryPolicy) retryOption {
// withRepeatablePolicy sets the repeatable policy of this call.
func withRepeatablePolicy() retryOption {
return retryOption{applyFunc: func(o *options) {
o.retryPolicy = rp
o.retryPolicy = repeatable
}}
}

Expand Down
5 changes: 0 additions & 5 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,6 @@ function govet_shadow_pass {
run_for_modules generic_checker govet_shadow_per_package "${shadow}"
}

function unparam_pass {
# TODO: transport/listener.go:129:60: newListenConfig - result 1 (error) is always nil
run_for_modules generic_checker run_go_tool "mvdan.cc/unparam"
}

function lint_pass {
run_for_modules generic_checker run golangci-lint run --config "${ETCD_ROOT_DIR}/tools/.golangci.yaml"
}
Expand Down
16 changes: 6 additions & 10 deletions server/embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,10 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
}
e.Server.Start()

if err = e.servePeers(); err != nil {
return e, err
}
if err = e.serveClients(); err != nil {
return e, err
}
e.servePeers()

e.serveClients()

if err = e.serveMetrics(); err != nil {
return e, err
}
Expand Down Expand Up @@ -561,7 +559,7 @@ func configurePeerListeners(cfg *Config) (peers []*peerListener, err error) {
}

// configure peer handlers after rafthttp.Transport started
func (e *Etcd) servePeers() (err error) {
func (e *Etcd) servePeers() {
ph := etcdhttp.NewPeerHandler(e.GetLogger(), e.Server)

for _, p := range e.Peers {
Expand Down Expand Up @@ -609,7 +607,6 @@ func (e *Etcd) servePeers() (err error) {
e.errHandler(l.serve())
}(pl)
}
return nil
}

func configureClientListeners(cfg *Config) (sctxs map[string]*serveCtx, err error) {
Expand Down Expand Up @@ -727,7 +724,7 @@ func resolveUrl(u url.URL) (addr string, secure bool, network string) {
return addr, secure, network
}

func (e *Etcd) serveClients() (err error) {
func (e *Etcd) serveClients() {
if !e.cfg.ClientTLSInfo.Empty() {
e.cfg.logger.Info(
"starting with client TLS",
Expand Down Expand Up @@ -771,7 +768,6 @@ func (e *Etcd) serveClients() (err error) {
e.errHandler(s.serve(e.Server, &e.cfg.ClientTLSInfo, mux, e.errHandler, e.grpcGatewayDial(splitHttp), splitHttp, gopts...))
}(sctx)
}
return nil
}

func (e *Etcd) grpcGatewayDial(splitHttp bool) (grpcDial func(ctx context.Context) (*grpc.ClientConn, error)) {
Expand Down
Loading

0 comments on commit 313be92

Please sign in to comment.