Skip to content

Allow multiple upstreams #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion revproxy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import urllib3

from itertools import cycle

from django.utils.six.moves.urllib.parse import urlparse, urlencode, quote_plus

from django.shortcuts import redirect
Expand All @@ -34,6 +36,8 @@

HTTP_POOLS = PoolManager()

UPSTREAM_CYCLES = {}


class ProxyView(View):
"""View responsable by excute proxy requests, process and return
Expand Down Expand Up @@ -70,7 +74,15 @@ def upstream(self, value):
self._upstream = value

def get_upstream(self, path):
upstream = self.upstream
if isinstance(self.upstream, (list, tuple)):
key = tuple(self.upstream)
upstream_rr = UPSTREAM_CYCLES.get(key)
if not upstream_rr:
upstream_rr = cycle(self.upstream)
UPSTREAM_CYCLES[key] = upstream_rr
upstream = next(upstream_rr)
else:
upstream = self.upstream

if not getattr(self, '_parsed_url', None):
self._parsed_url = urlparse(upstream)
Expand Down