Building a gRPC Service in Golang: Server Streaming RPC (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is mario welcome to another system design video in today's episode i'm sharing with you how to implement a grpc service specifically a year pc server streaming rpc so what is a year pc server streaming rpc it's one of the three streaming rpc types where a client sends a request to the server and gets a streaming to read a sequence of messages back the final implementation of this example will look like this this is a simple cli client that happens to be using the server streaming rpc the use case of this example consists of a server that is streaming data to some clients in this case will be a hypothetical wearable device that is measuring hard bits per minute let's look at the code as usual the link to this code is in description of this video so feel free to check it out the first thing that we need to do is implement the protocol buffer for this wearable service for doing that let's create a new folder called wearable inside this folder we create another folder called v1 and inside this folder we create a wearable service dot proto we save it and because this is at least super similar to the user service we're going to be copying that one over and we're going to make some modifications first one will be the package will be wearable similarly wearable here similarly wearable here you don't need to import the user proto because we are not going to be using it user will be changed to wearable service we're going to implement in a function called bits per minute that receives a bits per minute request and returns a bits per minute response but the important bit here is this called the stream this is the keyword for returning streams in grpc what comes next is implementing the actual messages for the two types i'm going to be using request we'll be using the same field and response is going to be returning two values one of them will be iu in 32 to indicate the value of the heartbeat and the second will be a second to indicate the second or rather not the second the minute to indicate the minute when this value was executed after that if we run both lane you will notice everything is fine we run both generate and now we should have a few new files on the gen now wearable and we have this jrpc that i have right here this one is one of the important files that we're going to be referring to in a few seconds so if we open it i want to show you a few things we're going to be looking for a type called server which is an interface type that allows us to indicate what we need to implement for the server that is going to be implementing the streaming rpc so i what i like doing when working with this type is just copy the values going to the actual server implementation which in this case in this case is under examples server main but instead of adding a new type like i did here i'm going to create a new file that is going to be used for representing this new wearable service so let's open a file let's call it wearable service dot go we paste this for now for reference and we're going to be defining a new type variable server a stroke type that implements the wearable pv er what is the unimplemented wearable server this is like i mentioned previously to keep forward compatibility with future implementations next what we need to define is a function w where we can read of this wearable server called bits per minute that receives a request from wearable bb which is this request and next the stream stream again let me copy and paste all of these and returns an error for now let me return nil because we're implementing a stream what this means and practice is that we're going to be sending the data constantly until we the server decide to stop sending data or the client decides to leave and you know we can close the connection for doing this we need to implement it video using a4 and a few different things that are going to be used via the stream using the select keyword a stream includes a context right here oh case stream context done when that happens we just return actually when that happens with it instead of returning nil we need to return something so we need this error codes canceled and then some message in this case stream has ended in the case well it was canceled and we cannot do anything else about it so what we have to do afterwards we save it so we can pull the other details like these packages followed by how can we send the data now that we have this full infinite loop for the four next we have to actually do the actual sending the actual values so we're going to be using a default statement that for this hypothetical case we're going to be sleeping for one minute one scroll up a little bit time minute then we're going to be generating some random value um in this case used to keep it more or less uh click on to say humanly possible we're going to be adding the 30 and some somewhat random value between 80 so that will give you some bits per minute more or less as is real next we're going to be calling this variable that we have right here you call the stream that is the one that is actually going to be sending data so a stream there is a function called send where you send a message that we need to send the data to the stream and this message is actually the protocol buffer message that we defined before in the response okay so we're staying with a stream send message a wearable pv bits per minute responds it has to be a pointer to that type the value will be the hypothetical value right here value so i need 32 followed by the minute which in this case will be time dot now dot second in in 32 okay so everything seems to be working there is uh oh because it's supposed to be a u int all right so everything seems to be working as expected obviously this one if you well dude maybe you don't know about this but this returns an error right here so we need to consider this error when we're implementing our code so if we go back we just assign an error if error is different than nil well we can return something similar like a status error codes let me open this up i mean no scroll up a little bit cancel the stream has ended i'm going to be covering errors in the new gear pc errors specifically in the near future because those have a little bit they are a little bit more not complicated but they require some thought when implementing them so one last thing that we need to do is go back to the server instantiation of the wearable server type that we defined in the same package in the main package and then what we have to do is just register that with the grp server grpc server that we have before so register we scroll up this again we don't have a space or register wearable service server erpg server and the thing so this will compile if we go ahead and we go to go run examples uh server star well everything is going to be running now we need to implement the client so let's do that for implementing the client we need to call the generated client code in this case i already added a skeleton i call it a stream client and what this stream client has at the moment is a bunch of code that refers to just the definition of the cli and those kind of things not nothing that is relevant to the grpc server streaming implementation so what should we do well we're going to be copying a few things from the other client and we're going to be calling the required methods that we need for this so i'm going to be opening the previous client that i used before and we're going to be copying this over and it's it's not called user pv we are not going to be using npv we're going to be using wearable ppe new wearable service client and the connection okay so everything seems to be working so far there what comes next is actually invoke the connection and for doing that we called client dot bits per minute context background variable the parameter that we're supposed to be passing into the server in this case doesn't matter we call it mario for that and the error so this one the one that i have right here called stream is the one that i was referring to a while ago that's the thing that i want to we're going to be looping until we have the data that we need or we want to exit and those kind of things so okay so we save if there is an error obviously we need to let's it's a log fatal and client error error what comes next is because we want to pull the data for until we have no more data or we decided to leave we're going to be calling the function that is in the stream using a go routine for doing that we are going to be also defining another for loop we're going to be using the stream and there is a function called receive that is right here that returns the response that we implemented in the protocol buffer in the year pc service all right so we go and call it receive this is a blocking method that's why i'm implementing it in a go routine so we have the value error if there is an error we use leaf but also if the error is io eof of endo file we just return we use log fatal n in the case of receive stream receive just so we have some information we don't need to return because it will exit anyways and then what we have to do next and this is more related to the way this cli package work is that there is a data here that probably you notice right here so what we're going to be doing is updating for each minute the field with the value that we're receiving there are 60 minutes in this graph so we're going to be receiving obviously for each minute the value and then reassigning that in the index of the correspond so we have end rest rare value get minute equals of load because that's what is expected in the data again it's right here so if we go back float let me scroll up a little bit float value get value we save this and then we need to call lc data again this is has nothing to do with your pc this is how the this specific package works and finally we call render again with this what is going to happen if you scroll up a little bit is that it's going to be taking creating a new go routine is going to be blocking until there is a new value being received and then it's updating this slice that we have and it's going to be re-rendering it again what we have to do next is also support the context that is coming from the client from in this case the stream client so case stream context done oh i'm missing the operator right here so if in this case right there in this case if it's finished we can print the land you know complete it if you compile this it should be okay i still have running my server if i do a go run examples s3 means client now this is working but now it's going to take one minute to actually show each one of the values let's do one more change so you can see this in action but in practice it should be one minute because we are referring to using it per minute for doing that we are going to be back going back to the implementation of the servers we are going to be changing this to second and then we can rerun it again and you will see the values being changed as we go which is what i show you in the beginning of this video and that's it that's how you implement a year pc server streaming rpc in go thank you for watching i will talk to you next time stay safe and take care
Info
Channel: Mario Carrion
Views: 7,952
Rating: undefined out of 5
Keywords: golang, go lang, golang tutorial, go lang tutorial, golang beginners, golang for beginners, learn golang, golang rpc, golang grpc, golang grpc tutorial, golang grpc unary prc, grpc unary rpc, go lang grpc, golang grpc api, grpc golang hello world, grpc tutorial, grpc protobuf, grpc streaming tutorial, grpc server streaming tutorial, grpc client streaming tutorial, grpc server bidirectional streaming tutorial
Id: l_74x_qQZB8
Channel Id: undefined
Length: 14min 27sec (867 seconds)
Published: Fri Apr 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.