Skip to content
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

Storage: Close response body after request #4116

Merged
merged 4 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -715,14 +715,19 @@ public void write(
int code;
String message;
IOException exception = null;
HttpResponse response = null;
try {
HttpResponse response = httpRequest.execute();
response = httpRequest.execute();
code = response.getStatusCode();
message = response.getStatusMessage();
} catch (HttpResponseException ex) {
exception = ex;
code = ex.getStatusCode();
message = ex.getStatusMessage();
} finally {
if (response != null ) {
response.disconnect();
}
chingor13 marked this conversation as resolved.
Show resolved Hide resolved
}
if (!last && code != 308 || last && !(code == 200 || code == 201)) {
if (exception != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.util.DateTime;
import com.google.api.gax.paging.Page;
import com.google.auth.ServiceAccountSigner;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.Identity;
import com.google.cloud.Policy;
Expand All @@ -46,6 +49,8 @@
import com.google.cloud.kms.v1.KeyManagementServiceGrpc;
import com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub;
import com.google.cloud.kms.v1.KeyRingName;
import com.google.cloud.TransportOptions;
import com.google.cloud.http.HttpTransportOptions;
import com.google.cloud.kms.v1.LocationName;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Acl.Role;
Expand Down Expand Up @@ -111,6 +116,8 @@
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import javax.crypto.spec.SecretKeySpec;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -184,6 +191,15 @@ public static void afterClass() throws ExecutionException, InterruptedException
}
}

private static class CustomHttpTransportFactory implements HttpTransportFactory {
@Override
public HttpTransport create() {
PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
manager.setMaxTotal(1);
return new ApacheHttpTransport(HttpClients.createMinimal(manager));
}
}

private static void prepareKmsKeys() throws IOException {
String projectId = remoteStorageHelper.getOptions().getProjectId();
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
Expand Down Expand Up @@ -1744,6 +1760,27 @@ public void testWriteChannelExistingBlob() throws IOException {
assertTrue(storage.delete(BUCKET, blobName));
}

@Test(timeout = 5000)
public void testWriteChannelWithConnectionPool() throws IOException {
TransportOptions transportOptions = HttpTransportOptions.newBuilder()
.setHttpTransportFactory(new CustomHttpTransportFactory()).build();
Storage storageWithPool =
StorageOptions.newBuilder().setTransportOptions(transportOptions).build().getService();
String blobName = "test-read-and-write-channels-blob";

This comment was marked as spam.

BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build();
byte[] stringBytes;
try (WriteChannel writer = storageWithPool.writer(blob)) {
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
writer.write(ByteBuffer.wrap(stringBytes));
}
try (WriteChannel writer = storageWithPool.writer(blob)) {
stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
writer.write(ByteBuffer.wrap(stringBytes));
}
}

@Test
public void testGetSignedUrl() throws IOException {
if (storage.getOptions().getCredentials() != null) {
Expand Down