Skip to content

Instantly share code, notes, and snippets.

@danscan
Created November 18, 2013 23:56
Show Gist options
  • Save danscan/7537577 to your computer and use it in GitHub Desktop.
Save danscan/7537577 to your computer and use it in GitHub Desktop.
My dream-code (just an idea) for how video append request failure can be handled.

Post Sequence

A dream-class that handles sequential-request streaming.

Features

  • POSTs multipart request bodies
  • Caches all requests
  • Failed request are retried immediately within a specified time window
  • If >10% of requests fail during the stream, the entire post sequence is cached for later retry under conditions of greater reliability.

Example

var reachability = require('reachability');
/**
 * init post sequence
 */
var postSequence = new PostSequence({
  endpoint: '/videos/12345/append'
});

/**
 * begin stream
 */
postSequence.post({
  mediaSegment = new Buffer('faux-content'),
  fileSequence = 0,
  isFinalMediaSegment = false
});
postSequence.post({
  mediaSegment = new Buffer('faux-content'),
  fileSequence = 1,
  isFinalMediaSegment = false
});
postSequence.post({
  mediaSegment = new Buffer('faux-content'),
  fileSequence = 2,
  isFinalMediaSegment = false
});
postSequence.post({
  mediaSegment = new Buffer('faux-content'),
  fileSequence = 3,
  isFinalMediaSegment = false
});
postSequence.post({
  mediaSegment = new Buffer('faux-content'),
  fileSequence = 4,
  isFinalMediaSegment = true
});
/**
 * end stream
 */
postSequence.end(function(error, failedRequests, allRequests) {
  if (error || failedRequests.length / allRequests.length > 0.10) {
  
    /**
     * if streaming encountered significant failure, retry 
     * all requests sequentially when wifi is available.
     */
    reachability.on('wifi', function() {
      postSequence.retry(allRequests);
    });
  } else {
    console.log('Success!');
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment