go

MySQL的MaxIdleConns不合理,会变成短连接

北战南征 提交于 2021-01-29 04:34:44
1 背景 最近石墨文档线上业务出现了一些性能问题,在突发流量情况下,有个业务性能急剧下降。该服务是依赖于数据库的业务,会批量获取数据库里的数据。在经过一系列的排查过程后,发现该服务到数据库的连接数经常超过MaxIdleConns,因此怀疑是数据库的配置导致的性能问题,所以以下针对数据库的代码进行了剖析,并做了相关实验。 2 配置解读 maxIdleCount int // zero means defaultMaxIdleConns; negative means 0 maxOpen int // <= 0 means unlimited maxLifetime time.Duration // maximum amount of time a connection may be reused maxIdleTime time.Duration // maximum amount of time a connection may be idle before being closed 可以看到以上四个配置,是我们Go MySQL客户端最重要的配置。 maxIdleCount 最大空闲连接数,默认不配置,是2个最大空闲连接 maxOpen 最大连接数,默认不配置,是不限制最大连接数 maxLifetime 连接最大存活时间 maxIdleTime 空闲连接最大存活时间 3 源码解析

为了梦想,从一而终

孤街醉人 提交于 2021-01-29 04:29:29
If you want to follow your dreams, you have to say no to all the alternatives Our brains behave like a beachball filled with bees. Hundreds of conflicting impulses, pushing us in different directions. People never want to do one thing. We want to do all the things. We simultaneously want to exercise and to learn Spanish and to go out for pizza. Our desires are countless, independent agents, working to nudge our beachball in their own selfish direction. And so usually, that ball is going nowhere. It’s controlled more by the terrain than by the will of what’s inside it. This is how most people

聊聊cortex的kv.Client

时光总嘲笑我的痴心妄想 提交于 2021-01-29 04:22:39
序 本文主要研究一下cortex的kv.Client kv.Client github.com/cortexproject/cortex/pkg/ring/kv/client.go // Client is a high-level client for key-value stores (such as Etcd and // Consul) that exposes operations such as CAS and Watch which take callbacks. // It also deals with serialisation by using a Codec and having a instance of // the the desired type passed in to methods ala json.Unmarshal. type Client interface { // List returns a list of keys under the given prefix. Returned keys will // include the prefix. List(ctx context.Context, prefix string) ([]string, error) // Get a specific key. Will use a

How to skip a lot of values when define const variable with iota?

六眼飞鱼酱① 提交于 2021-01-29 04:03:32
问题 Say I have next c program: #include <stdio.h> int main(int args, char* argv[]) { enum RC { APPLE=0, ORANGE, PEAR, BANANA=99, GRAPE }; printf("%d, %d, %d, %d, %d\n", APPLE, ORANGE, PEAR, BANANA, GRAPE); } The output is: 0, 1, 2, 99, 100 If in go, how can I use a more golang way to handle that? In fact, if I just want to skip some value. e.g. print 0, 1, 2, 5, 6 , then I can use next to skip some value, but here I need to skip 96 values... package main import "fmt" func main() { const ( APPLE =

Why does the HTTP Client Force an Accept-Encoding header

强颜欢笑 提交于 2021-01-28 20:58:00
问题 Sample Code: package main import ( "fmt" "net/http" "net/http/httputil" ) func main() { client := &http.Client{ Transport: &http.Transport{ DisableCompression: true, }, } url := "https://google.com" req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return } //req.Header.Set("Accept-Encoding", "*") //req.Header.Del("Accept-Encoding") requestDump, err := httputil.DumpRequestOut(req, false) if err != nil { fmt.Println(err) } fmt.Println(string(requestDump)) client.Do(req) }

Multiple goroutines access/modify a list/map

前提是你 提交于 2021-01-28 20:10:52
问题 I am trying to implement a multithreaded crawler using a go lang as a sample task to learn the language. It supposed to scan pages, follow links and save them do DB. To avoid duplicates I'm trying to use map where I save all the URLs I've already saved. The synchronous version works fine, but I have troubles when I'm trying to use goroutines. I'm trying to use mutex as a sync object for map, and channel as a way to coordinate goroutines. But obviously I don't have clear understanding of them.

Gorm - Has One relation with anonymous field

岁酱吖の 提交于 2021-01-28 19:32:40
问题 I use Golang and GORM. I have a User structure which has one Association . type User struct { ID int ... } type Association struct { ID int UserID int } I also have an AssoUser structure, which is composed of a anonymous field User , and has a pointer to Assocation . type AssoUser struct { User Asso *Association } When I run var assoUser AssoUser assoUser.Asso = &Association{ Name : "asso_name", ... } assoUser.Name = "user_name" ... // filling the struct db.Debug().Create(&assoUser) I expect

How to modify the value of a simple type through pointer receiver method in Go?

ぐ巨炮叔叔 提交于 2021-01-28 19:20:29
问题 I wanted to have a custom type based on a basic type and be able to set its value by calling a pointer receiver. When I run the following program: package main import ( "fmt" "strconv" ) type FooInt int func (fi *FooInt) FromString(i string) { num, _ := strconv.Atoi(i) tmp := FooInt(num) fi = &tmp } func main() { var fi *FooInt fi.FromString("5") fmt.Printf("%v\n", fi) } I receive <nil> . Why doesn't the pointer declared in main() change its value to the address of tmp ? Here is a Go

Calling “sed” from exec.Command

非 Y 不嫁゛ 提交于 2021-01-28 17:58:12
问题 I'm currently having trouble trying to run this code which is supposed to call the unix command sed to find and replace the string hello with goodbye in the file ./myfile.txt This works fine if you run it from the command line, but if I try the same thing from my Go code.... command := exec.Command("sed", "-e \"s/hello/goodbye/g\" ./myfile.txt") result,err := command.CombinedOutput() fmt.Println(string(result)) Bit I just keep on getting this output sed: -e expression #1, char 2: unknown

Calling “sed” from exec.Command

被刻印的时光 ゝ 提交于 2021-01-28 17:46:55
问题 I'm currently having trouble trying to run this code which is supposed to call the unix command sed to find and replace the string hello with goodbye in the file ./myfile.txt This works fine if you run it from the command line, but if I try the same thing from my Go code.... command := exec.Command("sed", "-e \"s/hello/goodbye/g\" ./myfile.txt") result,err := command.CombinedOutput() fmt.Println(string(result)) Bit I just keep on getting this output sed: -e expression #1, char 2: unknown