- 
                Notifications
    You must be signed in to change notification settings 
- Fork 225
close backendConn #33
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
          
     Open
      
      
            edwardhey
  wants to merge
  9
  commits into
  mwitkow:bugfix/streaming-fix
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
edwardhey:master
  
      
      
   
  
    
  
  
  
 
  
      
    base: bugfix/streaming-fix
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      af55d61
              
                Merge pull request #10 from mwitkow/bugfix/streaming-fix
              
              
                 428fa1c
              
                Fix a channel closing bug
              
              
                 a8f5f87
              
                fixup tests, extend readme
              
              
                 3fcbd37
              
                fixup closing conns
              
              
                 97396d9
              
                Merge pull request #11 from mwitkow/fix-close-bug
              
              
                 67591eb
              
                Break StreamDirector interface, fix metadata propagation for gRPC-Go>…
              
              
                 81a67c4
              
                git add proxy
              
              
                edwardhey 3577d6a
              
                关闭句柄
              
              
                edwardhey 084bf1b
              
                update
              
              
                edwardhey File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
            Binary file not shown.
          
    This file was deleted.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # proxy | ||
| -- | ||
| import "github.com/mwitkow/grpc-proxy/proxy" | ||
|  | ||
| Package proxy provides a reverse proxy handler for gRPC. | ||
|  | ||
| The implementation allows a `grpc.Server` to pass a received ServerStream to a | ||
| ClientStream without understanding the semantics of the messages exchanged. It | ||
| basically provides a transparent reverse-proxy. | ||
|  | ||
| This package is intentionally generic, exposing a `StreamDirector` function that | ||
| allows users of this package to implement whatever logic of backend-picking, | ||
| dialing and service verification to perform. | ||
|  | ||
| See examples on documented functions. | ||
|  | ||
| ## Usage | ||
|  | ||
| #### func Codec | ||
|  | ||
| ```go | ||
| func Codec() grpc.Codec | ||
| ``` | ||
| Codec returns a proxying grpc.Codec with the default protobuf codec as parent. | ||
|  | ||
| See CodecWithParent. | ||
|  | ||
| #### func CodecWithParent | ||
|  | ||
| ```go | ||
| func CodecWithParent(fallback grpc.Codec) grpc.Codec | ||
| ``` | ||
| CodecWithParent returns a proxying grpc.Codec with a user provided codec as | ||
| parent. | ||
|  | ||
| This codec is *crucial* to the functioning of the proxy. It allows the proxy | ||
| server to be oblivious to the schema of the forwarded messages. It basically | ||
| treats a gRPC message frame as raw bytes. However, if the server handler, or the | ||
| client caller are not proxy-internal functions it will fall back to trying to | ||
| decode the message using a fallback codec. | ||
|  | ||
| #### func RegisterService | ||
|  | ||
| ```go | ||
| func RegisterService(server *grpc.Server, director StreamDirector, serviceName string, methodNames ...string) | ||
| ``` | ||
| RegisterService sets up a proxy handler for a particular gRPC service and | ||
| method. The behaviour is the same as if you were registering a handler method, | ||
| e.g. from a codegenerated pb.go file. | ||
|  | ||
| This can *only* be used if the `server` also uses grpcproxy.CodecForServer() | ||
| ServerOption. | ||
|  | ||
| #### func TransparentHandler | ||
|  | ||
| ```go | ||
| func TransparentHandler(director StreamDirector) grpc.StreamHandler | ||
| ``` | ||
| TransparentHandler returns a handler that attempts to proxy all requests that | ||
| are not registered in the server. The indented use here is as a transparent | ||
| proxy, where the server doesn't know about the services implemented by the | ||
| backends. It should be used as a `grpc.UnknownServiceHandler`. | ||
|  | ||
| This can *only* be used if the `server` also uses grpcproxy.CodecForServer() | ||
| ServerOption. | ||
|  | ||
| #### type StreamDirector | ||
|  | ||
| ```go | ||
| type StreamDirector func(ctx context.Context, fullMethodName string) (*grpc.ClientConn, error) | ||
| ``` | ||
|  | ||
| StreamDirector returns a gRPC ClientConn to be used to forward the call to. | ||
|  | ||
| The presence of the `Context` allows for rich filtering, e.g. based on Metadata | ||
| (headers). If no handling is meant to be done, a `codes.NotImplemented` gRPC | ||
| error should be returned. | ||
|  | ||
| It is worth noting that the StreamDirector will be fired *after* all server-side | ||
| stream interceptors are invoked. So decisions around authorization, monitoring | ||
| etc. are better to be handled there. | ||
|  | ||
| See the rather rich example. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better, IMO, to allow the stream director to handle connection management, rather than going through a global callback. This was implemented in #16, which is also available as a separate repo: https://github.com/vgough/grpc-proxy/blob/master/proxy/handler.go.