Files
7c676d20c3 [MM-67978] Add open file descriptor count to support packet diagnostics (#35834)
* MM-67978: Add open file descriptor count to support packet diagnostics

Add OpenFileDescriptors and MaxFileDescriptors fields to
SupportPacketDiagnostics.Server. On Linux the open count is read from
/proc/self/fd; on macOS from /dev/fd. Both platforms use
syscall.Getrlimit(RLIMIT_NOFILE) for the soft limit. Unsupported
platforms (e.g. Windows) return -1 for both fields.

* MM-67978: Move OpenFileDescriptors/MaxFileDescriptors after TotalMemoryMB

* fix: guard int64 overflow in getMaxFileDescriptors and update tests for unsupported platforms

Add overflow check before casting rlimit.Cur (uint64) to int64 in
getMaxFileDescriptors on Linux. Update fd_test.go and
support_packet_test.go assertions to accept -1 as a valid sentinel for
unsupported platforms instead of requiring a strictly positive value.

* revert: remove unrelated test refactoring from support_packet_test.go

Restore the three unrelated changes that crept into this branch:
- SetLogRootPathOverride instead of t.Setenv("MM_LOG_PATH", ...)
- installTypeOverride field instead of t.Setenv(envVarInstallType, ...)
- Restore explanatory comment in TestGetSanitizedConfigFile

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-04-21 09:54:32 +00:00

36 lines
958 B
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build linux
package platform
import (
"fmt"
"math"
"os"
"syscall"
)
// getOpenFileDescriptors returns the number of open file descriptors for the current process.
func getOpenFileDescriptors() (int64, error) {
entries, err := os.ReadDir("/proc/self/fd")
if err != nil {
return -1, err
}
// Subtract 1 because the ReadDir call itself opens a file descriptor that appears in the listing.
return max(int64(len(entries))-1, 0), nil
}
// getMaxFileDescriptors returns the soft file descriptor limit for the current process.
func getMaxFileDescriptors() (int64, error) {
var rlimit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
return -1, err
}
if rlimit.Cur > math.MaxInt64 {
return -1, fmt.Errorf("rlimit.Cur %d overflows int64", rlimit.Cur)
}
return int64(rlimit.Cur), nil
}