分类 golang 中的文章

go client-server模式: grpc

什么是grpc? grpc是google开发的一个Remote Procedure Call (RPC) framework。 生成TLS Certificate 生成ca(Certificate Authority)私钥和自签名证书 openssl req -x509 -newkey rsa:4096 -nodes -days 3650 -keyout ca-key.pem -out ca-cert.pem -subj "/C=TR/ST=ASIA/L=ISTANBUL/O=DEV/OU=TUTORIAL/CN=*.hack.com/emailAddress=hack@foxmail.com" 脚本返回结果: winsun@unbuntu64:~/test/grpc-tls-go/cert$ ./generator.sh [1] 删除所有pem文件 [2] 生成ca私钥和自签名证书 ....+.....+....+..+.+...+..+.............+......... ................................................... ----- winsun@unbuntu64:~/test/grpc-tls-go/cert$ ll total 20 drwxrwxr-x 2 winsun winsun……

阅读全文

go worker pool模式

worker pool 定义job及channel type WorkerJob struct { from int size int isConcept bool } jobCh := make(chan WorkerJob,100) 定义生产者 生成者负责创建job,并把job投入job channel func producer(jobs chan WorkerJob){ job := WorkerJob{from:1,size:10} jobs <- job } 创建worker pool func worker(jobs chan WorkerJob,results chan WorkerResult ,wg *sync.WaitGroup){ for job := range jobs{ out := WorkerResult{jobId:job.id, message: cosumer(job.from,job.size,job.isConcept)} results <- out } wg.Done() } func createWorkPool(numOfWorkers int,jobs chan WorkerJob,results chan WorkerResult){ var wg sync.WaitGroup for i := 0; i < numOfWorkers ; i++{ wg.Add(1) go worker(jobs,results,&wg) } wg.Wait() close(results) } 定义消费者 func cosumer(from,size int,isConcept bool)string{ //to do... return "success"……

阅读全文