Online Marketing

Native Go Development on GCP with the Context Package – GopherCon SG 2017

This is a talk that was originally developed by a colleague of mine and the first thing he he asks. Everybody at the beginning is the Wi-Fi working, but – and so I’m borrowing this presentation, but uh, of course, is a Wi-Fi working is a Google office. So so obviously one people who you know take advantage of the fact that the Wi-Fi is not working.

But let’s take. Let’s imagine some NGO code that we’re just going to like talk about that’s going to just ping. The Google calm within HTTP yet – and you know, return an error if, if an error happens and also check the status code right, you’ve seen a lot of code. That looks like this, but there’s a couple of issues with this like say what happens if google comm, for some reason decides that they are going to essentially accept a request and then sleep forever before giving you a response, in that case, you’re going to be kind Of left out in the cold, because this is going to just essentially hang forever so so you know we could ping Google like this, but like.

Essentially, we want to be able to do something like canceled it in the middle of it like say, I only want to wait, maybe five or ten seconds for the response to come back and if not, then I want to do something else. So how would we do that in a go program and the way to do that is to use the context package and the context package is a very important package. I believe it was added to the standard library in 1.

5 and before that it lived as a you know: semi standard package in the golang org dot expat, you know directory, but it this became a standard standard library package and actually it’s worth 1.7 in 2016. Ok, so we got that wrong uh, it’s actually a lot later than I thought, but it essentially is about 370 lines without comments. It’s fairly, you know simple package, but it’s a it’s extremely extremely important, and inside of that it only exposes like one interface and several functions so the first.

The most important is the actual context, interface, and this context interface, is what you’ll be using and kind of objects that implement this interface are things that you’ll be passing around and using most of the time. So the context interface looks something like this. You’ll have an object that has all these like kind of methods on it. That done you know, deadlines and value and error, and we’ll kind of see how to use these as we’re going to go along.

The the done method essentially gives you a way of checking to see that a particular context is done, and you know to tell you kind of take, expect back and explain what a context is in a context is essentially a context or a or a wrapper that You can use to say when you get a request or you’re handling a sort of request. You can tell whether that request is done or to kind of attach values to that context that get passed along to lots of different components within the system and we’ll kind of see how that works in practice a little bit later.

I think that it’s easier instead of explaining what it is to actually see what it, how you use it. So it also has this error method too. In case you know timeouts and things like that happen – to get the actual error and these types of deadlines down here or these errors down here. These are actually uh. I think context types or actually error different types of error within within context that you can receive.

So how do we actually cancel the ping note when we did the when we paint go Google com? So if you aren’t going to change the interface itself, you can do the ping Google inside of a go routine and then ask the error that comes back to a blog, an error, blog and then essentially select on that error blog. So you could say whenever that ping is done, then you know return that error or wait if the content check, if the context is done and if the context is done, then return the contexts to error and then we can use the context to do to set Things like timeouts and whatnot, so here we’re doing this kind of we’re kind of wrapping.

Our ping Google function with this ping, Google, with context function, and then we can give it a context that can then do things like handle, timeouts and whatnot, and so how you set the context and how you actually create this context. Value is what’s kind of important for how it handles that. So, let’s talk about how to create context values themselves. So the context package exposes a number of functions that you can use to get context values.

The first one is the one that you can use that you use pretty often is the background, and this essentially returns the background context. So you can imagine that you might have an application that that will accept the request and then make several requests in the backend. So maybe you need to ping a you know: one run application server and another application server and another application server and then kind of aggregate.

The responses back so maybe you’re a web server that has to paying several other micro services within your architecture, and you can do that, like you know, asynchronously, say and then aggregate the responses back. But if any one of those takes too long, then your hold request. Will take too long or if say the client you know times out, then you want to cancel all of the requests that you’ve done.

So you can essentially take that context and pass it throughout your application to kind of and create a different context or create a type of tree structure. That will then allow you to to manage each of the different parts of the the dependent request so say like. If individual, if one individual request times out, then it becomes done, then all of the parent ones. That of that request will then, or that are dependent on it – will then actually become done as well, and so you can cancel the whole entire request out to the client and the reason why we call the background context is because this is essentially the root of That tree so – and this is actually a when you call this background – it returns you a single instance that is essentially a global context.

