forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkErrorSimulator.cpp
41 lines (35 loc) · 1.1 KB
/
NetworkErrorSimulator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <stdlib.h>
#include <glog/logging.h>
#include <sys/socket.h>
#include <thread>
#include <folly/Random.h>
#include "WdtOptions.h"
namespace facebook {
namespace wdt {
const static int kSimulatorSleepDurationMillis = 250;
void simulateNetworkError();
static std::thread errorSimulatorThread(&simulateNetworkError);
void simulateNetworkError() {
errorSimulatorThread.detach();
while (true) {
usleep(kSimulatorSleepDurationMillis * 1000);
auto &options = facebook::wdt::WdtOptions::getMutable();
int fd = 3 + folly::Random::rand32(2 * WdtOptions::get().num_ports + 1);
// close the chosen socket
if (shutdown(fd, SHUT_WR) < 0) {
PLOG(WARNING) << "socket shutdown failed for fd " << fd;
} else {
LOG(INFO) << "successfully shut down socket for fd " << fd;
}
}
}
}
}