Scaling Go Applications: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Line 85: Line 85:
...
...
     go http.ListenAndServe(":3000", new(util.GzipHandler))
     go http.ListenAndServe(":3000", new(util.GzipHandler))
</syntaxhighlight>
==Move to HTTP/2==
This provides
*Header compression
*Reusable Connections
*Server Push
To move to http/2 we need to run over https. Within go you can run this but I do mine manually
<syntaxhighlight lang="bash">
go run /usr/lib/go/src/crypto/tls/generate_cert.go -host localhost
</syntaxhighlight>
Then to use you just need to change the ListenAndServ to ListenAndServTLS and add the cert and key. I did not have problems using the self signed certs but here is what some people have done.
<syntaxhighlight lang="go">
package data
import (
"crypto/tls"
"flag"
"net/http"
)
var dataServiceUrl = flag.String("dataservice", "https://localhost:4000", "Address of the data service provider")
func init() {
tr := http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
http.DefaultClient = &http.Client{Transport: &tr}
}
</syntaxhighlight>
</syntaxhighlight>



Revision as of 12:37, 28 January 2021

Introduction

Resources

When we look at scaling we must consider the following resources.

  • Network Bandwidth
  • Processing Power
  • Available Memory
  • Data Storage

For this page we will be looking a the challenges around breaking an application in to several servers and following an architecture such as below This not meant to be THE solution but a walkthrough on some of the challenges to scale. This will be split into

  • Initial Optimizations
  • Creating a Load Balancer
  • Caching
  • Centralized Logging

AGAIN this is an approach and of course you can use third-party cloud solutions. This one will be confined to Docker and GO.

Initial Optimizations

Content Compression

One of the easiest things to do is to change the http messages to use compression in our messages.

Gzip Handler

For this we write a GzipHandler.

package util

import (
	"compress/gzip"
	"net/http"
	"strings"
)

type CloseableResponseWriter interface {
	http.ResponseWriter
	Close()
}

type gzipResponseWriter struct {
	http.ResponseWriter
	*gzip.Writer
}

func (w gzipResponseWriter) Write(data []byte) (int, error) {
	return w.Writer.Write(data)
}

func (w gzipResponseWriter) Close() {
	w.Writer.Close()
}

func (w gzipResponseWriter) Header() http.Header {
	return w.ResponseWriter.Header()
}

type closeableResponseWriter struct {
	http.ResponseWriter
}

func (w closeableResponseWriter) Close() {}

func GetResponseWriter(w http.ResponseWriter, req *http.Request) CloseableResponseWriter {
	if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
		w.Header().Set("Content-Encoding", "gzip")
		gRW := gzipResponseWriter{
			ResponseWriter: w,
			Writer:         gzip.NewWriter(w),
		}
		return gRW
	} else {
		return closeableResponseWriter{ResponseWriter: w}
	}
}

type GzipHandler struct{}

func (h *GzipHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	responseWriter := GetResponseWriter(w, r)
	defer responseWriter.Close()

	http.DefaultServeMux.ServeHTTP(responseWriter, r)
}

Implement on Server

To we can now use this within the server.

func main() {
...
    go http.ListenAndServe(":3000", new(util.GzipHandler))

Move to HTTP/2

This provides

  • Header compression
  • Reusable Connections
  • Server Push

To move to http/2 we need to run over https. Within go you can run this but I do mine manually

go run /usr/lib/go/src/crypto/tls/generate_cert.go -host localhost

Then to use you just need to change the ListenAndServ to ListenAndServTLS and add the cert and key. I did not have problems using the self signed certs but here is what some people have done.

package data

import (
	"crypto/tls"
	"flag"
	"net/http"
)

var dataServiceUrl = flag.String("dataservice", "https://localhost:4000", "Address of the data service provider")

func init() {
	tr := http.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}

	http.DefaultClient = &http.Client{Transport: &tr}
}

Creating a Load Balancer

Caching

Centralized Logging