This is the knowledge summary of September 2021.

Research and development of coding

C / C++

No.

golang

A few clips related to the time shift:

exTime := "20210901" mytime, _ := time.Parse("20060102", exTime) fmt.Println(mytime.UTC().Unix(), Mytime.local ().unix ()) // Convert mytime to Local time format, _ = time.parseinLocation ("20060102", exTime, Time.local) ftt.println (mytime.utc ().unix (), mytime.local ().unix ()) exTime = "2020-09-17t20:00:27" Parse("2006-01-02 15:04:05", exTime) themonth := int(mytime.month ()) // If mytime.year () == 1 && themonth == 1 && mytime.day () == 1 {mytime, _ = time.Parse("2006-01-02T15:04:05", exTime) themonth = int(mytime.Month()) }Copy the code

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

Code snippet for executing external commands:

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

When exec.Command is used to execute the command, the first two parameters are sh and -c. If the command is not used, an error occurs. The killall command for deleting a process is also feasible. I found some posts on the Internet, and there was a mention of using pkill -sigint, so I used it. Using the Start function starts the process and does not block. You can use the Run function if you want to execute a command that returns a value or waits for it to complete.

Example of writing a file. Nginx configuration file nginx.conf content, according to the parameters, add different URLS and corresponding ports in the location. Because there are special characters, it is easy to use backquotes. String assembly uses FMT.Sprintf. Call ioutil.WriteFile to write the file.

// Define config string, 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 configuration file configEnd :='}} '// format only for the address and port of location. Url := "test9001" port := 9001 TMPSTR := ftt. 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)Copy the code

Note: The above configuration is not complete.

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/binCopy the code

Note: During the image construction, observe the output log. The environment variables of the physical machine will be resolved. For example, the PATH in the last line will change to the physical PATH. If you use an environment variable defined by Dockerfile, it does not seem to take effect.

Linux

zip

For the same file or directory, if zip is compressed at different points in time, the MD5 values of the obtained ZIP files will be different (if the interval is very short, this problem does not exist). To solve this problem, run -x -d. For example:

zip -qr -X -D c.zip hello/
Copy the code

Reference stackoverflow.com/questions/1…

Other encoding

Some projects are more difficult to track, some are not well organized and logical, and some are caused by unclear clues. For a C++ class, an example of a business process is as follows:

ret = get(m_path); // Use the member variable m_PATH search_DIR (m_PATH); Mysort (); mysort(); mysort(); mysort()Copy the code

The details of this example are shown in the comments above, which is fine from the comments. However, it is difficult to see the logic purely from the function calls and parameters. You need to trace each function and its corresponding member variables to clarify the data flow. My personal opinion on this is: don’t use class member variables if you have utility functions in your class. In business functions, you can see the main thread from member variables. Or add a comment.

Research and development of knowledge

Reference some open source project git submission log format, divided into “head body tail” three parts. According to the actual situation, tidy up yourself.

Header: indicates the type and scope of influence of this submission. For example: format modification test reconstruction bug correction (scope of impact) Text: describes the changes submitted this time, can be listed. Tail: Some notes or notesCopy the code

Practical example:

Header bug (Time synchronization module) The body time synchronization module reduces the time difference threshold to avoid fluctuation. Tail (appendix) close #1Copy the code

Research and development of thinking

Do the right thing at the right time

Wanted to enter the background management system, selected Vue + Golang route. Checked a lot of information, also downloaded a lot of project source code, found not to learn. It turned out that the project was too complex for beginners. So give up, so far because of other things, have not been able to start writing. This month we started to make real organizational innovation, Jenkins is good, but Java only read a little syntax, the project is springboot, at the beginning of the IDEA project did not understand the establishment of JDK 1.8, 16 and other versions. It took a couple of days to make it work. IDEA is too powerful, Java framework too much too mature. However, because I am too mature, I do not like research, and I am used to C++.

Work record

A project has custom logging functions, sqlite3 database operations, and so on. The SQlite3 database file was corrupted after the device was suddenly powered down and restarted, and the binary was checked to show partial logs at an offset. Puzzled, has not been solved. However, I used cppCheck and valGrind checks to fix some coding errors/warnings. Remove a function that has no real use (called but has no effect).

In the 64-bit system, %ld is used to print the uint64_t type variable, which is normal. However, in the 32-bit system, a segment error is reported and % LLD is used to print. However, there is a warning in the 64-bit system, so we have no choice but to change the uint64_t type to long long. Fortunately, it was found in the pilot stage, otherwise it would have a big impact.

A project connects to two SFTP servers in the primary and secondary systems. The project downloads XML files and determines which system has the latest file version. The MD5 values of the same file are different, causing repeated downloads. It is found that the file is compressed at different times. The MD5 value of the compressed ZIP file changes even though the contents remain unchanged. After checking, the parameter can be consistent with MD5. Since the project is carried out by another team and implemented in Java, it is not known how to modify it.

A certain project needed to download files in a directory of a remote server and set an array variable char remoteFile[128]. Everything was fine before, but after the backup system was switched recently, an error occurred in a production environment and the download could not be achieved, resulting in traffic jam. Operation and maintenance colleagues contacted for emergency treatment. After the switchover, the length of the remote directory file exceeds 128 bytes and only exceeds 2 to 3 characters. The server IP used in the pilot site is relatively short (such as 10.0.45.16) and has not been detected, but some server IP is longer (such as 10.100.168.231), just more than a few characters, so it failed. After locating the cause, change the size of related variables to a larger size to resolve the problem.

Participated in some work of opening a certain road section. In the early stage, some data did not meet the requirements after tool inspection, and it was confirmed with other colleagues that it was not affected. In the pilot test, another server tool could not work normally and the data was not updated. The problem was found two days before the launch, so I worked overtime. Experience gained after the review: consult colleagues and report to the leader if it does not conform to the established rules (it did not adhere to or report to the leader at the beginning); Parameter modification, multi-party evaluation is required (other colleagues are responsible for); The production environment needs to be monitored.

What to do and plan for this month

Spare some time to complete the writing and testing of forwarding tools, and study and implement some load balancing algorithms.

I’ve recently noticed that my ability to express myself and think is declining, and I plan to read non-technical books.

Other intravenous drip

This month for the first time to repay the loan, officially began mortgage slave life. Personal expenses have to be more economical.