Summary of Li Chi's Knowledge in September 2021

Keywords: Go Nginx

This paper is a summary of knowledge for September 221.

R&D Coding

C / C++

None.

golang

Several passages related to time conversion:

    exTime := "20210901"
    mytime, _ := time.Parse("20060102", exTime)
    fmt.Println(mytime.UTC().Unix(), mytime.Local().Unix())
    
    // Convert in local time format
    mytime, _ = time.ParseInLocation("20060102", exTime, time.Local)
    fmt.Println(mytime.UTC().Unix(), mytime.Local().Unix())
    
    exTime = "2020-09-17T20:00:27"
    // There are two formats for time, so let's see here
	mytime, _ := time.Parse("2006-01-02 15:04:05", exTime)
	themonth := int(mytime.Month())
	// If not legal, year, month and day are 1
	if mytime.Year() == 1 && themonth == 1 && mytime.Day() == 1 {
		mytime, _ = time.Parse("2006-01-02T15:04:05", exTime)
		themonth = int(mytime.Month())
	}

When golang converts time, the value of the time template is fixed and must be the corresponding value in 2006-01-02T15:04:05, but the character of the interval can change, such as 20060102 in the example. When specifying a time string to be converted to a time stamp, use the ParseInLocation function to specify time.Local, otherwise the converted value will be added to the time zone (e.g., East Eight).

Execute external command snippet:

Delete process:
appname := "./httpforward_back.exe"
port := 9000
exec.Command("sh", "-c", fmt.Sprintf("pkill -SIGINT %s", appname[2:])).Output()
Start the process:
cmd := exec.Command("sh", "-c", fmt.Sprintf("%s -p %d -i \"run in port %d\" &", appname, port, port))
err := cmd.Start()

When exec.Command is used to execute a command, the first two parameters are sh and-c. If not used, errors will occur. It is also possible to use killall for the command to delete a process. Some posts have been found on the foreign network and pkill has been mentioned.-SIGINT, so use it. The Start function is used to start a process without blocking it. If the command you want to execute needs a return value or wait for it to finish executing, you can use the Run function.

Example of writing a file.
Requirements: Customize the nginx configuration file nginx.conf content in the program, add different URL s and corresponding ports in location according to the parameters.
KNOWLEDGE POINT: Because of special characters, it is easy to read and manipulate using inverted quotes. String assembly uses fmt.Sprintf. Call ioutil.WriteFile to write a file.

// Define the config string, something unchanged here
var config = `

http {
	log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
						'$status $body_bytes_sent "$http_referer" '
						'"$http_user_agent" "$http_x_forwarded_for"';
	access_log  /var/opt/rh/rh-nginx116/log/nginx/access.log  main;
	sendfile        on;
	tcp_nopush      on;
	tcp_nodelay     on;
	keepalive_timeout  65;
	server {
		listen       8080 default_server;
		listen       [::]:8080 default_server;
		server_name  _;
		root         /opt/app-root/src;
		include      /opt/app-root/etc/nginx.default.d/*.conf;

		location / {
		}
		location /foo/test9000 {
			fastcgi_pass 127.0.0.1:9000;
			fastcgi_index index.cgi;
			fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
			include fastcgi.conf;
		}
`
    // This is the end symbol of the profile
	configend := `
    }
}`
    // Formatting is only for the address and port of the location.
    // If you add more than one, just write in format
	url := "test9001"
	port := 9001
	tmpstr := fmt.Sprintf(`
        location /foo/%s {
		    fastcgi_pass 127.0.0.1:%d;
		    fastcgi_index index.cgi;
		    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
		    include fastcgi.conf;
	}
}`, url, port)

	config = config + tmpstr + configend

	klog.Println("write and reload nginx")
	ioutil.WriteFile("/etc/nginx/nginx.conf", []byte(config), 0666)

Note: The above configuration is incomplete.

Docker

Set environment variables in Dockerfile:

ENV MAVEN_HOME /usr/local/apache-maven-3.8.2

ENV GOROOT /usr/local/go
ENV GOBIN /usr/local/go/bin
ENV GOPROXY https://goproxy.io,direct

# seems cant pass $MAVEN_HOME here
ENV PATH $PATH:/usr/local/apache-maven-3.8.2/bin:/usr/local/go/bin

Note: In the build image, looking at the output log will parse in the physical machine's environment variables, such as $PATH in the last line will become the physical value of $PATH. It does not seem to work if the environment variables defined by Dockerfile are used.

Linux

zip compression

For the same file/directory, zip compression at different time points will result in different md5 values for zip files (very short time intervals do not exist). Use -X-D to solve, example:

