728x90
반응형

서버 장애가 나서 panic이 발생했다는 알람이 많이 왔었다.

 

원인은 데이터베이스가 다운돼 쿼리를 실행할 수 없어 발생한 장애였다.

 

코드를 확인해보니 에러를 확인하고 에러가 없는 경우에 Close 함수를 실행해야 했는데 

 

항상 Close 함수를 실행을 하여 rows가 nil 값인데도 참조하여 발생한 문제였다.

rows, err := db.Query(query, args)
if err != nil {
    return
}
defer rows.Close()
...

그래서 에러를 확인하고 Close 함수를 실행하도록 코드를 수정했다.

혹시 몰라 일정 시간 이상 응답이 없으면 에러가 발생하도록 하는 코드도 추가했다.

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
rows, err := db.QueryContext(ctx, query, args)
if err != nil {
    return
}
defer rows.Close()
...

 

참고 문헌

  1. https://golang.org/pkg/database/sql/#DB.ExecContext

  2. https://golang.org/pkg/database/sql/#Conn.QueryContext

  3. https://golang.org/pkg/database/sql/#DB.PingContext

반응형
728x90
반응형
  1. 테스트 타임아웃

Go로 테스트를 하는데 다음과 같은 에러가 발생하면서 종료됐다.

golang panic: test timed out after 10m0s

이유는 테스트를 수행할 때 기본으로 10분으로 타임아웃이 걸려있었기 때문이었다.

$ go test -timeout 1h

위와 같이 타임아웃 시간을 늘려서 수행하면 된다.

  1. 410 gone

이전에 Go 버전을 올리고 410 Gone 에러가 나서 환경변수를 설정했었는데

 

go get으로 다운로드가 안되는 경우가 발생했다.

 

비공개 저장소에서 받는 경우 발생하는 문제로

 

다음과 같이 설정하면 정상적으로 다운로드받는 것을 확인할 수 있다.

$ export GOPRIVATE=private_repo_url
  1. 모듈 버전 업 문제

모듈의 버전은 v1에서 v2로 변경해서 다운로드받으려는 경우 다음과 같이 에러가 발생했다.

go get github.com/gomodule/module@v2.0.0: github.com/gomodule/module@v2.0.0: invalid version: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2

다음과 같이 go.mod 파일을 수정하면 다운로드받을 수 있다.

module github.com/gomodule/module/v2

 

참고 문헌

  1. https://github.com/golang/go/issues/25886

  2. https://golang.org/cmd/go/#hdr-Module_configuration_for_non_public_modules

  3. https://github.com/golang/go/issues/35732

반응형

'Golang' 카테고리의 다른 글

[Golang] Go의 선(The Zen of Go)  (0) 2020.06.04
[Golang] Kafka 연동 문제  (0) 2020.04.30
[Golang] Java gzip migration  (0) 2020.01.30
[Golang] 410 Gone  (0) 2020.01.25
[Golang] Echo 415 에러 해결  (0) 2020.01.25

+ Recent posts