Skip to content

Instantly share code, notes, and snippets.

View JesseYan's full-sized avatar
🏠
Working from home

晏佳益 JesseYan

🏠
Working from home
View GitHub Profile
@JesseYan
JesseYan / async_pool.go
Created April 1, 2020 03:15
create pool using gorutines,and consumes in async
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup
@JesseYan
JesseYan / pool.go
Created April 1, 2020 02:48
create pool using gorutines
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int, 4)
go write(ch)
@JesseYan
JesseYan / proto-oneof-repeated.md
Last active February 12, 2020 08:27
fix oneof proto intems in repeated accures

proto define

message Sheet {
	string sheet_name=1;
	repeated Line lines=2;
}

message Line {
	repeated Cell cells=1;
}
@JesseYan
JesseYan / timeout_test.go
Last active December 14, 2019 08:41
自己实现goroutine超时控制,解决grpc超时在cloud不生效
package goroutinetimeout
import (
"context"
"encoding/json"
"fmt"
"testing"
"time"
"proto/bi/service/olap/service"
@JesseYan
JesseYan / spectre.go
Last active August 8, 2019 10:19
aws s3 service, store object
func spectreCase() error {
host := os.Getenv("SPECTRE_HOST")
key := os.Getenv("SPECTRE_KEY")
secret := os.Getenv("SPECTRE_SECRET")
if host == "" || key == "" || secret == "" {
fmt.Println("Error 环境变量SPECTRE_HOST、SPECTRE_KEY、SPECTRE_SECRET均不能为空")
}
var bucketName = "venus-table-bucket-test"
//var bucketName = "venus-table-bucket" //online
@JesseYan
JesseYan / email.go
Created August 8, 2019 10:16
go send email
//SendEmail 发送邮件
func SendEmail(conf *EmailConfig) error {
m := gomail.NewMessage()
m.SetHeader("From", conf.From)
m.SetHeader("To", conf.To...)
m.SetHeader("Subject", conf.Subject)
if conf.ContentType == EmailTextType {
m.SetBody("text/plain", conf.ContentBuffer.String())
} else if conf.ContentType == EmailHTMLType {
m.SetBody("text/html", conf.ContentBuffer.String())
@JesseYan
JesseYan / 指针赋值.md
Last active September 26, 2018 11:01
QueryOne 返回的是指针类型
// UpsertByDwIndicatorID 创建或者更新,返回最新的数据
func UpsertByDwIndicatorID(d *model.BiIndicator) error {
	var err error
	if nil == d || 0 == d.DwIndicatorID {
		return errors.New("UpsertByDwIndicatorID error: BiIndicator nil or DwIndicatorID value is empty")
	}

	if _, err := QueryOne([]basemodel.Pair{
		{K: "dw_indicator_id", V: d.DwIndicatorID},
数据库相关查询优化
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:
select id from t where num=0
3.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描。
4.应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num=10 or num=20
@JesseYan
JesseYan / defer_panic.md
Last active August 24, 2018 08:26
defer和panic的用法,注意点

defer和panic怎么合用?

if v.IndicatorType == IndicatorTypeCalc {
	func() {
		defer func() {
			if err := recover(); nil != err {
				fmt.Printf("\n$$$$$$ indicator [%v](data_model_id:[%v]) analyze failed:%v", v.Name, v.DataModelID, err)
			}
 }()
@JesseYan
JesseYan / 进程、线程、协程、goroutine区别.md
Last active August 13, 2018 07:45
进程、线程(内核级线程)、协程(用户级线程)

goroutine 和协程的区别?

备注:需要区分进程、线程(内核级线程)、协程(用户级线程)三个概念。

    1. 进程、线程、协程概念性区别 对于进程、线程,都是有内核进行调度,有CPU时间片的概念,进行抢占式调度(有多种调度算法)。

对于协程(用户级线程),这是对内核透明的,也就是系统并不知道有协程的存在,是完全由用户的程序自己调度的,因为是由用户程序自己控制,那么就很难像抢占式调度那样做到强制的CPU控制权切换到其他进程/线程,通常只能进行协作式调度,需要协程自己主动把控制权转让出去之后,其他协程才能被执行到。