phpfile-ionfsphp-iniphp-socket

PHP file_get_contents timeout "local file" (NFS/mount)


I would like to know how to set a timeout on local file with file_get_content (not HTTP uri), Sometime NFS mount partition is very slow and i prefer to give up after a time out (5s for exemple),

file_get_contents("/mnt/photo/photo.jpg");

I read answers to other similar questions, but I guess the solutions work only with HTTP, and not for local files:

$ctx = stream_context_create(array('http'=>array('timeout' => 1200)));

I also suppose that this solution is not for me, it's also for Web, right?

ini_set('default_socket_timeout', 900);

Solution

  • I'm afraid, you can not control the NFS timeouts via PHP, as the NFS protocol options are controlled only via mount options. PHP has no idea whether a file is on NFS, or on a local hard drive. I believe, there are ways to determine the fact that a file is on NFS, but it won't give us control on the timeouts.

    There are two types of timeouts in NFS: minor and major. Minor timeout occurs, when no confirmation is received from the server within the timeout specified in mount options (timeo). Then operation is retried, and the timeout is doubled. A major timeout occurs, when the timeout interval reaches 60 seconds. What happens next depends on other options.

    By default, the original timeout is doubled, and the retries are continued endlessly. It is said that a volume is hard-mounted (hard option is on by default). If the volume is mounted with soft option, a major timeout will cause an I/O error. You should consider soft-mounting. However, note that the default timeout is only 7(0.7 seconds), which is likely a slightly too small for soft-mounting. You should estimate the possible waiting time, and adjust the timeo option accordingly.

    Another interesting option is intr, which is supposed to allow interruption of the pending NFS operations. However, this option is deprecated after kernel version 2.6.25. Only SIGKILL can interrupt an NFS operation. You may want to implement a watchdog process using the fork() function. The child process may run an NFS operation, and the parent may kill it after a certain period of time.