Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion common/util/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ func IsExpired(url string, headers map[string]string, lastModified int64, eTag s
headers = make(map[string]string)
}
if lastModified > 0 {
headers["If-Modified-Since"] = strconv.FormatInt(lastModified, 10)
lastModifiedStr, _ := ConvertTimeIntToString(lastModified)
headers["If-Modified-Since"] = lastModifiedStr
}
if !IsEmptyStr(eTag) {
headers["If-None-Match"] = eTag
Expand Down
22 changes: 22 additions & 0 deletions common/util/net_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ package util

import (
"bufio"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"time"

log "github.com/sirupsen/logrus"
)

const (
separator = "&"
layoutGMT = "GMT"
)

var defaultRateLimit = "20M"
Expand Down Expand Up @@ -236,6 +240,24 @@ func GetAllIPs() (ipList []string, err error) {
return
}

// ConvertTimeStringToInt converts a string time to a int64 timestamp.
func ConvertTimeStringToInt(timeStr string) (int64, error) {
formatTime, err := time.ParseInLocation(http.TimeFormat, timeStr, time.UTC)
if err != nil {
return 0, err
}

return formatTime.Unix() * int64(1000), nil
}

// ConvertTimeIntToString converts a int64 timestamp to a string time.
func ConvertTimeIntToString(timestamp int64) (string, error) {
localTime := time.Unix(timestamp/int64(1000), 0)
timeString := localTime.UTC().Format(http.TimeFormat)

return fmt.Sprintf("%s%s", timeString[:len(timeString)-3], layoutGMT), nil
}

// slice2Map translate a slice to a map with
// the value in slice as the key and true as the value.
func slice2Map(value []string) map[string]bool {
Expand Down
14 changes: 14 additions & 0 deletions common/util/net_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,17 @@ func (suite *UtilSuite) TestConvertHeaders(c *check.C) {
c.Assert(headers, check.DeepEquals, v.e)
}
}

func (suite *UtilSuite) TestConvertTimeStringToInt(c *check.C) {
timeStr := "Fri, 15 Jun 2018 14:40:41 GMT"
result, err := ConvertTimeStringToInt(timeStr)
c.Check(err, check.IsNil)
c.Check(result, check.Equals, int64(1529073641000))
}

func (suite *UtilSuite) TestConvertTimeIntToString(c *check.C) {
timestamp := int64(1529073641000)
result, err := ConvertTimeIntToString(timestamp)
c.Check(err, check.IsNil)
c.Check(result, check.Equals, "Fri, 15 Jun 2018 14:40:41 GMT")
}
5 changes: 1 addition & 4 deletions supernode/daemon/mgr/cdn/cache_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,5 @@ func checkSameFile(task *types.TaskInfo, metaData *fileMetaData) bool {
return metaData.Md5 == task.Md5
}

if cutil.IsEmptyStr(metaData.Md5) {
return metaData.Identifier == task.Identifier
}
return false
return metaData.Identifier == task.Identifier
}
3 changes: 1 addition & 2 deletions supernode/daemon/mgr/cdn/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/md5"
"path"
"strconv"

"github.com/dragonflyoss/Dragonfly/apis/types"
cutil "github.com/dragonflyoss/Dragonfly/common/util"
Expand Down Expand Up @@ -166,7 +165,7 @@ func (cm *Manager) handleCDNResult(ctx context.Context, task *types.TaskInfo, re
}

func (cm *Manager) updateLastModifiedAndETag(ctx context.Context, taskID, lastModified, eTag string) {
lastModifiedInt, _ := strconv.ParseInt(lastModified, 10, 64)
lastModifiedInt, _ := cutil.ConvertTimeStringToInt(lastModified)
if err := cm.metaDataManager.updateLastModifiedAndETag(ctx, taskID, lastModifiedInt, eTag); err != nil {
logrus.Errorf("failed to update LastModified(%s) and ETag(%s) for taskID %s: %v", lastModified, eTag, taskID, err)
}
Expand Down