1. 程式人生 > >Thrift第四課 連接中斷異常處理

Thrift第四課 連接中斷異常處理

while循環 copy app cal 客戶端 pan all per 機制

場景

Thrift框架采用了異常處理機制,當客戶端異常斷開連接,服務端這個時候嘗試發送數據給客戶端,Thrift庫會拋出異常,導致進程中斷。這種情況是非常正常的,服務器端應該捕獲異常的發生,但是不應該異常退出。所以應該當前發送數據失敗,直接返回

修改代碼如下:

uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) {

if (socket_ == -1) {

return -1;

throw TTransportException(TTransportException::NOT_OPEN, "Called write on non-open socket");

}


uint32_t sent = 0;


int flags = 0;

#ifdef MSG_NOSIGNAL

// Note the use of MSG_NOSIGNAL to suppress SIGPIPE errors, instead we

// check for the EPIPE return condition and close the socket in that case

flags |= MSG_NOSIGNAL;

#endif // ifdef MSG_NOSIGNAL


int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags);

++g_socket_syscalls;


if (b < 0) {

if (errno == EWOULDBLOCK || errno == EAGAIN) {

return 0;

}

// Fail on a send error

int errno_copy = errno;

GlobalOutput.perror("TSocket::write_partial() send() " + getSocketInfo(), errno_copy);


if (errno_copy == EPIPE || errno_copy == ECONNRESET || errno_copy == ENOTCONN) {

//修改的第一個地方,直接返回-1,不拋出異常

close();

return -1;

//throw TTransportException(TTransportException::NOT_OPEN, "write() send()", errno_copy);

}

//修改的第二個地方,直接返回-1,不拋出異常

close();

return -1;

throw TTransportException(TTransportException::UNKNOWN, "write() send()", errno_copy);

}


// Fail on blocked send

if (b == 0) {

throw TTransportException(TTransportException::NOT_OPEN, "Socket send returned 0.");

}

return b;

}


如下是新的嘗試,嘗試在最外層進行異常的捕獲

void TSocket::write(const uint8_t* buf, uint32_t len) {

uint32_t sent = 0;


while (sent < len) {

//嘗試通過try捕獲write_partial函數拋出的異常,但是在測試結果中並沒有什麽用,異常還是中斷程序

try

{

uint32_t b = write_partial(buf + sent, len - sent);

if (b == 0) {

// This should only happen if the timeout set with SO_SNDTIMEO expired.

// Raise an exception.

throw TTransportException(TTransportException::TIMED_OUT,

"send timeout expired");

}

sent += b;

}

catch (apache::thrift::transport::TTransportException* e)

{

return;

}

}

}



分析一下服務器發送數據的函數

void TSocket::write(const uint8_t* buf, uint32_t len) {

uint32_t sent = 0;


while (sent < len) {

uint32_t b = write_partial(buf + sent, len - sent);

if (b == 0) {

// This should only happen if the timeout set with SO_SNDTIMEO expired.

// Raise an exception.

throw TTransportException(TTransportException::TIMED_OUT,

"send timeout expired");

}

sent += b;

}

}

但b==0拋出異常,代表當前發送超時。while循環是為了循環發送,因為一次不一定發送完用戶數據,畢竟MTU的限制。註意sent是一個無符號整型,當b返回-1的時候,sent==0-1意味著將達到32位整數最大值,大於len,從而直接退出循環。因為套接字已經中斷,所以發送失敗,在調用write_partial函數的時候,返回b ==-1,導致退出循環,從而避免了拋出異常,因此返回-1,是非常合理的值


Thrift第四課 連接中斷異常處理