[FFmpeg-devel] [PATCH 2/2] avformat/http: exponentially delay reconnection to avoid flooding the server

Michael Niedermayer michaelni at gmx.at
Sun Sep 6 23:18:33 CEST 2015


On Sun, Sep 06, 2015 at 02:05:05PM -0700, Ganesh Ajjanagadde wrote:
> On Sun, Sep 6, 2015 at 1:52 PM, Michael Niedermayer <michaelni at gmx.at> wrote:
> > From: Michael Niedermayer <michael at niedermayer.cc>
> >
> > Signed-off-by: Michael Niedermayer <michael at niedermayer.cc>
> > ---
> >  libavformat/http.c |    7 ++++++-
> >  1 file changed, 6 insertions(+), 1 deletion(-)
> >
> > diff --git a/libavformat/http.c b/libavformat/http.c
> > index aa60aef..126c7b3 100644
> > --- a/libavformat/http.c
> > +++ b/libavformat/http.c
> > @@ -28,6 +28,7 @@
> >  #include "libavutil/avassert.h"
> >  #include "libavutil/avstring.h"
> >  #include "libavutil/opt.h"
> > +#include "libavutil/time.h"
> >
> >  #include "avformat.h"
> >  #include "http.h"
> > @@ -107,6 +108,7 @@ typedef struct HTTPContext {
> >      int reconnect;
> >      int reconnect_at_eof;
> >      int reconnect_streamed;
> > +    int reconnect_delay;
> >      int listen;
> >      char *resource;
> >      int reply_code;
> > @@ -1249,6 +1251,8 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
> >          || (read_ret == 0 && s->reconnect_at_eof && (!h->is_streamed || s->reconnect_streamed))) {
> >          int64_t target = h->is_streamed ? 0 : s->off;
> >          av_log(h, AV_LOG_INFO, "Will reconnect at %"PRId64" error=%s.\n", s->off, av_err2str(read_ret));
> > +        av_usleep(1000U*1000*s->reconnect_delay);
> > +        s->reconnect_delay = 1 + 2*s->reconnect_delay;
> >          seek_ret = http_seek_internal(h, target, SEEK_SET, 1);
> >          if (seek_ret != target) {
> >              av_log(h, AV_LOG_ERROR, "Failed to reconnect at %"PRId64".\n", target);
> > @@ -1256,7 +1260,8 @@ static int http_read_stream(URLContext *h, uint8_t *buf, int size)
> >          }
> >
> >          read_ret = http_buf_read(h, buf, size);
> > -    }
> > +    } else
> > +        s->reconnect_delay = 0;
> >
> >      return read_ret;
> >  }
> > --
> > 1.7.9.5
> 
> Doesn't this have overflow issues?
> More precisely, are you sure this behaves correctly when the int
> reconnect_delay wraps around?

i intend to add a reconnect_max that limits reconnects and prevents
overflow, just wanted to first make sure noone has objections to
these 2 patches before i write more code on top

[...]
--
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

When you are offended at any man's fault, turn to yourself and study your
own failings. Then you will forget your anger. -- Epictetus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20150906/e6ec2df1/attachment.sig>


More information about the ffmpeg-devel mailing list