Skip to content

Commit

Permalink
fix: golangci-lint error
Browse files Browse the repository at this point in the history
Signed-off-by: PoAn Yang <[email protected]>
  • Loading branch information
FrankYang0529 committed Apr 26, 2024
1 parent d631464 commit 6972c2e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 73 deletions.
3 changes: 3 additions & 0 deletions pkg/client/sync_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,9 @@ func (client *SyncClient) DownloadToDst(srcFilePath, dstFilePath string) error {
}
}
dst, err := os.Create(dstFilePath)
if err != nil {
return errors.Wrapf(err, "failed to create the dst file before download")
}
defer dst.Close()

httpClient := &http.Client{Timeout: 0}
Expand Down
49 changes: 24 additions & 25 deletions pkg/datasource/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,36 +158,33 @@ func (s *Service) waitForBeginning() {
ticker := time.NewTicker(RetryInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if count >= RetryCount {
s.lock.Lock()
s.timeoutBegin = true
s.dsInfo.State = string(types.StateFailed)
s.dsInfo.Message = TimeoutBeginErrorMessage
s.lock.Unlock()
if err := s.syncClient.Delete(s.filePath); err != nil {
s.log.Errorf("DataSource Service: failed to do cleanup after timeout waiting for the datasource processing begin: %v", err)
}
return
<-ticker.C
if count >= RetryCount {
s.lock.Lock()
s.timeoutBegin = true
s.dsInfo.State = string(types.StateFailed)
s.dsInfo.Message = TimeoutBeginErrorMessage
s.lock.Unlock()
if err := s.syncClient.Delete(s.filePath); err != nil {
s.log.Errorf("DataSource Service: failed to do cleanup after timeout waiting for the datasource processing begin: %v", err)
}
return
}

count++
count++

dsInfo, err := s.syncDataSourceFileInfo()
if err != nil {
s.log.Debugf("DataSource Service: failed to get the datasource file info, the processing may be not begin yet: %v", err)
continue
}
dsInfo, err := s.syncDataSourceFileInfo()
if err != nil {
s.log.Debugf("DataSource Service: failed to get the datasource file info, the processing may be not begin yet: %v", err)
continue
}

notBeginYet := dsInfo.State == "" || dsInfo.State == string(types.StatePending) || dsInfo.State == string(types.StateStarting)
if !notBeginYet {
return
}
s.log.Debugf("DataSource Service: datasource file is state %v, the processing is not begin yet", dsInfo.State)
notBeginYet := dsInfo.State == "" || dsInfo.State == string(types.StatePending) || dsInfo.State == string(types.StateStarting)
if !notBeginYet {
return
}
s.log.Debugf("DataSource Service: datasource file is state %v, the processing is not begin yet", dsInfo.State)
}

}

func (s *Service) restoreFromBackupURL() (err error) {
Expand Down Expand Up @@ -315,7 +312,9 @@ func (s *Service) Get(writer http.ResponseWriter, request *http.Request) {
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(outgoingJSON)
if _, err := writer.Write(outgoingJSON); err != nil {
logrus.WithError(err).Error("Failed to write response")
}
}

func (s *Service) Transfer(writer http.ResponseWriter, request *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions pkg/manager/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func (s *TestSuite) TestSingleBackingImageFetch(c *C) {
c.Assert(dsInfo.FilePath, Equals, dsFilePath)

bi, err := getAndWaitFileState(cli1, biName, biUUID, string(types.StateReady), 30)
c.Assert(err, IsNil)
c.Assert(bi.Status.CurrentChecksum, Equals, checksum)

_, err = os.Stat(dsFilePath)
Expand Down
6 changes: 4 additions & 2 deletions pkg/manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ func (m *Manager) monitoring() {
done = true
break
case <-ticker.C:
m.listAndUpdate()
if _, err := m.listAndUpdate(); err != nil {
m.log.WithError(err).Error("failed to list and update backing image files")
}
}
if done {
break
Expand Down Expand Up @@ -372,7 +374,7 @@ func (m *Manager) Sync(ctx context.Context, req *rpc.SyncRequest) (resp *rpc.Bac
// sender.Send is a non-blocking call
sender := client.NewBackingImageManagerClient(req.FromAddress)
if err = sender.Send(req.Spec.Name, req.Spec.Uuid, toAddress); err != nil {
err = errors.Wrapf(err, "sender failed to request backing image sending to %v", toAddress)
err = errors.Wrapf(err, "sender failed to request backing image sending to %v", toAddress) // nolint:ineffassign,staticcheck
return
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sync/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func (s *SyncTestSuite) TestReadyFileValidation(c *C) {
fInfo, err = getAndWaitFileState(cli, curPath, string(types.StateUnknown), 1)
c.Assert(err, IsNil)
c.Check(fInfo.ModificationTime == oldModificationTime, Equals, false)
fInfo, err = getAndWaitFileState(cli, curPath, string(types.StateFailed), 10)
_, err = getAndWaitFileState(cli, curPath, string(types.StateFailed), 10)
c.Assert(err, IsNil)

err = cli.Delete(curPath)
Expand Down
10 changes: 6 additions & 4 deletions pkg/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ func (s *Service) List(writer http.ResponseWriter, request *http.Request) {
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(outgoingJSON)
if _, err := writer.Write(outgoingJSON); err != nil {
logrus.WithError(err).Error("Failed to write response")
}
}

func (s *Service) Get(writer http.ResponseWriter, request *http.Request) {
Expand All @@ -157,7 +159,9 @@ func (s *Service) Get(writer http.ResponseWriter, request *http.Request) {
return
}
writer.Header().Set("Content-Type", "application/json")
writer.Write(outgoingJSON)
if _, err := writer.Write(outgoingJSON); err != nil {
logrus.WithError(err).Error("Failed to write response")
}
}

func (s *Service) Delete(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -203,7 +207,6 @@ func (s *Service) cleanup(filePath string, deleteFile bool) {
if sf != nil && deleteFile {
sf.Delete()
}
return
}

func (s *Service) DownloadToDst(writer http.ResponseWriter, request *http.Request) {
Expand Down Expand Up @@ -249,7 +252,6 @@ func (s *Service) DownloadToDst(writer http.ResponseWriter, request *http.Reques
err = ioErr
return
}
return
}

func (s *Service) checkAndInitSyncFile(filePath, uuid, diskUUID, expectedChecksum string, size int64) (*SyncingFile, error) {
Expand Down
49 changes: 22 additions & 27 deletions pkg/sync/sync_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,22 +267,20 @@ func (sf *SyncingFile) waitForProcessingBeginWithTimeout() {
ticker := time.NewTicker(RetryInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
count++
sf.lock.Lock()
notBeginYet := sf.state == "" || sf.state == types.StatePending || sf.state == types.StateStarting
if !notBeginYet {
sf.lock.Unlock()
return
}
if count >= RetryCount {
sf.handleFailureNoLock(fmt.Errorf("failed to wait for processing begin in %v seconds, current state %v", RetryCount, sf.state))
sf.lock.Unlock()
return
}
<-ticker.C
count++
sf.lock.Lock()
notBeginYet := sf.state == "" || sf.state == types.StatePending || sf.state == types.StateStarting
if !notBeginYet {
sf.lock.Unlock()
return
}
if count >= RetryCount {
sf.handleFailureNoLock(fmt.Errorf("failed to wait for processing begin in %v seconds, current state %v", RetryCount, sf.state))
sf.lock.Unlock()
return
}
sf.lock.Unlock()
}
}

Expand All @@ -291,18 +289,16 @@ func (sf *SyncingFile) WaitForStateNonPending() error {
ticker := time.NewTicker(RetryInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
count++
sf.lock.RLock()
state := sf.state
sf.lock.RUnlock()
if state != types.StatePending {
return nil
}
if count >= LargeRetryCount {
return fmt.Errorf("sync file is still in empty state after %v second", LargeRetryCount)
}
<-ticker.C
count++
sf.lock.RLock()
state := sf.state
sf.lock.RUnlock()
if state != types.StatePending {
return nil
}
if count >= LargeRetryCount {
return fmt.Errorf("sync file is still in empty state after %v second", LargeRetryCount)
}
}
}
Expand Down Expand Up @@ -387,7 +383,6 @@ func (sf *SyncingFile) Delete() {
if err := os.RemoveAll(configFilePath); err != nil {
sf.log.Warnf("SyncingFile: failed to delete sync file config file %v: %v", configFilePath, err)
}
return
}

func (sf *SyncingFile) GetFileReader() (io.ReadCloser, error) {
Expand Down
16 changes: 2 additions & 14 deletions scripts/validate
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ PACKAGES="$(find -name '*.go' | xargs -I{} dirname {} | cut -f2 -d/ | sort -u |
echo Running: go vet
go vet ${PACKAGES}

if [ ! -z "${DRONE_REPO}" ] && [ ! -z "${DRONE_PULL_REQUEST}" ]; then
wget https://github.com/$DRONE_REPO/pull/$DRONE_PULL_REQUEST.patch
echo "Running: golangci-lint run --new-from-patch=${DRONE_PULL_REQUEST}.patch"
golangci-lint run --new-from-patch="${DRONE_PULL_REQUEST}.patch"
rm "${DRONE_PULL_REQUEST}.patch"
elif [ ! -z "${DRONE_COMMIT_REF}" ]; then
echo "Running: golangci-lint run --new-from-rev=${DRONE_COMMIT_REF}"
golangci-lint run --new-from-rev=${DRONE_COMMIT_REF}
else
git symbolic-ref -q HEAD && REV="HEAD" || REV="HEAD^"
headSHA=$(git rev-parse --short=12 ${REV})
echo "Running: golangci-lint run --new-from-rev=${headSHA}"
golangci-lint run --new-from-rev=${headSHA}
fi
echo "Running: golangci-lint"
golangci-lint run --timeout=5m

echo Running: go fmt
test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)"

0 comments on commit 6972c2e

Please sign in to comment.