mirror of
https://gitea.com/gitea/tea.git
synced 2026-02-22 06:13:32 +01:00
Move versions/filelocker into dedicated subpackages, and consistent headers in http requests (#888)
- move filelocker logic into dedicated subpackage - consistent useragent in requests Reviewed-on: https://gitea.com/gitea/tea/pulls/888 Co-authored-by: techknowlogick <techknowlogick@gitea.com> Co-committed-by: techknowlogick <techknowlogick@gitea.com>
This commit is contained in:
committed by
techknowlogick
parent
982adb4d02
commit
49a9032d8a
@@ -5,9 +5,10 @@ package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/tea/modules/filelock"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -16,9 +17,6 @@ const (
|
||||
|
||||
// mutexPollInterval is how often to retry acquiring the in-process mutex.
|
||||
mutexPollInterval = 10 * time.Millisecond
|
||||
|
||||
// fileLockPollInterval is how often to retry acquiring the file lock.
|
||||
fileLockPollInterval = 50 * time.Millisecond
|
||||
)
|
||||
|
||||
// configMutex protects in-process concurrent access to the config.
|
||||
@@ -42,30 +40,20 @@ func acquireConfigLock(lockPath string, timeout time.Duration) (unlock func() er
|
||||
}
|
||||
|
||||
// Mutex acquired, now try file lock
|
||||
file, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0o600)
|
||||
remaining := max(time.Until(deadline), 0)
|
||||
locker := filelock.New(lockPath, remaining)
|
||||
|
||||
fileUnlock, err := locker.Acquire()
|
||||
if err != nil {
|
||||
configMutex.Unlock()
|
||||
return nil, fmt.Errorf("failed to open lock file: %w", err)
|
||||
}
|
||||
|
||||
// Try to acquire file lock with remaining timeout
|
||||
remaining := max(time.Until(deadline), 0)
|
||||
|
||||
if err := lockFile(file, remaining); err != nil {
|
||||
file.Close()
|
||||
configMutex.Unlock()
|
||||
return nil, fmt.Errorf("failed to acquire file lock: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return unlock function
|
||||
return func() error {
|
||||
unlockErr := unlockFile(file)
|
||||
closeErr := file.Close()
|
||||
unlockErr := fileUnlock()
|
||||
configMutex.Unlock()
|
||||
if unlockErr != nil {
|
||||
return unlockErr
|
||||
}
|
||||
return closeErr
|
||||
return unlockErr
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build unix
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// lockFile acquires an exclusive lock on the file using flock.
|
||||
// It polls with non-blocking flock until timeout.
|
||||
func lockFile(file *os.File, timeout time.Duration) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
|
||||
for {
|
||||
err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if err != syscall.EWOULDBLOCK {
|
||||
return fmt.Errorf("flock failed: %w", err)
|
||||
}
|
||||
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("timeout waiting for file lock")
|
||||
}
|
||||
time.Sleep(fileLockPollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
// unlockFile releases the lock on the file.
|
||||
func unlockFile(file *os.File) error {
|
||||
return syscall.Flock(int(file.Fd()), syscall.LOCK_UN)
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
//go:build windows
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// lockFile acquires an exclusive lock on the file using LockFileEx.
|
||||
// It polls with non-blocking LockFileEx until timeout.
|
||||
func lockFile(file *os.File, timeout time.Duration) error {
|
||||
deadline := time.Now().Add(timeout)
|
||||
handle := windows.Handle(file.Fd())
|
||||
|
||||
// LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY
|
||||
const flags = windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY
|
||||
|
||||
for {
|
||||
// Lock the first byte (advisory lock)
|
||||
var overlapped windows.Overlapped
|
||||
err := windows.LockFileEx(handle, flags, 0, 1, 0, &overlapped)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if err != windows.ERROR_LOCK_VIOLATION {
|
||||
return fmt.Errorf("LockFileEx failed: %w", err)
|
||||
}
|
||||
|
||||
if time.Now().After(deadline) {
|
||||
return fmt.Errorf("timeout waiting for file lock")
|
||||
}
|
||||
time.Sleep(fileLockPollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
// unlockFile releases the lock on the file.
|
||||
func unlockFile(file *os.File) error {
|
||||
handle := windows.Handle(file.Fd())
|
||||
var overlapped windows.Overlapped
|
||||
return windows.UnlockFileEx(handle, 0, 1, 0, &overlapped)
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"code.gitea.io/tea/modules/debug"
|
||||
"code.gitea.io/tea/modules/httputil"
|
||||
"code.gitea.io/tea/modules/theme"
|
||||
"code.gitea.io/tea/modules/utils"
|
||||
"github.com/charmbracelet/huh"
|
||||
@@ -290,9 +291,9 @@ func doOAuthRefresh(l *Login) (*oauth2.Token, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
Transport: httputil.WrapTransport(&http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: l.Insecure},
|
||||
},
|
||||
}),
|
||||
}
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
||||
|
||||
@@ -336,7 +337,7 @@ func (l *Login) Client(options ...gitea.ClientOption) *gitea.Client {
|
||||
options = append([]gitea.ClientOption{gitea.SetGiteaVersion("")}, options...)
|
||||
}
|
||||
|
||||
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient))
|
||||
options = append(options, gitea.SetToken(l.Token), gitea.SetHTTPClient(httpClient), gitea.SetUserAgent(httputil.UserAgent()))
|
||||
if debug.IsDebug() {
|
||||
options = append(options, gitea.SetDebugMode())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user