2019-06-01 19:34:03 +08:00
|
|
|
// Copyright 2017-2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-09-17 18:43:59 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-09-17 18:43:59 +08:00
|
|
|
|
2019-01-16 13:02:59 +08:00
|
|
|
// Package gfpool provides io-reusable pool for file pointer.
|
2018-09-17 18:43:59 +08:00
|
|
|
package gfpool
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
2020-03-14 17:12:44 +08:00
|
|
|
"github.com/gogf/gf/os/gfile"
|
2019-06-19 09:06:52 +08:00
|
|
|
"os"
|
2020-03-14 17:12:44 +08:00
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/container/gmap"
|
|
|
|
"github.com/gogf/gf/container/gpool"
|
|
|
|
"github.com/gogf/gf/container/gtype"
|
|
|
|
"github.com/gogf/gf/os/gfsnotify"
|
2018-09-17 18:43:59 +08:00
|
|
|
)
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// File pointer pool.
|
2018-09-17 18:43:59 +08:00
|
|
|
type Pool struct {
|
2020-03-14 17:12:44 +08:00
|
|
|
id *gtype.Int // Pool id, which is used to mark this pool whether recreated.
|
|
|
|
pool *gpool.Pool // Underlying pool.
|
|
|
|
init *gtype.Bool // Whether initialized, used for marking this file added to fsnotify, and it can only be added just once.
|
|
|
|
ttl time.Duration // Time to live for file pointer items.
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// File is an item in the pool.
|
2018-09-17 18:43:59 +08:00
|
|
|
type File struct {
|
2020-03-14 17:12:44 +08:00
|
|
|
*os.File // Underlying file pointer.
|
|
|
|
pid int // Belonging pool id, which is set when file pointer created. It's used to check whether the pool is recreated.
|
|
|
|
pool *Pool // Belonging ool.
|
|
|
|
flag int // Flash for opening file.
|
|
|
|
perm os.FileMode // Permission for opening file.
|
|
|
|
path string // Absolute path of the file.
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
var (
|
2020-03-14 17:12:44 +08:00
|
|
|
// Global file pointer pool.
|
2019-07-23 23:20:27 +08:00
|
|
|
pools = gmap.NewStrAnyMap(true)
|
2019-06-01 19:34:03 +08:00
|
|
|
)
|
2018-09-17 18:43:59 +08:00
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// Open creates and returns a file item with given file path, flag and opening permission.
|
|
|
|
// It automatically creates an associated file pointer pool internally when it's called first time.
|
|
|
|
// It retrieves a file item from the file pointer pool after then.
|
|
|
|
func Open(path string, flag int, perm os.FileMode, ttl ...time.Duration) (file *File, err error) {
|
|
|
|
var fpTTL time.Duration
|
|
|
|
if len(ttl) > 0 {
|
|
|
|
fpTTL = ttl[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-14 17:12:44 +08:00
|
|
|
path, err = gfile.Search(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pool := pools.GetOrSetFuncLock(
|
|
|
|
fmt.Sprintf("%s&%d&%d&%d", path, flag, fpTTL, perm),
|
|
|
|
func() interface{} {
|
|
|
|
return New(path, flag, perm, fpTTL)
|
|
|
|
},
|
|
|
|
).(*Pool)
|
2018-11-03 17:50:00 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
return pool.File()
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// New creates and returns a file pointer pool with given file path, flag and opening permission.
|
|
|
|
//
|
|
|
|
// Note the expiration logic:
|
|
|
|
// ttl = 0 : not expired;
|
|
|
|
// ttl < 0 : immediate expired after use;
|
|
|
|
// ttl > 0 : timeout expired;
|
|
|
|
// It is not expired in default.
|
|
|
|
func New(path string, flag int, perm os.FileMode, ttl ...time.Duration) *Pool {
|
|
|
|
var fpTTL time.Duration
|
|
|
|
if len(ttl) > 0 {
|
|
|
|
fpTTL = ttl[0]
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
p := &Pool{
|
2020-03-14 17:12:44 +08:00
|
|
|
id: gtype.NewInt(),
|
|
|
|
ttl: fpTTL,
|
|
|
|
init: gtype.NewBool(),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-14 17:12:44 +08:00
|
|
|
p.pool = newFilePool(p, path, flag, perm, fpTTL)
|
2019-06-19 09:06:52 +08:00
|
|
|
return p
|
2018-10-31 19:00:28 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// newFilePool creates and returns a file pointer pool with given file path, flag and opening permission.
|
|
|
|
func newFilePool(p *Pool, path string, flag int, perm os.FileMode, ttl time.Duration) *gpool.Pool {
|
|
|
|
pool := gpool.New(ttl, func() (interface{}, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
file, err := os.OpenFile(path, flag, perm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &File{
|
2020-03-14 17:12:44 +08:00
|
|
|
File: file,
|
|
|
|
pool: p,
|
|
|
|
pid: p.id.Val(),
|
|
|
|
flag: flag,
|
|
|
|
perm: perm,
|
|
|
|
path: path,
|
2019-06-19 09:06:52 +08:00
|
|
|
}, nil
|
|
|
|
}, func(i interface{}) {
|
|
|
|
_ = i.(*File).File.Close()
|
|
|
|
})
|
|
|
|
return pool
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// File retrieves file item from the file pointer pool and returns it. It creates one if
|
|
|
|
// the file pointer pool is empty.
|
|
|
|
// Note that it should be closed when it will never be used. When it's closed, it is not
|
|
|
|
// really closed the underlying file pointer but put back to the file pinter pool.
|
2018-09-17 18:43:59 +08:00
|
|
|
func (p *Pool) File() (*File, error) {
|
2019-06-19 09:06:52 +08:00
|
|
|
if v, err := p.pool.Get(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
f := v.(*File)
|
|
|
|
stat, err := os.Stat(f.path)
|
|
|
|
if f.flag&os.O_CREATE > 0 {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if file, err := os.OpenFile(f.path, f.flag, f.perm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
f.File = file
|
|
|
|
if stat, err = f.Stat(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f.flag&os.O_TRUNC > 0 {
|
|
|
|
if stat.Size() > 0 {
|
|
|
|
if err := f.Truncate(0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f.flag&os.O_APPEND > 0 {
|
|
|
|
if _, err := f.Seek(0, 2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := f.Seek(0, 0); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 17:12:44 +08:00
|
|
|
// It firstly checks using !p.init.Val() for performance purpose.
|
|
|
|
if !p.init.Val() && p.init.Cas(false, true) {
|
2019-06-19 09:06:52 +08:00
|
|
|
_, _ = gfsnotify.Add(f.path, func(event *gfsnotify.Event) {
|
2020-03-14 17:12:44 +08:00
|
|
|
// If teh file is removed or renamed, recreates the pool by increasing the pool id.
|
2019-06-19 09:06:52 +08:00
|
|
|
if event.IsRemove() || event.IsRename() {
|
2020-03-14 17:12:44 +08:00
|
|
|
// It drops the old pool.
|
2019-06-19 09:06:52 +08:00
|
|
|
p.id.Add(1)
|
2020-03-14 17:12:44 +08:00
|
|
|
// Clears the pool items staying in the pool.
|
2019-06-19 09:06:52 +08:00
|
|
|
p.pool.Clear()
|
2020-03-14 17:12:44 +08:00
|
|
|
// It uses another adding to drop the file items between the two adding.
|
|
|
|
// Whenever the pool id changes, the pool will be recreated.
|
2019-06-19 09:06:52 +08:00
|
|
|
p.id.Add(1)
|
|
|
|
}
|
|
|
|
}, false)
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// Close closes current file pointer pool.
|
2019-01-24 20:15:11 +08:00
|
|
|
func (p *Pool) Close() {
|
2019-06-19 09:06:52 +08:00
|
|
|
p.pool.Close()
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-14 17:12:44 +08:00
|
|
|
// Close puts the file pointer back to the file pointer pool.
|
2018-09-17 18:43:59 +08:00
|
|
|
func (f *File) Close() error {
|
2020-03-14 17:12:44 +08:00
|
|
|
if f.pid == f.pool.id.Val() {
|
|
|
|
return f.pool.pool.Put(f)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
return nil
|
2018-09-17 18:43:59 +08:00
|
|
|
}
|