golangci-lint fix ineffassign (#17027)

Signed-off-by: yminer <yminer@vmware.com>

delete src/lib/redis/helper.go

Signed-off-by: yminer <yminer@vmware.com>
This commit is contained in:
MinerYang 2022-06-21 12:01:59 +08:00 committed by GitHub
parent c1b879a594
commit aab320591f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 34 additions and 124 deletions

View File

@ -22,7 +22,7 @@ linters:
# - gocyclo
# - goimports
# - goprintffuncname
# - ineffassign
- ineffassign
# - nakedret
# - nolintlint
# - revive

View File

@ -74,7 +74,7 @@ func (c *controller) populateRegistry(ctx context.Context, p *pkgmodel.Policy) (
if err != nil {
return nil, err
}
var srcRegistryID, destRegistryID int64 = 0, 0
var srcRegistryID, destRegistryID int64
if policy.SrcRegistry != nil && policy.SrcRegistry.ID != 0 {
srcRegistryID = policy.SrcRegistry.ID
destRegistryID = 0

View File

@ -93,7 +93,7 @@ func parseParams(params map[string]interface{}) (*model.Resource, *model.Resourc
if err := parseParam(params, "dst_resource", dst); err != nil {
return nil, nil, 0, err
}
var speed int32 = 0
var speed int32
value, exist := params["speed"]
if !exist {
speed = 0

View File

@ -121,6 +121,10 @@ func (pm *PolicyMigrator) Migrate() error {
// Transaction
err = conn.Send("MULTI")
if err != nil {
logger.Errorf("send command MULTI failed with error: %s", err)
continue
}
setArgs := []interface{}{
fullID,
"status",
@ -135,6 +139,10 @@ func (pm *PolicyMigrator) Migrate() error {
}
// Set fields
err = conn.Send("HMSET", setArgs...)
if err != nil {
logger.Errorf("send command HMSET failed with error: %s", err)
continue
}
// Remove useless fields
rmArgs := []interface{}{
@ -160,6 +168,10 @@ func (pm *PolicyMigrator) Migrate() error {
if rawJSON, er := policy.Serialize(); er == nil {
// Remove the old one first
err = conn.Send("ZREMRANGEBYSCORE", rds.KeyPeriodicPolicy(pm.namespace), numbericPolicyID, numbericPolicyID)
if err != nil {
logger.Errorf("send command ZREMRANGEBYSCORE failed with error: %s", err)
continue
}
// Save back to the rdb
err = conn.Send("ZADD", rds.KeyPeriodicPolicy(pm.namespace), numbericPolicyID, rawJSON)
} else {

View File

@ -1,72 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
import (
"os"
"strconv"
"sync"
"time"
"github.com/gomodule/redigo/redis"
)
var (
pool *redis.Pool
poolOnce sync.Once
poolMaxIdle = 200
poolMaxActive = 1000
poolIdleTimeout int64 = 180
dialConnectionTimeout = 30 * time.Second
dialReadTimeout = 10 * time.Second
dialWriteTimeout = 10 * time.Second
)
// DefaultPool return default redis pool
func DefaultPool() *redis.Pool {
poolOnce.Do(func() {
maxIdle, err := strconv.Atoi(os.Getenv("REDIS_POOL_MAX_IDLE"))
if err != nil || maxIdle < 0 {
maxIdle = poolMaxIdle
}
maxActive, err := strconv.Atoi(os.Getenv("REDIS_POOL_MAX_ACTIVE"))
if err != nil || maxActive < 0 {
maxActive = poolMaxActive
}
idleTimeout, err := strconv.ParseInt(os.Getenv("REDIS_POOL_IDLE_TIMEOUT"), 10, 64)
if err != nil || idleTimeout < 0 {
idleTimeout = poolIdleTimeout
}
// get _REDIS_URL_REG from environment directly here to avoid cyclic dependency
url := os.Getenv("_REDIS_URL_REG")
if url == "" {
url = "redis://localhost:6379/1"
}
pool, err = GetRedisPool("CommonRedis", url, &PoolParam{
PoolMaxIdle: maxIdle,
PoolMaxActive: maxActive,
PoolIdleTimeout: time.Duration(idleTimeout) * time.Second,
DialConnectionTimeout: dialConnectionTimeout,
DialReadTimeout: dialReadTimeout,
DialWriteTimeout: dialWriteTimeout,
})
})
return pool
}

View File

@ -1,40 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
import (
"fmt"
"github.com/stretchr/testify/require"
"os"
"testing"
)
const testingRedisHost = "REDIS_HOST"
func TestGetRedisPool(t *testing.T) {
pool, err := GetRedisPool("test", fmt.Sprintf("redis://%s:%d", getRedisHost(), 6379), nil)
require.Nil(t, err)
conn := pool.Get()
defer conn.Close()
}
func getRedisHost() string {
redisHost := os.Getenv(testingRedisHost)
if redisHost == "" {
redisHost = "127.0.0.1" // for local test
}
return redisHost
}

View File

@ -167,7 +167,7 @@ func getAdapterInfo() *model.AdapterPattern {
func (a *adapter) listNamespaces(c *cr.Client) (namespaces []string, err error) {
// list namespaces
var nsReq = cr.CreateGetNamespaceListRequest()
var nsResp = cr.CreateGetNamespaceListResponse()
var nsResp *cr.GetNamespaceListResponse
nsReq.SetDomain(a.domain)
nsResp, err = c.GetNamespaceList(nsReq)
if err != nil {

View File

@ -50,7 +50,7 @@ func (a *aliyunAuthCredential) Modify(r *http.Request) (err error) {
}
var tokenRequest = cr.CreateGetAuthorizationTokenRequest()
var tokenResponse = cr.CreateGetAuthorizationTokenResponse()
var tokenResponse *cr.GetAuthorizationTokenResponse
tokenRequest.SetDomain(fmt.Sprintf(endpointTpl, a.region))
tokenResponse, err = client.GetAuthorizationToken(tokenRequest)
if err != nil {

View File

@ -121,7 +121,7 @@ func newAdapter(registry *model.Registry) (a *adapter, err error) {
Values: []*string{common.StringPtr(strings.ReplaceAll(registryURL.Host, ".tencentcloudcr.com", ""))},
},
}
var resp = tcr.NewDescribeInstancesResponse()
var resp *tcr.DescribeInstancesResponse
resp, err = client.DescribeInstances(req)
if err != nil {
log.Errorf("DescribeInstances error=%s", err.Error())

View File

@ -54,7 +54,7 @@ func (a *adapter) createRepository(namespace, repository string) (err error) {
repoReq.RegistryId = a.registryID
repoReq.NamespaceName = &namespace
repoReq.RepositoryName = &repository
var repoResp = tcr.NewDescribeRepositoriesResponse()
var repoResp *tcr.DescribeRepositoriesResponse
repoResp, err = a.tcrClient.DescribeRepositories(repoReq)
if err != nil {
return
@ -69,7 +69,7 @@ func (a *adapter) createRepository(namespace, repository string) (err error) {
req.NamespaceName = &namespace
req.RepositoryName = &repository
req.RegistryId = a.registryID
var resp = tcr.NewCreateRepositoryResponse()
var resp *tcr.CreateRepositoryResponse
resp, err = a.tcrClient.CreateRepository(req)
if err != nil {
log.Debugf("[tencent-tcr.PrepareForPush.createRepository] error=%v", err)
@ -124,7 +124,7 @@ func (a *adapter) isNamespaceExist(namespace string) (exist bool, err error) {
var req = tcr.NewDescribeNamespacesRequest()
req.NamespaceName = &namespace
req.RegistryId = a.registryID
var resp = tcr.NewDescribeNamespacesResponse()
var resp *tcr.DescribeNamespacesResponse
resp, err = a.tcrClient.DescribeNamespaces(req)
if err != nil {
return

View File

@ -101,9 +101,13 @@ func (d *dao) Update(ctx context.Context, policy *model.Policy, props ...string)
return err
}
n, err := ormer.Update(policy, props...)
if e := orm.AsConflictError(err, "replication policy %s already exists", policy.Name); e != nil {
err = e
if err != nil {
if e := orm.AsConflictError(err, "replication policy %s already exists", policy.Name); e != nil {
err = e
}
return err
}
if n == 0 {
return errors.NotFoundError(nil).WithMessage("replication policy %d not found", policy.ID)
}

View File

@ -203,6 +203,9 @@ func (t *taskDAO) GetMaxEndTime(ctx context.Context, executionID int64) (time.Ti
var endTime time.Time
err = ormer.Raw("select max(end_time) from task where execution_id = ?", executionID).
QueryRow(&endTime)
if err != nil {
return time.Time{}, err
}
return endTime, nil
}

View File

@ -75,7 +75,7 @@ func (r *repositoryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
}
// handle the pagination
resRepos := repoNames
var resRepos []string
repoNamesLen := len(repoNames)
// with "last", get items form lastEntryIndex+1 to lastEntryIndex+maxEntries
// without "last", get items from 0 to maxEntries'

View File

@ -108,6 +108,9 @@ func (c *configAPI) GetInternalconfig(ctx context.Context, params configure.GetI
return c.SendError(ctx, err)
}
cfg, err := c.controller.AllConfigs(ctx)
if err != nil {
return c.SendError(ctx, err)
}
resultCfg, err := c.controller.ConvertForGet(ctx, cfg, true)
if err != nil {
return c.SendError(ctx, err)