shrink sink internal buffer if need (#407) (#422)

Signed-off-by: qupeng <[email protected]>
diff --git a/.travis.yml b/.travis.yml
index 0fe9738..1a2f940 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -13,7 +13,7 @@
 rust:
   - stable
   - beta
-  - nightly
+  - nightly-2019-09-05-x86_64-unknown-linux-gnu
 matrix:
   include:
   - os: osx
@@ -29,7 +29,8 @@
   - export LIBRARY_PATH="$HOME/.cache/lib"
   - export PKG_CONFIG_PATH="$HOME/.cache/lib/pkgconfig"
 script:
-  - if [[ $TRAVIS_RUST_VERSION == "nightly" ]]; then rustup component add clippy-preview --toolchain nightly && cargo clippy --all; fi
+  - if [[ $TRAVIS_RUST_VERSION == "nightly-2019-09-05-x86_64-unknown-linux-gnu" ]]; then rustup install $TRAVIS_RUST_VERSION && rustup default $TRAVIS_RUST_VERSION; fi
+  - if [[ $TRAVIS_RUST_VERSION == "nightly-2019-09-05-x86_64-unknown-linux-gnu" ]]; then rustup component add clippy-preview --toolchain $TRAVIS_RUST_VERSION && cargo clippy --all; fi
   - cargo build --no-default-features
   - cargo build --no-default-features --features protobuf-codec
   - cargo build
diff --git a/src/call/mod.rs b/src/call/mod.rs
index 96f2350..fcec160 100644
--- a/src/call/mod.rs
+++ b/src/call/mod.rs
@@ -28,6 +28,9 @@
 
 pub use grpc_sys::GrpcStatusCode as RpcStatusCode;
 
+// By default buffers in `SinkBase` will be shrink to 4K size.
+const BUF_SHRINK_SIZE: usize = 4 * 1024;
+
 /// Method types supported by gRPC.
 #[derive(Clone, Copy)]
 pub enum MethodType {
@@ -585,6 +588,11 @@
             c.call
                 .start_send_message(&self.buf, flags.flags, self.send_metadata)
         })?;
+        // NOTE: Content of `self.buf` is copied into grpc internal.
+        if self.buf.capacity() > BUF_SHRINK_SIZE {
+            self.buf.truncate(BUF_SHRINK_SIZE);
+            self.buf.shrink_to_fit();
+        }
         self.batch_f = Some(write_f);
         self.send_metadata = false;
         Ok(true)