zip -qr -X -D c.zip hello/

Reference resourceshttps://stackoverflow.com/questions/19523063/zip-utility-giving-me-different-md5sum-every-time-in-linux

Encode Others

Some engineering tracing is cumbersome, some due to poor organization and logic, some due to clues. For example, a C++ class, a business process example is as follows:

ret = get(m_path);  // Use member variable m_path
search_dir(m_path);  // Use member variable m_path
mysort()    // Without parameters, the member variable m_path previously assigned is used in the function, and the member variable m_file is set
changedate(m_file) // Pass in m_file directly here

The example is handled in the comments above, which are fine, but logic is harder to see from function calls and parameters alone. Tracking each function and its corresponding member variables is required to clarify the flow of data.
In this regard, my personal view is:
Do not use class member variables if there are tool functions in the class.
In business functions, you can see the main line from member variables. Or add a comment.

R&D knowledge

Refer to the git submission log format of some open source projects, which is divided into three parts: the header and the tail. Organize it yourself according to the actual situation.

Header: Give the type and scope of this submission. For example: Format modification test refactoring bug Amendment (Scope of Influence)
Text: Describe the changes submitted, list them.
Tail: some notes or notes

Actual examples:

head         bug Fix (Time Synchronization Module)
text         The time synchronization module reduces the threshold of time difference judgment to avoid fluctuations.
Tail (appendix) off#1

R&D thinking

Do the right thing at the right time

When I wanted to get started with the background management system, I chose the vue + golang route. I checked a lot of data, downloaded a lot of project source code, and found that I couldn't learn. Later, I found that the project was too complex to be suitable for beginners. I gave up, and I still couldn't write manually for other reasons.
This month, jenkins is doing a really good job of organizational innovation, but Java just looks at the syntax, the project is springboot, IDEA projects were not built at the beginning, and jdk versions 1.8, 16 and so on were not understood. It took a few days to harden your scalp. I just exclaimed that IDEA was too powerful, JavaToo many frameworks are too mature. But just because they are too mature, they don't like research, or they get used to C++.

Work Records

A project has custom log functions, operation sqlite3 database, etc. After the device was suddenly powered off and restarted, the sqlite3 database file was damaged, after checking its binary, there was a partial log at an offset. Bewildly puzzled, it has not been resolved. Actually, no tricks, then use cppcheck and ValgrindCheck to fix some coding errors/warnings. Delete functions that are not of real use (called but did not work).

A project needs to be compatible with 32-bit and 64-bit, printing uint64_t type variables with%ld in 64-bit systems. Normal, printing segment errors with%lld while running in 32-bit systems, but warnings appear in 64-bit systems. Fortunately, in the pilot phase, uint64_t is changed to long-long type. Otherwise, the impact is greater.

Two sftp servers are connected to the primary system of a project. The project downloads xml files and determines which system's file version is newer and downloads them. It then finds that the md5 values of the same file are different, causing continuous downloads. It is found that the file is packaged and compressed at different times, even if the content is unchanged, but the compressed zip file MD5The value has changed. It has been checked that md5 consistency can be maintained through parameters. Since the project is the responsibility of other teams and is implemented in Java, how to modify it is unknown.

A project needs to download files from a directory on a remote server, set an array variable char remoteFile[128]After switching the backup system recently, there was an error in a production environment that caused traffic jams and traffic jams the night before the Mid-Autumn Festival. After checking, the remote directory file length exceeded 128 bytes and only exceeded 2-3 characters. The server IP used in the pilot site was shorter (e.g. 10.0.45.16)., but some servers have long IP (e.g. 10.100.168.231), just over a few characters, and fail. After locating the cause, change the size of the related variable to solve the problem.

Initially, a tool check found that a data did not meet the requirements and confirmed with other colleagues that it did not affect. During the pilot test, another server tool failed to work properly, the data was not updated, the problem was found 2 days before going online, and therefore overtime.
Experience gained after revaluation: in case of inconsistency with the established rules, consult colleagues and report to the leaders (who did not insist or report at the beginning); parameter modification, which requires multi-party participation in evaluation (which is the responsibility of other colleagues); production environment needs monitoring means.

What to do and plan for this month

Take time to write and test the forwarding tool, and research and implement some load balancing algorithms.

Recently, I found that my expressive and thinking abilities have declined. I plan to see non-technical books.

Other drips

For the first time this month, the mortgage is being repaid, officially starting to live as a house slave. Individual spending needs to be saved even more.

Posted by benrussell on Thu, 30 Sep 2021 12:41:57 -0700