기본 콘텐츠로 건너뛰기

socketcluster tutorial - 7. 실패 처리 (Handling failure)

실패 처리


프로덕션의 실시스템은 시스템 실패를 처리 할 수 있어야합니다. 고려해야 할 가장 일반적인 실패 원인 중 하나는 연결 불량에 대한 가능성입니다.

SC의 이벤트 관련 메소드는 서버가 액션을 확인할 때 '콜백'함수를 최종 인수로 사용합니다. 메서드가 실패하면 콜백은 인수로 오류를 받습니다.이 경우 메서드 호출을 다시 시도하거나 사용자에게 메시지를 표시하여 문제를 알릴 수 있습니다.

다양한 오류를 처리하는 방법은 다음과 같습니다. (샘플 코드)

Client측

보내기

// Client code
socket.emit('ping', 'This is a ping', function (err) {
  if (err) {
    // 보내기 이벤트 실패, 재시도 혹은 사용자에게 계속할지 여부를 물어봄.
  } else {
    // 보내기 이벤트 성공
  }
});
콜백 인수를 전달하지 않으면 emit 메소드가 실패하지 않습니다. 이 경우 emit 호출은 이벤트를 중요도 낮음 (휘발성)으로 처리합니다. 이 기능은 초당 여러 번 이벤트를 내보내고 가끔 실패하는 경우 신경 쓰지 않는 경우에 유용합니다. emit 메소드에 콜백을 전달하면 상대에게 명시적으로 응답을 보내야 합니다. (그렇지 않으면 콜백이 시간 초과됩니다):
// Server code
socket.on('ping', function (data, res) {
  // ...
  if (success) {
    res(null, 'Success'); // 성공 메시지 돌려주기
  } else {
    res('Error message'); // 오류 반환
  }
});
res 함수의 첫 번째 인수는 오류(모든 JSON 호환 타입 또는 객체)입니다. 일반 메시지(오류 없음)를 보낼  경우 첫 번째 인수를 null로 해야합니다. 원하는 경우: res (123, 'This is the error message') 이와 같이 첫 번째 인수를 오류 코드로 사용할 수 있습니다.

발행하기

// Client code
socket.publish('pong', 'This is a pong', function (err) {
  if (err) {
    // 발행 실패, 재시도 혹은 사용자에게 계속할지 여부를 물어봄.
  } else {
    // 발행 성공
  }
});

구독하기

// Client code
var pongChannel = socket.subscribe('pong');

pongChannel.on('subscribeFail', function (err, channelName) {
  // 구독 실패 처리
});

pongChannel.on('subscribe', function (channelName) {
  // 구독 성공 처리
});

// pong 채널에서 발행한 데이터 다루기
pongChannel.watch(function (data) {
  // pong 채널이 데이터를 발행할 때 마다 실행합니다.
});

Server-side

보내기

// Server code
socket.emit('ping', 'This is a ping', function (err) {
  if (err) {
    // Failed to emit event, retry or log?
  } else {
    // Event was emitted successfully
  }
});
콜백을 제공하면 클라이언트에서 응답을 보내야합니다:
// Client code
socket.on('ping', function (data, res) {
  // ...
  if (success) {
    res(null, 'Success'); // 성공 메시지 돌려주기
  } else {
    res('Error message'); // 오류 반환
  }
});

발행하기

// Server code
socket.exchange.publish('pong', 'This is a pong', function (err) {
  if (err) {
    // 발행 이벤트 실패. 재시도 혹은 로그?
  } else {
    // 이벤트 성공
  }
});

댓글

이 블로그의 인기 게시물

세상 간단한 https(+secured websocket): Caddy

nginx, apache2 같은 걸로 매번 certbot 연동을 통해 https 하는게 지겨워서 알아보니 Caddy라는게 있더라. 설치법은 강하게 크기 위해 알아서 해본다. # cat /etc/caddy/Caddyfile your.shitty.site:8123 {   proxy / localhost:9123 {     websocket     transparent   } } 이건 외부에서 8123으로 들어오는 걸 내부적으로 9123로 맞춰서 https 맞춰주는 reverse proxy. 만일 port를 생략하면 # cat /etc/caddy/Caddyfile your.shitty.site {   proxy / localhost:9123 {     websocket     transparent   } } 기본포트인 443으로 되어 https://your.shitty.site 로 접속이 된다. 요새 말썽이던 cloudflare 버리고 netlify domain이랑 caddy를 쓰니까 앓던 이가 빠진 느낌. 이 조합은 당분간 계속 써야겠다 싶다.

저지연 오디오 생성 LLVC 사용기

https://github.com/KoeAI/LLVC 써봄 CUDA 11.8 버전 쓰고 있어 pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 VC 없어서 설치 https://wikidocs.net/168848 참조 FAIRSEQ 관련 오류 https://github.com/facebookresearch/fairseq/issues/2908 pip install editdistance  설치 No CUDA runtime is found, using CUDA_HOME='C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8' 오류 멀쩡하게 있는데 안됨. pip install torch==2.1.0+cu118 torchvision==0.16.0+cu118 torchaudio==2.1.0+cu118 --extra-index-url https://download.pytorch.org/whl/cu118 다시 설치 set DISTUTILS_USE_SDK=1 이게 문젠가 설정하고 재도전 Using cached ruamel.yaml.clib-0.2.8-cp311-cp311-win_amd64.whl (118 kB) Building wheels for collected packages: fairseq   Building wheel for fairseq (pyproject.toml) ... error   error: subprocess-exited-with-error   × Building wheel for fairseq (pyproject.toml) did not run successfully.   │ exit code: 1   ╰─> [1829 lines of output]       No CUDA runtime is found, usi...