This is a creation in Article, where the information may have evolved or changed.
Their usual work is basically in PHP and Nodejs between the hovering, but now facing the fierce attack of Python and Java, in fact, there is a faint pain in the heart "PHP is the best language in the World", "Nodejs in CPU-intensive service completely paralyzed" ...
Look at the half-month Python real discovery, in fact, it is too similar to PHP language, so the basic need not how to understand the will. Golang watched for 1 months really have to write more to see the source code to harvest, although only 30 a few keywords but the content is really many, golang performance is really high can greatly reduce server overhead, for example Web services PHP needs 100 machines, Then Golang may only need 10 units or less!
Recently in the study of MySQL proxy, in fact, MySQL itself is a support agent, but want to try to do this will be very flexible:
- Flexible SLB MySQL Load balancer
- Read and write directly through proxy directly to judge
- Early warning or rejection of risky SQL
- ... Too much, too much, too many benefits.
The following is the code for Golang MySQL Proxy
Package main import ("Net" "FMT" "Time") const (mysql_address = "Mysq-host" Mysql_port = " 3306 ") Func main () {listener, err: = Net. Listen ("TCP", ": 1234") if err! = Nil {fmt. PRINTLN ("TCP", err. Error ()) return} else {fmt. PRINTLN ("TCP", listener. ADDR (), "Success")} for {user, err: = Listener. Accept () if err! = Nil {fmt. PRINTLN ("Accept Error:", err.) Error ()) Continue} go proxyrequest (user)}}//Manage user request Func Proxyreques T (user net. Conn) {fmt. Println (user. Localaddr ()) Bytes: = Make ([]byte, 10240) conn, err: = Net. Dial ("TCP", Mysql_address + ":" + mysql_port) if err! = Nil {fmt. PRINTLN (Conn. Remoteaddr (), "Error:", err. Error ()) Conn. Close () return} ch: = Make (chan bool, 1) Go proxyresponse (User, conn, ch) For {n, err: = user. Read (bytes) If err! = nil {break} fmt. Println (String (Bytes[:n])) Conn. Write (Bytes[:n])} defer close (ch) Select {Case <-ch:conn. Close () user. Close () fmt. PRINTLN ("proxy over") case <-time. After (time. Second *): FMT. PRINTLN ("Proxy timeout")}}//The proxy service is returned to the user func proxyresponse (user net. Conn, service net. Conn, CH chan bool) {bytes: = make ([]byte, 10240) for {n, err: = service. Read (bytes) If err! = nil {break} user. Write (bytes[:n])} ch <-true}
The following is a simple proxy for Nodejs
var net = require (' net '); var model = require ('.. /.. /models/proxy '); var trace = require ('.. /.. /libs/trace ');//proxy table var proxys = [];//tcp servervar server = null;//proxy Informationvar information = {count:0, success: 0, error:0};/** * Start service * @param info array * @param callback function */exports.start = (info, callback) and {model . Getproxylistfromserver (Info.id, (err, result) = {if (err) {callback (err, err); } else {Proxys = result; Initserver (Info.port, Info.to_host, Info.to_port); callback (NULL); } });};/ * * Stop service. * @return bool */exports.stop = function () {if (server && server.listening) {server.close (); Server = NULL; return true; } return false;};/ * * Get information * @return Object */exports.getinfo = () = () = {return information;};/ * * Initialize TCP proxy server. * @param port * @param tohost * @param toPort */function initserver (port, Tohost, toPort) {server = Net.cReateserver (client) = {information.count++; Client.on (' End ', () = {connect.end (); }); var connect = net.createconnection ({host:tohost, port:toport}, (err) = = {}); Connect.on (' Error ', (err) = {information.error++; }); Connect.on (' End ', () = {information.success++; }); Client.pipe (connect); Connect.pipe (client); Client.on (' Data ', function (data) {//var buf = buffer.from (data); Console.log (' data: ' + buf.tostring ()); // }); }); Server.listen (port, (err) = = {if (err) {Trace.log (' Proxy server error ', err); Process.exit (); } trace.log (' Proxy server started ', port); });}