我正在关闭这个函数吗?

huangapple go评论79阅读模式
英文:

Am I closing this function correctly?

问题

我在几个小时后一直收到一个错误,说我打开的文件太多。

我已经编辑了ulimit -n,但这似乎只是延长了错误发生并导致进程崩溃的时间。

我相信我已经将问题缩小到这个函数,只是不确定我是否正确关闭它。

我目前有一个函数

    if _, err := io.Copy(rw, stdout); err != nil {
        if !WritePipeBroken.MatchString(err.Error()) &&
            !ConnectionResetByPeer.MatchString(err.Error()) {
            rollbar.Error(rollbar.ERR, err)
        }
        log.Printf("pipeThruFfmpegToMp3: %v\n", err)
        if err := ffmpeg.Process.Kill(); err != nil {
            log.Printf("pipeThruFfmpegToMp3: %v\n", err)
        }
    }
    rw.Flush()
    wg.Done()
}()```

我认为它是挂起的,所以我应该这样做

```go func() {
    if _, err := io.Copy(rw, stdout); err != nil {
        if !WritePipeBroken.MatchString(err.Error()) &&
            !ConnectionResetByPeer.MatchString(err.Error()) {
            rollbar.Error(rollbar.ERR, err)
        }
        log.Printf("pipeThruFfmpegToMp3: %v\n", err)
        if err := ffmpeg.Process.Kill(); err != nil {
            log.Printf("pipeThruFfmpegToMp3: %v\n", err)
        }
    }
    if ffmpeg.Process != nil {
        ffmpeg.Process.Kill()
    }
    if stdout != nil {
        stdin.Close()
    }
    rw.Flush()
    wg.Done()
}()```

<details>
<summary>英文:</summary>

I keep getting an error after a few hours that I have too many open files.

I have already edited the ```ulimit -n``` but that only seems to prolong the time before the error occurs and crashes the process.

I believe I have narrowed it down to this function, I just am not sure if I am closing it right.

I currently have a function 



	go func() {
		if _, err := io.Copy(rw, stdout); err != nil {
			if !WritePipeBroken.MatchString(err.Error()) &amp;&amp;
				!ConnectionResetByPeer.MatchString(err.Error()) {
				rollbar.Error(rollbar.ERR, err)
			}
			log.Printf(&quot;pipeThruFfmpegToMp3: %v\n&quot;, err)
			if err := ffmpeg.Process.Kill(); err != nil {
				log.Printf(&quot;pipeThruFfmpegToMp3: %v\n&quot;, err)
			}
		}
		rw.Flush()
		wg.Done()
	}()


I believe that it is hanging, so should I do this instead


	go func() {
		if _, err := io.Copy(rw, stdout); err != nil {
			if !WritePipeBroken.MatchString(err.Error()) &amp;&amp;
				!ConnectionResetByPeer.MatchString(err.Error()) {
				rollbar.Error(rollbar.ERR, err)
			}
			log.Printf(&quot;pipeThruFfmpegToMp3: %v\n&quot;, err)
			if err := ffmpeg.Process.Kill(); err != nil {
				log.Printf(&quot;pipeThruFfmpegToMp3: %v\n&quot;, err)
			}
		}
		if ffmpeg.Process != nil {
			ffmpeg.Process.Kill()
		}
		if stdout != nil {
			stdin.Close()
		}
		rw.Flush()
		wg.Done()
	}()



</details>


# 答案1
**得分**: 1

使用`defer`语句,就像谷歌[建议][1]的那样:

```go
func CopyFile(dstName, srcName string) (written int64, err error) {
    src, err := os.Open(srcName)
    if err != nil {
        return
    }
    defer src.Close()

    dst, err := os.Create(dstName)
    if err != nil {
        return
    }
    defer dst.Close()

    return io.Copy(dst, src)
}
英文:

use defer statement like google advises:

func CopyFile(dstName, srcName string) (written int64, err error) {
src, err := os.Open(srcName)
if err != nil {
return
}
defer src.Close()
dst, err := os.Create(dstName)
if err != nil {
return
}
defer dst.Close()
return io.Copy(dst, src)
}

huangapple
  • 本文由 发表于 2016年3月1日 03:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/35707946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定