Skip to content

Commit

Permalink
fixed thread safety
Browse files Browse the repository at this point in the history
  • Loading branch information
brosner committed Sep 1, 2016
1 parent ab9ddb3 commit 0a17db7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Change Log

## 0.2.1

* fixed thread safety issue with GCS client
13 changes: 10 additions & 3 deletions gapc_storage/storage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import mimetypes
import os
import threading

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -75,15 +76,21 @@ class GoogleCloudStorage(Storage):
"""

def __init__(self):
self.set_client()
self.thread = threading.local()
config = _gcs_file_storage_settings()
self.bucket = config["bucket"]
self.path_prefix = self.path_prefix if hasattr(self, "path_prefix") else config["path_prefix"]

def set_client(self):
def build_client(self):
credentials = self.get_oauth_credentials()
http = credentials.authorize(httplib2.Http())
self.client = discovery_build("storage", "v1", http=http)
return discovery_build("storage", "v1", http=http)

@property
def client(self):
if not hasattr(self.thread, "client"):
self.thread.client = self.build_client()
return self.thread.client

def get_oauth_credentials(self):
return self.create_scoped(GoogleCredentials.get_application_default())
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="django-gapc-storage",
version="0.2.0",
version="0.2.1",
author="Eldarion, Inc.",
author_email="[email protected]",
description="a Django storage backend using GCS JSON API",
Expand Down

0 comments on commit 0a17db7

Please sign in to comment.