Skip to content

Commit

Permalink
Manual file creation, not depend on os.CreateTemp
Browse files Browse the repository at this point in the history
  • Loading branch information
linxGnu committed Jan 15, 2022
1 parent b769aa6 commit c6c507a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func (q *queue) enqueueBatch(b entry.Batch) error {
}

func (q *queue) newSegment() (*segment, error) {
f, err := os.CreateTemp(q.settings.DataDir, segPrefix)
f, err := createFile(q.settings.DataDir, segPrefix)
if err != nil {
return nil, err
}
Expand Down
19 changes: 19 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package pqueue

import (
"container/list"
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -79,3 +82,19 @@ func loadFileInfos(dir string, infoExtractor func(os.DirEntry) (os.FileInfo, err
func fileInfoExtractor(f os.DirEntry) (os.FileInfo, error) {
return f.Info()
}

func createFile(dir, prefix string) (f *os.File, err error) {
prefix = path.Join(dir, prefix)

for attempt := 0; attempt < 10_000; attempt++ {
name := prefix + strconv.FormatInt(time.Now().UnixNano(), 10)

f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if !os.IsExist(err) {
return
}
}

err = fmt.Errorf("creating file but fail. path: %s", dir)
return
}

0 comments on commit c6c507a

Please sign in to comment.