Bytecode Manipulation - Javassist
概要 Unlike other similar bytecode editors, Javassist provides two levels of API: source level and bytecode level. 生成字节码1234567891011121314151617package me.zhongmingmao.javassist;public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public void move(int x, int y) { this.x = x; this.y = y; }} 12$ javac me/zhongmingmao/javassist/Point.java$ javap -v me.zhongmingmao.javassist.Point 123456789101112131415161...
Bytecode Manipulation - Java Agent Practice
概念Instrument Instrument 是 JVM 提供的一个可以修改已加载类的类库,依赖于 JVMTI 的 Attach API 机制 要使用 Instrument 的类修改功能,需要实现 java.lang.instrument.ClassFileTransformer 接口 可以在 ClassFileTransformer#transform 中利用 ASM 或者 Byte Buddy 等工具对字节码进行操作 Instrument 通过与 Java Agent 结合来注入到 JVM 中 JVMTI & Agent JPDA(Java Platform Debugger Architecture)是 JDK 标准,必须实现 如果 JVM 启动时开启了 JPDA,那么类是允许被重新加载的 已加载的旧版类信息被卸载,然后重新加载新版本的类 JVMTI 是 JVM 提供的一套对 JVM 进行操作的工具接口,Agent 是 JVMTI 的一种实现 Attach API 的作用:提供 JVM 进程间通信的能力 Attach 后,目标 JVM 在运行时走到 Agent 中定义的 age...
Cloud Native Foundation - Go Thread Scheduling
进程 & 线程 进程:资源分配的基本单位 线程:调度的基本单位 在内核视角,线程与进程无本质差别,在 Linux 中都是以**task_struct**进行描述 glibc 中的 pthread 库提供了 Native POSIX Thread Library 支持 Native POSIX Thread Library (NPTL) is an implementation of the POSIX Threads specification for the Linux operating system. 子进程通过 fork 的方式产生,基于父子关系形成进程树,Linux 中的初始进程一般为systemd 12$ pmap 11: /lib/systemd/systemd --system --deserialize 37 Linux 进程内存 虚拟地址 https://ggirjau.com/text-data-bss-heap-stack-and-where-in-memory-are-stored-variables-of-c-program/ Segme...
Cloud Native Foundation - Go Sync
线程安全 锁 Go 不仅提供了基于 CSP 的通讯模型,也支持基于共享内存的多线程数据访问 sync 包提供了锁的基本原语 同步工具 用途 sync.Mutex 互斥锁:Lock加锁,Unlock解锁 sync.RWMutex 读写分离锁:不限制并发读,只限制并发写和并发读写 sync.WaitGroup 等待一组 goroutine 返回,类似于 Java 的 CountDownLatch sync.Once 保证某段代码只执行1次,典型场景:单例模式 sync.Cond 让一组 goroutine 在满足特定条件时被唤醒,典型场景:生产者消费者模型 Mutex12345678910111213141516171819202122232425262728293031323334353637func unsafeWrite() { // fatal error: concurrent map writes conflictMap := map[int]int{} for i := 0; i < 100; i++ ...
Cloud Native Foundation - Go Features
特点 可以高效编译、支持高并发、面向垃圾回收 秒级完成大型程序的单节点编译 依赖管理清晰 不支持继承 支持垃圾回收、支持并发执行、支持多线程通讯 对多核计算机支持友好 特性来源 环境变量 Env Desc Value GOARCH The architecture, or processor, for which to compile code. amd64 GOBIN The directory where ‘go install’ will install a command. GOCACHE The directory where the go command will store cached information for reuse in future builds. ~/Library/Caches/go-build GOMODCACHE The directory where the go command will store downloaded modules. ~/go/pkg/mod ...
Go Paradigm - Visitor
概述 Visitor 模式是将算法和结构分离的一种方法 在不修改结构的情况下,向现有对象结构添加新操作,遵循 OCP 不同 Visitor 用来访问同一个数据结构的不同部分 示例 1 类似于 Java java.util.function.Consumer 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253type Visitor func(shape Shape)type Shape interface { accept(Visitor)}type Circle struct { Radius int}func (c *Circle) accept(v Visitor) { v(c)}type Rectangle struct { Width, Height int}func (r *Rectangle) accept(v Visitor) ...
Go Paradigm - Pipeline
HTTP1234567891011121314151617181920212223242526272829303132333435363738type HttpHandlerDecorator func(http.HandlerFunc) http.HandlerFuncfunc Handler(h http.HandlerFunc, decors ...HttpHandlerDecorator) http.HandlerFunc { for i := range decors { h = decors[len(decors)-1-i](h) } return h}func WithServerHeader(h http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { log.Println("--->WithServerHeader") ...
Go Paradigm - Decorator
示例示例 11234567891011121314151617181920func decorator(f func(s string)) func(s string) { return func(s string) { fmt.Println("Started") f(s) fmt.Println("Done") }}func Hello(s string) { fmt.Println(s)}func main() { decorator(Hello)("Hello, World!")}// output:// Started// Hello, World!// Done 示例 2123456789101112131415161718192021222324252627type SumFunc func(int64, int64) int64func getFuncti...
Go Paradigm - Generation
代码生成 Go 的代码生成主要用来解决泛型编程的问题 泛型编程:类型与算法解耦 类型检查 Go 在支持泛型之前,只能用 interface{},必然涉及到类型检查 Type Assertion1234567891011121314151617181920212223type Container []interface{}func (c *Container) Put(elem interface{}) { *c = append(*c, elem)}func (c *Container) Get() interface{} { elem := (*c)[0] *c = (*c)[1:] return elem}func main() { c := &Container{} c.Put(1) c.Put("hello") elem, ok := c.Get().(int) /...
Go Paradigm - MapReduce
IntroductionMap1234567891011121314151617181920212223242526272829func MapStrToStr(arr []string, fn func(s string) string) []string { var sl []string for _, s := range arr { sl = append(sl, fn(s)) } return sl}func MapStrToInt(arr []string, fn func(s string) int) []int { var sl []int for _, s := range arr { sl = append(sl, fn(s)) } return sl}func main() { list := []string{"hello", "world"} ...















