Skip to content

Commit

Permalink
session keepalive fix and error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtakingiteasy committed Dec 3, 2021
1 parent f1dd286 commit 92992af
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions integration/nicovideo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,12 +576,25 @@ func (client *Client) createSession(
return
}

func (client *Client) keepaliveError(reporter mediaservice.Reporter, err *error) {
if err == nil || *err == nil || reporter == nil {
return
}

reporter.Submit("keepalive error: "+(*err).Error(), true)
}

func (client *Client) keepalive(
ctx context.Context,
reporter mediaservice.Reporter,
session *APIDataMovieSession,
sess *SessionResponse,
sessdata *SessionResponseData,
) {
var err error

defer client.keepaliveError(reporter, &err)

t := time.NewTicker(time.Duration(session.HeartBeatLifetime/3000) * time.Second)

defer t.Stop()
Expand All @@ -602,6 +615,7 @@ func (client *Client) keepalive(
q := sessurl.Query()

q.Set("_method", "PUT")
q.Set("_format", "json")

sessurl.RawQuery = q.Encode()

Expand All @@ -614,7 +628,7 @@ func (client *Client) keepalive(
return
}

err = json.Unmarshal(bs, &sess)
err = json.Unmarshal(bs, sess)
if err != nil {
return
}
Expand All @@ -623,7 +637,7 @@ func (client *Client) keepalive(
return
}

err = json.Unmarshal(sess.Data, &sessdata)
err = json.Unmarshal(sess.Data, sessdata)
if err != nil {
return
}
Expand Down Expand Up @@ -740,8 +754,6 @@ func (client *Client) SaveFormat(

defer cancel()

contentURL := sessdata.Session.ContentURI

fmtname := strings.TrimPrefix(vformatid, "archive_") + "-" + strings.TrimPrefix(aformatid, "archive_")

outpath = strings.ReplaceAll(outpath, "${fmt}", fmtname)
Expand Down Expand Up @@ -781,7 +793,7 @@ func (client *Client) SaveFormat(
return "", err
}

resp, err := client.methodPage(ctx, contentURL, http.MethodGet, nil, http.Header{
resp, err := client.methodPage(ctx, sessdata.Session.ContentURI, http.MethodGet, nil, http.Header{
"range": []string{fmt.Sprintf("bytes=%d-", finfo.Size())},
})
if err != nil {
Expand All @@ -797,7 +809,7 @@ func (client *Client) SaveFormat(
return "", err
}

go client.keepalive(cctx, &session, &sess, &sessdata)
go client.keepalive(cctx, reporter, &session, &sess, &sessdata)

if cl == finfo.Size() {
return outpath, nil
Expand Down

0 comments on commit 92992af

Please sign in to comment.