Skip to content

Instantly share code, notes, and snippets.

@hxzhouh
hxzhouh / atomic_1.go
Created February 28, 2024 03:18
The assembly code for the assignment statement is different for different architectures. On x86 platforms, _=i is split into two assembly inThe assembly code for the assignment statement is different for different architectures. On x86 platforms, _=i is split into two assembly instructions, so it's not atomic.
package main
const x int64 = 1 + 1<<33
func main() {
var i = x
_ = i
}
/**
@hxzhouh
hxzhouh / atomic_queue.go
Created February 29, 2024 12:34
An atomic implementation of a lock-free queue.
package main
import (
"sync/atomic"
"unsafe"
)
// lock-free queue
type LKQueue struct {
head unsafe.Pointer
@hxzhouh
hxzhouh / count.go
Created February 29, 2024 13:04
defer benchmark test
func sum(max int) int {
total := 0
for i := 0; i < max; i++ {
total += i
}
return total
}
func fooWithDefer() { defer func() { sum(10) }() }
func fooWithoutDefer() { sum(10) }
func BenchmarkFooWithDefer(b *testing.B) {
@hxzhouh
hxzhouh / main_test.go
Created May 19, 2024 13:01
grpc client。 has crt
func Test_server_SayHello(t *testing.T) {
certificate, err := tls.LoadX509KeyPair("./keys/client.crt", "./keys/client.key")
if err != nil {
log.Fatalf("Failed to load client key pair, %v", err)
}
certPool := x509.NewCertPool()
ca, err := os.ReadFile("./keys/ca.crt")
if err != nil {
log.Fatalf("Failed to read %s, error: %v", "./keys/ca.crt", err)
@hxzhouh
hxzhouh / main.go
Created May 19, 2024 13:02
grpc server has crt
func main() {
certificate, err := tls.LoadX509KeyPair("./keys/server.crt", "./keys/server.key")
if err != nil {
log.Fatalf("Failed to load key pair: %v", err)
}
certPool := x509.NewCertPool()
ca, err := os.ReadFile("./keys/ca.crt")
if err != nil {
log.Fatalf("Failed to read ca: %v", err)
@hxzhouh
hxzhouh / clone_test.go
Created June 4, 2024 02:19
compare unique
type testStruct2 struct {
c1 string
c2 string
c3 testStruct3
}
type testStruct3 struct {
c31 string
c32 string
}
type testStructData struct {
@hxzhouh
hxzhouh / bussiness_test.go
Created June 4, 2024 02:32
unique string
package huizhou92
import (
"testing"
"unique"
)
var Token string
var tokenUnique unique.Handle[string]
package main
import (
"golang.org/x/sync/singleflight"
"log"
"time"
)
type User struct {
Name string
@hxzhouh
hxzhouh / utils.go
Created September 2, 2024 03:14
RetryOperation
func RetryOperation(operation func() (int64, error), errMsgPrefix string, retryCount int64) error {
localRetryCount := retryCount
for {
t3 := time.Now()
cnt, err := operation()
if err == nil {
logger.Infof(`%s Success Num: %d`, errMsgPrefix, cnt)
} else {
logger.Errorf(`%s err:%+v`, errMsgPrefix, err)
}
@hxzhouh
hxzhouh / http.go
Created September 5, 2024 01:33
http request body leak
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"time"
)