That’s used inside of your go program, another one that I’ve notice the to do, one, which is essentially the same thing its background, except for the fact that this is essentially labeling. The fact that you’re, you don’t know what you’re doing or you don’t know what. What context to use in that particular scenario, so you can go back and kind of check that later to actually kind of make sure that you’re passing context around properly.

So the easiest way to do this is there like get a context background and pass pass it to the ping with context, but this essentially doesn’t really do anything. There’s no timeouts or anything like that associated with this. So this is just going to give us a kind of background of or blank context, the same thing with to do so what we want to do is be able to say like either cancel it partway through, and so in this case, what we’re going to do Is kind of cancel it instead of like setting a timeout we’re going to just say you know, add the risk cancel wrapper to this.

So if we call with cancel this is going to give us another context, but also significantly it’s going to give it to this little cancel, and this cancel is actually a function that we can use that we can call to cancel our context. So if we do this, we can say with time after func, you know past one second call cancel, and so after one second we’re actually going to cancel our context. So if Google doesn’t respond within one second, we’re actually going to cancel the context and turn out of here and we’ll end up getting this could not ping Google error? Actually, we will get a if we look back at the code.

We will get a this context. Error and will actually see that, as could not paint Google and then it will say like context canceled or something similar like that. So another thing that you can use is to you can set timeouts, which essentially do the same thing as we did here, which is, after a second or two rule, call cancel. And here what we’re going to do is called the with timeout function, which gives us just where you pass the context and a deadline, a timeout and then with the final thing that you can do with context is actually pass a value to them.

And this can be used as kind of a you know. Maybe a request. Global type of you know key value store that you can use throughout the application. I’r not going to really talk about this too much, but you can essentially add some keys and values to a particular context and then use that throughout your request. So like say, you want to set a request ID and then log the request ID throughout the application.

You can use something like that here, so these context values as I’m doing this. I mentioned that it caused, creates this key structure, so we need call it background. It creates idiot, it creates this background or it uses this background context, which is a global route context, and then, when we call with cancel or with value from the context package, it’s actually going to create us a new context that does a that creates a a Sub sub context, that is a or is it a child of the the background context, and then it as we do this.

It creates a new and newer context to form a kind of tree, so you can kind of create call on the the valid the value that got returned from with cancel. You can actually call things like with deadlines or with value on those as well, and it forms this kind of tree structure. So if any one of these like say this, one that was returned by with value at the bottom here becomes finishes, then all those contexts above it will then continue.

It will kind of propagate throughout the system back to the via to the original context. So, let’s kind of go back so the deadlines exceeded error is, is one of the errors within context that can kind of get used to that. You can kind of check when these are, when you use the this West deadline and there’s also a cancelled error as well that you can check for so this is another, so this is so when you get the son of errors back from your context, this is Something that you should actually kind of handle these type of errors, gracefully not just or don’t just check them.

You actually have to handle them, and this is actually written by was said by somebody who had believed in the audience. It kind of looks like this vaguely, but yeah he’s. This is actually a really really good thing to say or a thing to follow in that you really want to be able to handle things like handling timeouts and handling these types of things gracefully. But what happens? A lot of times is that folks, don’t actually use the context package, and so they don’t even know that there’s it’s not like an error gets passed back to them.

It’s a nice that they’re not even kind of handling something or they’re, not even understanding that there could be an error to begin with. So and that’s that’s the case with things like deadlines and timeouts. So I’m going to kind of skip over these a little bit. But essentially, what you can kind of do with the the cancel is you can do things like actually cancel it later um, but I think what I’m going to do is actually kind of go through and show you a little bit about how to actually use the Package itself and what that looks like kind of in practice right so here I’ve got a kind of application that is going to do to basically wait for a certain number of seconds and then it’s going to print a message right.

This is a very simple application, and so here I have kind of a main method that, where I’m doing getting this context and then I’m going to wrap that another with another context here with the Swiss timeout and give it. You know this four seconds. So this is kind of how we did this with the or how I show that you can use this to context package, but just this will actually show you how, within your applications, you can actually receive a context and then kind of actually make use of it.

