ghkdqhrbals / spring-chatting-server

In this project, we develop a chat server with automatic distribution, automatic scale in/out, and maintenance functions! For more information, please visit https://ghkdqhrbals.github.io/portfolios/docs/project
MIT License
41 stars 3 forks source link

get status code 409 by request multiple rest api #9

Closed ghkdqhrbals closed 1 year ago

ghkdqhrbals commented 1 year ago

While I test my chatting Server with following golang test codes, unintended error occurs which is response with 409 status code.

Here's my test code written in golang.

func worker(finished chan map[int]int, requestURL string, bodyReader *bytes.Reader, client http.Client, transferRatePerSecond int) {

    // 로컬 맵 생성
    m := make(map[int]int)

    for i := 0; i < transferRatePerSecond; i++ {
        // 요청 생성
        req, err := http.NewRequest(http.MethodPost, requestURL, bodyReader)
        if err != nil {
            fmt.Printf("client: could not create request: %s\n", err)
            os.Exit(1)
        }

        // json 헤더 설정
        req.Header.Set("Content-Type", "application/json")

        // 실제 요청 전송 및 반환
        res, _ := client.Do(req)

        // 로컬 맵에 삽입
        m[res.StatusCode] += 1
    }

    // 완료 후 채널로 로컬 맵 전송
    finished <- m
}

func main() {

    argsWithProg := os.Args
    argsWithoutProg := os.Args[1:]
    fmt.Println(argsWithProg)
    fmt.Println(argsWithoutProg)

    // http 전송 url
    requestURL := argsWithoutProg[0]

    // json 파일 경로 ex) user
    // .json 확장자 명은 제외
    jsonReq := argsWithoutProg[1]

    // 실행횟수 설정
    transferRatePerSecond, _ := strconv.Atoi(argsWithoutProg[2])

    // JSON 파일 읽기
    jsonFile, err := os.Open(jsonReq + ".json")
    if err != nil {
        fmt.Println(err)
    }
    defer jsonFile.Close()

    // 클라이언트 설정 및 timeout
    client := http.Client{
        Timeout: 30 * time.Second,
    }

    // json파일을 바이트로 변환
    jsonBody, _ := ioutil.ReadAll(jsonFile)
    bodyReader := bytes.NewReader(jsonBody)

    // 스레드 싱크 맵
    var contexts = sync.Map{}
    finished := make(chan map[int]int)

    // 멀티 스레드 http request
    go worker(finished, requestURL, bodyReader, client, transferRatePerSecond)

    // 스레드 별 채널을 통한 완료 확인 및 스레드의 맵 가져오기
    m := <-finished

    // 스레드 별 완료값 꺼내서 mutex lock해주는 맵 저장
    for k, v := range m {
        result, ok := contexts.Load(k)
        if ok {
            contexts.Store(k, result.(int)+v)
        } else {
            contexts.Store(k, v)
        }
    }

    // 성공한 http request 개수 확인
    contexts.Range(func(k, v interface{}) bool {
        fmt.Println("status: ", k, " count: ", v)
        return true
    })
}

And my user.json file looks like

{
    "userId":"a",
    "userName": "a",
    "email":"a@naver.com",
    "userPw":"1234"
}

And here's the console of mine when running testing codes.

gyuminhwangbo@Gyuminui-MacBookPro testing % go run main.go http://localhost:8080/auth/user user 50
[http://localhost:8080/auth/user user 50]
status:  409  count:  1 #<--- unIntended
status:  400  count:  49 #<--- Intended

This is my server log.

auth-server               | 2023-01-16 05:55:17.348  INFO 1 --- [nio-8085-exec-9] chatting.chat.web.UserController         : request URI=/auth/user
auth-server               | 2023-01-16 05:55:17.350 DEBUG 1 --- [nio-8085-exec-9] org.hibernate.SQL                        : 
auth-server               |     select
auth-server               |         user0_.user_id as user_id1_0_0_,
auth-server               |         user0_.email as email2_0_0_,
auth-server               |         user0_.join_date as join_dat3_0_0_,
auth-server               |         user0_.login_date as login_da4_0_0_,
auth-server               |         user0_.logout_date as logout_d5_0_0_,
auth-server               |         user0_.user_name as user_nam6_0_0_,
auth-server               |         user0_.user_pw as user_pw7_0_0_ 
auth-server               |     from
auth-server               |         user_table user0_ 
auth-server               |     where
auth-server               |         user0_.user_id=?
auth-server               | 2023-01-16 05:55:17.351 TRACE 1 --- [nio-8085-exec-9] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [a]
auth-server               | 2023-01-16 05:55:17.355 ERROR 1 --- [nio-8085-exec-9] c.chat.web.error.GlobalExceptionHandler  : handleCustomException throw CustomException : DUPLICATE_RESOURCE

# ------------------------------ UnIntended ------------------------------
nginx                     | 192.168.192.1 - - [16/Jan/2023:05:55:17 +0000] "POST /auth/user HTTP/1.1" 409 162 "-" "Go-http-client/1.1" "-"
# ------------------------------ UnIntended ------------------------------

nginx                     | 192.168.192.1 - - [16/Jan/2023:05:55:17 +0000] "POST /auth/user HTTP/1.1" 400 0 "-" "Go-http-client/1.1" "-"
nginx                     | 192.168.192.1 - - [16/Jan/2023:05:55:17 +0000] "POST /auth/user HTTP/1.1" 400 0 "-" "Go-http-client/1.1" "-"
nginx                     | 192.168.192.1 - - [16/Jan/2023:05:55:17 +0000] "POST /auth/user HTTP/1.1" 400 0 "-" "Go-http-client/1.1" "-"
nginx                     | 192.168.192.1 - - [16/Jan/2023:05:55:17 +0000] "POST /auth/user HTTP/1.1" 400 0 "-" "Go-http-client/1.1" "-"
ghkdqhrbals commented 1 year ago

This was Golang problem, not Java Spring