1# Copyright 2019 the gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""An example of compression on the server side with gRPC.""" 15 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20import argparse 21from concurrent import futures 22import logging 23import threading 24 25import grpc 26import helloworld_pb2 27import helloworld_pb2_grpc 28 29_DESCRIPTION = "A server capable of compression." 30_COMPRESSION_OPTIONS = { 31 "none": grpc.Compression.NoCompression, 32 "deflate": grpc.Compression.Deflate, 33 "gzip": grpc.Compression.Gzip, 34} 35_LOGGER = logging.getLogger(__name__) 36 37_SERVER_HOST = "localhost" 38 39 40class Greeter(helloworld_pb2_grpc.GreeterServicer): 41 def __init__(self, no_compress_every_n): 42 super(Greeter, self).__init__() 43 self._no_compress_every_n = no_compress_every_n 44 self._request_counter = 0 45 self._counter_lock = threading.RLock() 46 47 def _should_suppress_compression(self): 48 suppress_compression = False 49 with self._counter_lock: 50 if ( 51 self._no_compress_every_n 52 and self._request_counter % self._no_compress_every_n == 0 53 ): 54 suppress_compression = True 55 self._request_counter += 1 56 return suppress_compression 57 58 def SayHello(self, request, context): 59 if self._should_suppress_compression(): 60 context.set_compression(grpc.Compression.NoCompression) 61 return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name) 62 63 64def run_server(server_compression, no_compress_every_n, port): 65 server = grpc.server( 66 futures.ThreadPoolExecutor(), 67 compression=server_compression, 68 options=(("grpc.so_reuseport", 1),), 69 ) 70 helloworld_pb2_grpc.add_GreeterServicer_to_server( 71 Greeter(no_compress_every_n), server 72 ) 73 address = "{}:{}".format(_SERVER_HOST, port) 74 server.add_insecure_port(address) 75 server.start() 76 print("Server listening at '{}'".format(address)) 77 server.wait_for_termination() 78 79 80def main(): 81 parser = argparse.ArgumentParser(description=_DESCRIPTION) 82 parser.add_argument( 83 "--server_compression", 84 default="none", 85 nargs="?", 86 choices=_COMPRESSION_OPTIONS.keys(), 87 help="The default compression method for the server.", 88 ) 89 parser.add_argument( 90 "--no_compress_every_n", 91 type=int, 92 default=0, 93 nargs="?", 94 help="If set, every nth reply will be uncompressed.", 95 ) 96 parser.add_argument( 97 "--port", 98 type=int, 99 default=50051, 100 nargs="?", 101 help="The port on which the server will listen.", 102 ) 103 args = parser.parse_args() 104 run_server( 105 _COMPRESSION_OPTIONS[args.server_compression], 106 args.no_compress_every_n, 107 args.port, 108 ) 109 110 111if __name__ == "__main__": 112 logging.basicConfig() 113 main() 114