So here the de nieve way to do it would be just to do something like time sleep. I think you could just take the duration here and then you would just say log print. You know: what’s our message here: msg right and if we do something like just run, this go run, startup go. It will essentially wait for three seconds and then print our message, hello, right and if we said that this timeout was two seconds we would.

We might expect that that we would actually timeout, but that’s actually not the case, we’re actually going to just we’re actually going to wait two three seconds and then print our message. So why is that? The reason? Why is because we have to actually check that to make sure that the context is done and we’ll do that using the Select using select to kind of check the the done the done blog on the context as well as checking when app-like will check the timer.

That will set for the original three-second. So the first thing we’ll do is handle the base case, which is to check time after our de regular duration and then we’ll just say a log print message and then our other case, which is to check. If the context is done, we’ll actually check the context, blog TX done and then we’ll just say. Actually this doesn’t return an error so we’ll just log that here as well, pretest and we’ll just say: V CT X, dot, v RR like this right.

So this should actually check to check our context so that when we, if the context actually times out, then it will return a value on this context, done blog and I’ll actually drop out here and print the error that came back from the context. So you might expect as well that if we extend this deadline here, that it will actually run through to completion and for an hour, hello message. So that’s great, that’s really interesting, but the real power.

What comes when you have like things like clients, types of applications, client-server type of applications, so I’m going to drop out to a client app and what this is going to do is just kind of ping. Our do a request to a local host, and so what we’re going to do here is create a context that we’re going to send it to our web server. That’s going to actually it’s going to wait, set a timeout for our for our web server.

So we’ll say, see: TX TX, equals context background and will actually set a context, cancel a rules, yech context with timeout TX and then we’ll set, it is to say like three seconds and then we’ll defer the cancel. So the cancel actually deferring, cancel and running cancel here is important because this actually sets up some timer or you set some some resources using to to run the timer. And so we need to actually cancel that so that make sure that those are freed and then we’re going to set the request for our HTTP server to request with context, I believe, is the right incantation.

So here we’ve created a HTTP request and then we’ve created our context and we’re setting it there with context. This should be request. My fake CTX. Let me if I remember right here, it’s going to complain about this. A little bit call to voice context. It’s you P request context, think it was that yeah, that’s right, because this is actually a method: okay, yeah! So it’s! This is actually a compiling now.

So then, we’ll actually go jump over to our server app and we’ll implement, essentially the same sort of application, but we’re going to like return our hello message from the web server. So here we can say we’ll have this sleep and talk and we’ll say, select on this and run this guy again. Essentially after time, it’s kinda after D, then we’ll say, take this writer and then write a gosh. I had this as a string, so miss a GE and then this is a case gets done and then you’ll return.

This is actually not returning error, but let’s see just log the error message: protests TX start there right so with our application. Here we should we’re going to pass it the the response writer for our for our HTTP server, so that our right to the client and then we’ll have this we’ll. Have it wait for five seconds before returning hello to the client right and I’m going to jump out here and actually run this just allow that and then, if we jump back over to our client, you should see that we have a timeout here of three seconds.

So we can expect that our client will actually timeout this time. Okay, so the client deadline exceeded is the error message that we got out here because we’re actually the client times out after three seconds, but our server is, is actually waiting five seconds before a before printing the message, so our server is taking actually five seconds to Have returned a message, so we can kind of extend this to actually make it work, but or we can go back to our server side application and go to the five seconds here and make this two instead and then, if we do that, then our client app One should actually return the hello message rate, so so this is a great way to like from the clients perspective, as well as the service perspective, so like.

If the client makes a request to a server, that’s going to take a long time, you want to be able to set timeouts so that you’re going to actually be able to to like not wait forever and on the server side as well. You don’t want to wait for clients to send their their the request forever right. You, the server, wants to timeout as well, and so it’s essential that you actually set these timeouts, but this is actually something that a lot of people don’t do so who are? Who actually, who actually write API servers or a rights web servers and go actually is checking this sort of context and setting timeouts? So what let’s? Let’s raise your hand? How many people are actually writing? Api servers are web servers and go just altogether, okay and then how many people are actually using context to check timeout? I thought Dave Cheney actually lowered his hand here.

So even Dave Cheney doesn’t even do this so, but one of the one of the actual you know you could imagine that this sort of thing is really powerful for things like client, libraries that make that connects to web servers and that sort of thing and connect To the cloud api’s or whatnot, and so one of the the libraries that actually makes kind of judicious use of the context package and make sure that it you can set timeouts and you can pass context properly, is the library from the go Club.

The Google cloud platform, the API libraries and go for Google cloud platform, so I’m going to kind of switch over here and have a Schilling talk a little bit about the about GCP and the client libraries and go. Do you have yeah yeah? My market is one all right. Alright, thanks again, for sharing is because using context is very important if you to API, otherwise you’re thousands of the routine three blocks back bogus clients or non-responsive servers.

So today I’m going to be shared. It be more about gold micro service support, whatever API app summer on the clouds. Of course, I work at Google and mr. Educator engineer, so I know other cloud provider provide relatively good go support and we do focus on Google ami all right when talking about goal. Supporting cloud, we really mean two things actually one: how do you graduate you? Have some clouds in this case, Pescador gave up gtp the otherwise how go is well supported the client API language on Google cloud.

I have prepared a short demo to show you how to use kubernetes thanks or Matthew from each ocean sharing credit. Is I’m going to shoot any reaction? How do you run the backing application according activity on creditors? Parent, so good, as you know, is the red wine. Doesn’t run everywhere if I have to compare comp, our people have only runs everywhere, yeah some even see I don’t know so in somebody.

Google’s Google out ad support many many ways: gramma collapses. As you know, so Google cloud offers fresh machine. Obviously, obviously you can compile your go into binary and create a system D. In this query, whatever deploy to a virtual machine, your Vic would go right. Alternatively, you can run in users groups. You can create docker container mounted to Google Cloud instance group or to skills as a CPU resources.

You can monitor bike ups at the R and so forth. You can also run Google App Engine flags, which is a new feature. We just release general availability. You can create a darker version, top image and develop Google Cloud specify. How do you want to run your apps English? What kind of skill the last one we offer is I discovered in this cover needs, as you know, is open source distributed scheduler for proper containers.

So there are some good and bad about all this, so virtual machine is very simple, but it is ready to manage. Instance. Group is the slip slightly better, yet is still preparatory, and there you need step office to support that happen. Deflects, the full zero ops support and you can really scale to almost unlimited skill. As you probably know, snapchat ran from App Engine, but it still prefers if you prefer not to be locked in then community system of your best option available moon cloud in terms of API support.

I have to assure you golden is the first-class season of Google cloud client language. If you look at all the APS, we provide almost all the major products we have gola if you have to work. If we don’t it’s a very, very rare case that you can go to the HTTP API level by using your own do programming language, I just think some snippet for for that, for example, if you want to use Google Cloud storage, which is the Amazon s3 equivalent, You really need to get a context, then create new clients and refer to Bucky an object.

Then you define a Twitter, you start reading the object, the simple as that. If you want to write object, you just needs change here and changes to a new writer. Then you can start ready to. The object. Is the English few lines of code? You can start writing and reading from Google Cloud Storage. The other one example is the Google cloud BigTable. This is our managed HBase version right with this example.

You really can open the client a few lines of code and the do start scanning by prefix, and you can map the rows to a function and the iteration for the rows in the few lines as well. Another example of it is a store, so this I’m not going to go through the details but is seeming somewhat similar. You just need to create a client and start writing entities and you are good to go. The last line spanner 7, is our new distributed, strong, consistent new cinco database.

So vendor is known. Nothing spencey here you just create a client issue, SQL statement with binding a parameter to a certain value. You run it. You get results back and go in a few lines of code and all this code you can easily copy and paste or go and start running in next 10 seconds or so so here I have a short demo for you. I’r going to show you the code, then I’m going to show you how to use deploy this run incriminates.

How do you scale this application? So I have pre created a spanner table on Google Cloud is very simple, is running in Taiwan data center. I create this demo is for core banking application. Imagine you are doing a banking application supports a few transactions like open account. You put the money to the accounts, Debbie money from the account and transfer money from account a July zombie. That’s all I want to do today so have two tables like the comfortable.

It’s very very simple. I have the account number and balance and have transaction table which has what’s a setting ID and when did it happen, what this attraction is about and which the account involves the amount involved with this successful, not right, that’s really simple schema. You can actually look at the table and you can look at the article. You can see it’s created using standard sequel using Create table syntax.

Alright, then I have a pre created container engine. So, if you don’t know you want to create kubernetes cluster, you don’t have to spend five 10 hours or type of stage to do is just equal to Google Cloud and create cluster and special web. How much CPU, how much RAM we need. Then, in a few minutes you have a cluster than the on cloud. I have to have this container cluster created here, as you can see, have a made 36b CPU 135 gb of ram, so you just give up scale down easily.

I just frequent this for demo purpose. Looking at the code, oh okay, yeah, look at the code. I really I really have a transaction manager here and everything else is basically obviously wrapping code which can be available through GRDC running on the cloud. So I’m going to talk about the TR transaction manager. Ladies only so have a table. I have a few operations like open accounts, supported debits credits, get a trading account information and transfer, so I’m going to skip all the rest.

I’ve spent like one minute here explaining a spinner on the context: API 8 in there yeah contact a page here, yeah, that’s very important, so so it’s better doing through the transaction. You probably you’ve already know in closing JDBC just start and second setup commit the falls to whatever you do in commit in spinner you best operation together, you run it it’s all in all successful or fail. So we all manage my strategy for you.

So what I did here is really read it combatants of account one and we kind of read the balance of a country right then I add the balance to account and debit from one account one just negative yeah I can. I can make it bigger. Unfortunately, I think it’s the resolution issues, but I hope you can see it clearly. Sorry. So it’s very simple: it’s very simple to division body. You get balance of two accounts, add to the second account and do something about it and save it together.

So ever ever demo banking application as a standalone. I just want to run it here to show you works. Ok, ok! This is banking Co. I am going to open account. Eight eight eight, eight okay, open now open okay, I read it account how much money I have of course, zero right. I have already pre opened one account. Okay, I don’t have a one one: seven, three dollars a transfer to eight eight, eight, seven dollars: okay, a transfer successfully already a’muslim value is 7 right, at least one one, one, each one, six six, so you worked as a task right.

How do I run in cloud right now, so I have to go to the cloud console. I already have a machine running in cloud, so I’m going to attach it in there and show you how to deploy a cloud and how simple it is. Do we have Wi-Fi yes sure, so, I’m going to build and deploy for simplicity authority to have damage in the single shell script. I probably have to use the line work, what slope, but maybe later so I export a good path.

I basically compile the soft code right. I copy the docker file to the binary, folder and start building dirges in the docker field and their tag it and the cloud. I push you to Google Cloud as a one version of the of the code, so I’m going to build it. You finish you in second, because the code hasn’t been changed. Yet. Oh someone changed it. Okay, we will push into Google Cloud. This done. Okay, so in seconds I push this kota-kun cloud now I want to push to the kubernetes cluster.

I want to run five copy of the company’s rotation of the scheduling, at least if Michael, had ballet crashed after three days community of star one immediately mini seconds. So in kubernetes I really create a replication controller. So really really what does it? Do? You really say: okay, I’m a replication, solar disk sure I have a certain number of replicas running on the cloud. The number of radicals – five, the name of the replicas starting with banking RTC and had some labels there and, most importantly, the image image here – is actually the image I deployed at now.

So with this I can use overcooks it here to deploy the cloud yeah guess what is running by parties. Now, it’s not happy. I own around ten copies, because that we need a skill in the few minutes. In a few seconds you can see. 10 cops are running one still creating I’ll do again. You’ll see they’re all running you all wired behind load balancer. I have free creative equal to a bunch of takes like three minutes.

Okay, now I’m going to run the demo on untested listing out I’m going to use ten threats, a factors rent end transaction on a new server. Then I’m going to use finder stress, 100. Thousands of transactions, four beckons all right. That’s about a little bit close to 3,000 clip yet and with 3,000, keep us in, for we can handle 260 million per second per day, and that’s probably enough for China and you wan na spend find a dollar same amount on this.

Thank you very much. Oh. We don’t hold you back for lunch, but apparently I’m happy to wear happier to answer. We are yeah. Let’s just go too much. Yes,


By Jimmy Dagger

Find out my interests on my awesome blog!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.