2016-11-04 1 views
0

사용자가 URL에 도달했을 때 응답을 제공하기 위해 http.ListenAndServe을 사용하는 경우 함수의 해당 동작을 goroutine으로 실행해야합니까? 예를 들어http.ListenAndServe에서 goroutines을 사용해야합니까?

, 나는 /에서 듣고있어 말 : 그것은 사실 후에 오는 잠재적 인 요구를 느리게하지 않도록

func main() { 
    http.HandleFunc("/", provideMainContent) 
} 

func provideMainContent(w http.ResponseWriter, r *http.Request) { 
    /// Bunch of code, looks up details in databases, parses, then returns 
} 

provideMainContent 코드의 무리는 goroutine에 싸여해야 하는가?

+2

ListenAndServe() 메서드를 사용하면 처리 할 필요가 없습니다. 각 처리기가 다른 goroutine에서 실행된다는 것을 기억하면됩니다. –

답변

6

짧은 대답, http.Serve에서 아무

GoDoc :

Serve accepts incoming HTTP connections on the listener l, creating a new service goroutine for each. The service goroutines read requests and then call handler to reply to them.

그러나 @Mellow Marmot에 의해 연결 질문에서 언급 한 바와 같이, 당신은 몇 가지 할 수있는 goroutine를 생성 할 수 있습니다 경우가있을 수 있습니다 요청자가 응답을 얻기 위해 모든 처리를 기다릴 필요가 없도록 핸들러에서 리턴 할 때 처리합니다.