西维蜀黍

【Network】Benchmarking Unix Domain Socket

  ...


【Network】查看正在使用的 Unix Domain Socket

Linux

netstat

$ netstat -a -p --unix
  ...


【Golang】使用Unix Domain Socket

Server

package main

import (
	"log"
	"net"
	"os"
	"os/signal"
	"syscall"
)

func echoServer(c net.Conn) {
	for {
		buf := make([]byte, 512)
		nr, err := c.Read(buf)
		if err != nil {
			return
		}

		data := buf[0:nr]
		println("Server got:", string(data))
		_, err = c.Write(data)
		if err != nil {
			log.Fatal("Writing client error: ", err)
		}
	}
}

func main() {
	log.Println("Starting echo server")
	ln, err := net.Listen("unix", "/tmp/go.sock")
	if err != nil {
		log.Fatal("Listen error: ", err)
	}

	sigc := make(chan os.Signal, 1)
	signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
	go func(ln net.Listener, c chan os.Signal) {
		sig := <-c
		log.Printf("Caught signal %s: shutting down.", sig)
		ln.Close()
		os.Exit(0)
	}(ln, sigc)

	for {
		fd, err := ln.Accept()
		if err != nil {
			log.Fatal("Accept error: ", err)
		}

		go echoServer(fd)
	}
}
  ...


【Network】Unix Domain Socket

Unix Domain Socket (UDS)

A Unix domain socket, Unix socket, or IPC socket (inter-process communication socket) is a data communications endpoint for exchanging data between processes executing on the same host operating system. Valid socket types in the UNIX domain are:

  • SOCK_STREAM (compare to TCP) – for a stream-oriented socket
  • SOCK_DGRAM (compare to UDP) – for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don’t reorder datagrams)
  • SOCK_SEQPACKET (compare to SCTP) – for a sequenced-packet socket that is connection-oriented, preserves message boundaries, and delivers messages in the order that they were sent
  ...


【Prometheus】Histogram

Histogram

The histogram has several similarities to the summary. A histogram is a combination of various counters. Like summary metrics, histogram metrics are used to track the size of events, usually how long they take, via their observe method. There’s usually also the exact utilities to make it easy to time things as there are for summarys. Where they differ is their handling of quantiles.

  ...


【Prometheus】Storage

  ...


【Linux】命令 - journalctl

Boot Messages

# Show all messages from this [b]oot:
$ journalctl -b

# Show all messages from last [b]oot:
$ journalctl -b -1

# Show all messages from previous_boot's_offset [b]oot:
# e.g., the previous boot has an offset of -1, the boot before that is -2, and so on
$ journalctl -b -<offset>

# To list the boots of the system, use the following command.
$ journalctl --list-boots
-2 12792aa4791f4087a27f93a2bf4e191f Sun 2021-07-04 23:34:07 +08—Sun 2021-07-04 23:34:29 +08
-1 8e10828970ac4e7ebec55f268f6e945d Mon 2021-07-05 07:36:05 +08—Sun 2021-07-04 23:37:17 +08
 0 f027aa1e23c44e2c88d8203fc7f93575 Mon 2021-07-05 07:37:28 +08—Wed 2021-07-14 22:12:20 +08
  ...


【Docker】Contaienr OOM Debug

Problem

It’s one of the most common question that I come across: “Why is my container not running?”

Can docker container exit codes help troubleshoot this issue?

The first step in answering this question is to identify the exit code for the docker container. The exit code may give a hint as to what happened to stop the container running.

  ...


【Golang】benchmark

The Golang testing package contains a benchmarking facility that can be used to examine the performance of your Golang code. In this article we’ll see how to write simple benchmark tests that are able to provide us good insights about a given algorithmic solution.

  ...


【Linux】OS 如何保持当前时间

Time Stamp Counter

The Time Stamp Counter (TSC) is a 64-bit register present on all x86 processors since the Pentium. It counts the number of CPU cycles since its reset. The instruction RDTSC returns the TSC in EDX:EAX. In x86-64 mode, RDTSC also clears the upper 32 bits of RAX and RDX. Its opcode is 0F 31. Pentium competitors such as the Cyrix 6x86 did not always have a TSC and may consider RDTSC an illegal instruction. Cyrix included a Time Stamp Counter in their MII.

  ...