Skip to content

Instantly share code, notes, and snippets.

@irvinlim
Created April 30, 2020 16:44
Show Gist options
  • Save irvinlim/1410b36a6bc40929d3261070729ed35f to your computer and use it in GitHub Desktop.
Save irvinlim/1410b36a6bc40929d3261070729ed35f to your computer and use it in GitHub Desktop.
import grpc
from consistent_storage.pb.consistent_storage_pb2 import *
from consistent_storage.pb.consistent_storage_pb2_grpc import ConsistentStorageServicer
from lib.consistent_storage import BaseStorageBackend
class GrpcProxyServer(ConsistentStorageServicer):
"""
Consistent storage proxy server that exposes a gRPC interface.
This might be inane to translate a protobuf message to a Python dict and then back to a protobuf,
especially if the backend is also a gRPC server. However,
"""
def __init__(self, backend: BaseStorageBackend):
self.backend = backend
def Get(self, request, context):
res = self.backend.get(request.key)
value = res.get('value')
if not value:
value = ''
value = value.encode('utf-8')
return GetResponse(
exists=res.get('exists', False),
value=value,
isOwner=res.get('is_owner', False),
owner=res.get('owner', ''),
)
def Put(self, request, context):
res = self.backend.put(request.key, request.value)
return PutResponse(
ok=res.get('ok', False),
owner=res.get('owner', ''),
)
def Remove(self, request, context):
res = self.backend.remove(request.key)
error = res.get('error', '')
if error == 'Key does not exist':
error = REMOVE_KEY_ERROR
elif error == 'Not owner of key':
error = REMOVE_NOT_OWNER
else:
error = REMOVE_KEY_ERROR # Default error, not accurate.
return RemoveResponse(
removed=res.get('removed', False),
error=error,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment