URL Path To Array In Golang
Here’s a short Go function that converts the URL path into an array; it’s useful if you want to implement REST style queries into a simple application.
func parseComponents(r *http.Request) []string {
//Creates an App Engine Context - required to access App Engine specific services
c := appengine.NewContext(r)
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Log the URL received:
c.Infof("URL: ", path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cut_off_last_char_len := len(path) - 1
path = path[:cut_off_last_char_len]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}
Free Tier Performance
Two weeks ago I posted a picture of a free tier application serving close to 52,000 requests in 1 day. Soon after that, the same application exceeded that by processing over 53,000 requests within a single day. Here’s a few pictures demonstrating the capabilities of GAE’s free tier:
First off, here’s a picture of the Current Load screen:
A graph of milliseconds/request over the same time period:
The instances used:
And finally requests/second:
App Engine’s free tier is generally enough to host applications during development, testing, and low-level production traffic. With that said, it’s always a good idea to activate billing if you believe that your application will be receiving more than small amounts of traffic.
Reading In A File With Java
Inspecting Task Queue Logs
Here’s a sample view of a request from the Task Queue service. The log includes the name of the queue (in this case default), the task’s name (the long number starting with 1636460), and the instance that handled the request (the blue text on the bottom).
An important note: a quick and easy way to figure out if a request comes from the Task Queue is to look at the request’s IP address. All Task Queue requests come from the IP 0.1.0.2.
Generating MD5 Hashes In Java
Retrieving All Recently Created Entities
Error Code 202
On high traffic App Engine applications, you may occasionally see a request fail with error code 202. You’ll see the following text in your logs:
A problem was encountered with the process that handled this request, causing it to exit. This is likely to cause a new process to be used for the next request to your application. (Error code 202)
The request log will look similar to the picture below:
Error code 202 is an internal App Engine failure code: an unknown error occurred within App Engine or its associated services, not from your application. This is not an error that your application can catch or recover from, nor is the application at fault for this error. The current request will fail, but the client is free to retry the request. If this request is from the task queue service, the task queue will automatically retry the request if you set the appropriate settings while creating the task.
A note about error codes: the code 202 relates to App Engine; it is NOT a HTTP status code. When a request encounters this internal error, it fails with the HTTP status code 500 (as you can see from the above picture).
HTTP Basic Access Authorization In Golang
One of the simplest and oldest methods of authorization is HTTP Basic authorization. While it isn’t as secure as the Users service of App Engine or the OAuth authorization model, it’s easy to implement and looks visually impressive depending on the user’s browser.
Here’s a picture of the HTTP Basic authorization prompt in IE10 on Windows 8:
The variable authorization contains a base64 encoded hash generated by the user’s browser, created by concatenating the username and password together with a colon: username:password. R represents a http.Request reference, c is appengine.Context, and w is http.ResponseWriter.
//Get the authorization header.
authorization_array := r.Header["Authorization"]
if len(authorization_array) > 0 {
authorization := strings.TrimSpace(authorization_array[0])
c.Infof("Authorization: ", authorization)
} else {
w.Header().Set("WWW-Authenticate", "Basic realm=\"user\"")
http.Error(w, http.StatusText(401), 401)
}