mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-06-28 10:00:34 +00:00
acc333c40b
When GTS is running in a container runtime which has configured CPU or memory limits or under an init system that uses cgroups to impose CPU and memory limits the values the Go runtime sees for GOMAXPROCS and GOMEMLIMIT are still based on the host resources, not the cgroup. At least for the throttling middlewares which use GOMAXPROCS to configure their queue size, this can result in GTS running with values too big compared to the resources that will actuall be available to it. This introduces 2 dependencies which can pick up resource contraints from the current cgroup and tune the Go runtime accordingly. This should result in the different queues being appropriately sized and in general more predictable performance. These dependencies are a no-op on non-Linux systems or if running in a cgroup that doesn't set a limit on CPU or memory. The automatic tuning of GOMEMLIMIT can be disabled by either explicitly setting GOMEMLIMIT yourself or by setting AUTOMEMLIMIT=off. The automatic tuning of GOMAXPROCS can similarly be counteracted by setting GOMAXPROCS yourself.
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
// The UnixCredentials system call is currently only implemented on Linux
|
|
// http://golang.org/src/pkg/syscall/sockcmsg_linux.go
|
|
// https://golang.org/s/go1.4-syscall
|
|
// http://code.google.com/p/go/source/browse/unix/sockcmsg_linux.go?repo=sys
|
|
|
|
// Local implementation of the UnixCredentials system call for FreeBSD
|
|
|
|
package dbus
|
|
|
|
/*
|
|
const int sizeofPtr = sizeof(void*);
|
|
#define _WANT_UCRED
|
|
#include <sys/types.h>
|
|
#include <sys/ucred.h>
|
|
*/
|
|
import "C"
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"syscall"
|
|
"unsafe"
|
|
)
|
|
|
|
// http://golang.org/src/pkg/syscall/ztypes_linux_amd64.go
|
|
// https://golang.org/src/syscall/ztypes_freebsd_amd64.go
|
|
type Ucred struct {
|
|
Pid int32
|
|
Uid uint32
|
|
Gid uint32
|
|
}
|
|
|
|
// http://golang.org/src/pkg/syscall/types_linux.go
|
|
// https://golang.org/src/syscall/types_freebsd.go
|
|
// https://github.com/freebsd/freebsd/blob/master/sys/sys/ucred.h
|
|
const (
|
|
SizeofUcred = C.sizeof_struct_ucred
|
|
)
|
|
|
|
// http://golang.org/src/pkg/syscall/sockcmsg_unix.go
|
|
func cmsgAlignOf(salen int) int {
|
|
salign := C.sizeofPtr
|
|
|
|
return (salen + salign - 1) & ^(salign - 1)
|
|
}
|
|
|
|
// http://golang.org/src/pkg/syscall/sockcmsg_unix.go
|
|
func cmsgData(h *syscall.Cmsghdr) unsafe.Pointer {
|
|
return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(syscall.SizeofCmsghdr)))
|
|
}
|
|
|
|
// http://golang.org/src/pkg/syscall/sockcmsg_linux.go
|
|
// UnixCredentials encodes credentials into a socket control message
|
|
// for sending to another process. This can be used for
|
|
// authentication.
|
|
func UnixCredentials(ucred *Ucred) []byte {
|
|
b := make([]byte, syscall.CmsgSpace(SizeofUcred))
|
|
h := (*syscall.Cmsghdr)(unsafe.Pointer(&b[0]))
|
|
h.Level = syscall.SOL_SOCKET
|
|
h.Type = syscall.SCM_CREDS
|
|
h.SetLen(syscall.CmsgLen(SizeofUcred))
|
|
*((*Ucred)(cmsgData(h))) = *ucred
|
|
return b
|
|
}
|
|
|
|
// http://golang.org/src/pkg/syscall/sockcmsg_linux.go
|
|
// ParseUnixCredentials decodes a socket control message that contains
|
|
// credentials in a Ucred structure. To receive such a message, the
|
|
// SO_PASSCRED option must be enabled on the socket.
|
|
func ParseUnixCredentials(m *syscall.SocketControlMessage) (*Ucred, error) {
|
|
if m.Header.Level != syscall.SOL_SOCKET {
|
|
return nil, syscall.EINVAL
|
|
}
|
|
if m.Header.Type != syscall.SCM_CREDS {
|
|
return nil, syscall.EINVAL
|
|
}
|
|
ucred := *(*Ucred)(unsafe.Pointer(&m.Data[0]))
|
|
return &ucred, nil
|
|
}
|
|
|
|
func (t *unixTransport) SendNullByte() error {
|
|
ucred := &Ucred{Pid: int32(os.Getpid()), Uid: uint32(os.Getuid()), Gid: uint32(os.Getgid())}
|
|
b := UnixCredentials(ucred)
|
|
_, oobn, err := t.UnixConn.WriteMsgUnix([]byte{0}, b, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if oobn != len(b) {
|
|
return io.ErrShortWrite
|
|
}
|
|
return nil
|
|
}
|