you are better than you think

备忘

last update:

同事有个模块会定期获取宿主机上的所有容器信息,代码中使用http client 请求docker engine获取容器信息。运行后收到用户反馈,程序导致系统fd泄漏,修复后代码如下,比原来代码多了一句 req.Close=true

    client := &http.Client{
        Timeout: time.Duration(timeout) * time.Millisecond,
        Transport: &http.Transport{
            Dial: unixDial,
        },
    }

    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        return nil, fmt.Errorf("NewRequest(): url = %s, err = %s", url, err)
    }

    req.Close = true // fd leak without setting this
    resp, err := client.Do(req)
    ...

因为我自己有个清理容器数据的代码也有类似逻辑。代码如下,但是这个清理容器的模块是运行一次就退出的(磁盘资源紧张时才运行) 所以没有这个问题。

  tr := &http.Transport{Dial: fakeDial, } 
    client := &http.Client{Transport: tr}
    resp, err := client.Get("http://localhost/containers/json")
    if err != nil {
        return nil, err
    }
    defer resp.Body.Close()

翻了下源码发现这段注释

    // Close indicates whether to close the connection after
    // replying to this request (for servers) or after sending this
    // request and reading its response (for clients).
    //
    // For server requests, the HTTP server handles this automatically
    // and this field is not needed by Handlers.
    //
    // For client requests, setting this field prevents re-use of
    // TCP connections between requests to the same hosts, as if
    // Transport.DisableKeepAlives were set.
    Close bool

背景:登录到relay需要输入token+密码, 然后选择中控机。

需求:k8s机房大概有X个了,master ip 记不住,这次要给自动登录脚本加一个展示机器列表的功能,选择ip 或hostname 的序号进行登录,登录其他node机器时 只需要直接gg hostname 即可。 gg prod 会展示 master 列表,输入 master对应的序号,回车即可登录到master.

花时间学习了tcl的语法 完成了这个脚本。

先设置线上机器的ssh 会话复用 ,这样每天只需要输入一次token ,其他时间不需要再输入token了

host *
    Protocol 2
    ServerAliveInterval 30
    ServerAliveCountMax 3
host xxxx.xxx.efg
    ControlMaster auto
    ControlPath ~/.ssh/master-%r@%h
host xxx.xxx.abc
    ControlMaster auto
    ControlPath ~/.ssh/master-%r@%h

golang邮件抄送

最近接触的一个api模块中使用了smtp.SendMail发送邮件。因为要实现cc功能,看了下源码,简单记录下。

smtp中关于SendMail的声明

SendMail(addr string, a Auth, from string, to []string, msg []byte) error

参数依次是,

    addr: smtp server地址,格式为hostname:port 或者 ip:port;
    a: smtp PlainAuth信息, 包含identity, username, password, host, 即身份id、用户名、密码、smtp服务器host. identify 一般为空,表明identity与username一致;
    from: 发件邮箱
    to: 收件人地址列表,包含to,cc,bcc的所有收件地址
    msg: 邮件内容,包含邮件header和body

来一个栗子