Building gRPC Interceptors in Golang: Client and Server (Unary and Streaming)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Mario welcome to another system designing a video in today's episode I'm sharing with you how to implement erpc interceptors specifically for both the client and the server implementations so what are interceptors interceptors intercept the execution of each RPC call users can do interceptors to the login authentication authorization metrics collection and many other functionality that can be shared across rpcs in our case like I told you before we're going to be maintain interceptors or clients and servers let's start with the server implementation as usual the link to the code is in the description of this video so feel free to check that out we're going to be implementing the server Interceptor first both the unary implementation as well as the streaming implementation what I did just to speed up things I added a new file called Interceptor under the server folder that defines the types that we need to implement and these are the types that are coming from the grpc package there are literally functions that we just have to implement and do some work we do the things that we are supposed to be doing which in this case are basically three steps there is a pre-processing there is an invocation step and there is a post processing so in order used to import the packages I'm going to save this and we're going to be doing the most basic Interceptor out there which we log in so we're going to be doing a printerland Unity server Interceptor that is going to be printed out the name of the method and we are going to be doing something similar with the post so we have now pre and post but now we need a invocation right so we need to call the Handler we need to pass in a context and we need to pass in the request this will is going to return the value that is coming from the Handler and we're going to be returning those back in our method so this is the most simple implementation what we need to do next is go back and update the main we need to go down to the grpc server we need to add a new option that allows us to add or inject the Interceptor in this case will be grpc unit Interceptor and the method that we use implemented unity and intercept so that's basically it if we compile you will notice down here that everything seems to be working so if we go into another window we want to jump into example server we run our server everything seems to be running we run our client and as you will notice that you will see that here we have the things that we implemented previously is just printing now pre and post and the method name now there is an interesting thing that we're going to be using afterwards that is called metadata and in order to do that we just have to modify a few things here one will be using this metadata package and then from incoming context we're going to add a bus in the context this is going to be written in the metadata and okay and we are going to be doing if if there is no metadata we just just going to print out lock print again no meta data if there is a metadata we're going to be printing out the values are in the metadata this is important because although in go we use context to pass in the values between different requests for example when we're wrapping around a server method in the context of erpc this doesn't exist so we have to use the grpc way of passing in values from the context between the different goals so let's go back and run the server you will see what I mean so we are running again the server we run again Decline and you will notice that now is printing out some metadata values this will come into place when we're implementing the client unary Interceptor you will see in a moment now let's look at the implementation of the server Interceptor for implementing the survey Interceptor we're going to be doing something similar we just have to open up the Interceptor file that I added before what I did is I added the type that we need to implement again is function so we just have to get rid of this as well as this value use return nil in the meantime and this will make our editor happy and then we need to add the three steps that we need to add which will be log print Ln which indicates the pre we're just going to do something similar just print out the name of the method we do a post that is just going to be printing out that value we're going to be completed the handle with the server streaming which returns an error and then we're going to be returning the error and similarly to the unary implementation we just have to open up main go down to the new server call and this will be a stream Interceptor which receives an extreme server intercept we compile this just to make sure again down here everything seems to be fine we run our server and now what is going to be happening we're going to printing out some values when we're using any streaming RPC so if you run the client streaming client you will see that now the server is printing out these values right here before the pre and this value right here for the post but we also want to intercept each one of the messages in that case we need to do something extra and that extra will be implementing a type that is actually going to be wrapping around the receive and descent methods let me show you this new type can be defined anywhere it's just basically something that we're going to be adding in our server so we can call it wrap stream and it's going to be embedding the grpc serverless stream type and this type is going to prevent in two functions one for receiving and one percent for now we're just going to be calling what we receive initially so we'll be server stream receive message and the value and that we receive and something similar for the send which will be send message and send message now we want to print out the values that we will receive it so we do a log print f receive and we can do something similar with the send now next what we have to do is Define a new function that is going to be used for wrapping this receive a stream into the stream that we just implemented for doing that we just have to be receiving a server stream that Returns the server stream that we are just implemented above so basically return server wrap server stream with the receive stream so we are literally receiving the server stream and we are wrapping that around and that will allow us to actually intercept the send and receive for the server code and we're going to be modifying this line in order to do that so we call new wrap stream SS and with this now we can allow us to receive those method and also print out those values let me show you so we stop and run the server again one more time and we if we run the client you will notice that now for each one of the calls we're actually getting the receive and when we finish we do the send right here so we are still receiving the initial code in the streaming initialization the one that opens up the stream but then for each message we can wrap around those values and actually indicate the send and the receive as well now let's talk about the client interceptors for decline Interceptor the steps are a little bit similar we're going to be opening a decline and there is an Interceptor file that I previously it includes the type which again similar to the server is just a function so in order to make IMI editor happy we use return nail for now and we're going to be doing something similar again it has three steps pre-invocation and post so print Ln unary client Interceptor free we print out the method name we do a post and in this case we print out the result that we receive we will be the reply and finally there is going to be an invocation so we have the invocation with the context the method request the reply decline connection and all the options this returns an error finally we just return the error if we run the client again I didn't stop my server that didn't change I'm going to be running my client oh before that again we need to add that to the dial option so grpc with generally Interceptor and the unary client intercept so we run our client and now you will see that the there is a pre and there is a post right here and as before the things are still printing now the pre is printing out the metadata and it's doing the post now this is important because now we're going to be adding some values to the metadata and in order to do that we're going to be modifying our method to modify the context and in order to do that we're going to call this metadata package one more time and we're going to be appending something and again if you record what I said before this is similar to the way context Works in go but because this is your PC and there are different programming languages you need to have your own erpc way to add values like this we're going to be passing in hello let's ask the key and world as the value now we leave the code AS is we run again our client and now you will notice that the values that the server is receiving are including the Halo world that we Define above so let's jump into the client streaming interceptor the client is streaming Interceptor is a bit similar to what we did with the server streaming Interceptor I'm going to be using the bi-directional client because I want to show you the send and receive and again I added a new file that defines a type that we need to implement in this case will be a function but as you maybe imagine this is similar to the server one so we need to implement if we want to also catch the send and divs and receive methods as well we need to define a new type and then wrap the server connection in this case the client connection to do something with those values so let me show you first the easiest one which will be the streaming client Interceptor this will be doing the three steps as usual we're going to be printing out some values that indicate the pre-step as well as a post and the invocation of the streamer which we'll be receiving a context a description the CC client connection method and the options this returns an error and the stream as well and then we can return those back so this is the simplest implementation that we have we need to also update main to include that Interceptor and similar to the client we're going to be having a year PC with a stream interceptor and a stream client interceptor and if we go and run the client you will see that now it's going to be printing now a pre and a post now what if we want to actually do something about the receive and send messages like we did in the server we need to do something similar we need to implement a new type we need to implement a few methods and then wrap around the streamer value and then send back to the grpc client let me show you so we're going to be defining a new type let's call it wrap stream it's going to be extract it's going to be embedding the erpc client esteem type we're going to be defining two functions one called receive message is going to be receiving an empty interface enter in an error and be calling the values in the extract client stream receive message and similarly we're going to be doing something for descent now we want to print out the values so we will do a printf receive similar to what we did with the server and we're going to be printing out descent similar to what we did with server as well and finally we're going to implement the new initializer new wrap stream sgrpc client stream which returns a grpc client stream that is just basically wrapping the receive value with the value that we have now we need to change and use that in here so we're going to be modifying this return to start using this new wrap stream and pass in the s that we received previously this similar to what we did in the server so there's that doesn't really change so we run the client one more time you will notice that now we have more values being printed out we have a cent and you scroll down a little bit you will notice that now we have a receive as well so for each send you we're going to be printing out the values as as usual and for each receive we're going to be printing out those values as well which is similar to what we did with the server Interceptor and that's it this is how you implement interceptors in grpc for both unary rpcs and streaming rpcs for both client and server hopefully this is useful for you again the code is in description feel free to check that out I will talk to you next time take care stay safe see you
Info
Channel: Mario Carrion
Views: 3,192
Rating: undefined out of 5
Keywords: golang, go lang, golang tutorial, go lang tutorial, golang beginners, golang for beginners, learn golang, golang grpc unit testing, golang unit testing, golang grpc tutorial, golang grpc server streaming, golang grpc client streaming, golang grpc bidirectional streaming, golang grpc, golang interceptor, golang server interceptor, golang client interceptor, learn grpc, grpc golang
Id: 3r1FWEYWZe8
Channel Id: undefined
Length: 14min 26sec (866 seconds)
Published: Sat Mar 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.