Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fca1030e1a |
@@ -60,6 +60,13 @@ func (s *Service) getImage(uid string) (file []byte, mimeType string, err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
func (s *Service) GetImage(ctx context.Context, uid string, options Options) (img io.Reader, mimeType string, err error) {
|
||||||
|
if err = s.seph.Acquire(ctx, 1); err != nil {
|
||||||
|
err = fmt.Errorf("could not Acquire semaphore: %w", err)
|
||||||
|
return
|
||||||
|
|
||||||
|
}
|
||||||
|
defer s.seph.Release(1)
|
||||||
|
|
||||||
rawImage, mimeType, err := s.getImage(uid)
|
rawImage, mimeType, err := s.getImage(uid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/valkey-io/valkey-go"
|
"github.com/valkey-io/valkey-go"
|
||||||
|
"golang.org/x/sync/semaphore"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
@@ -15,12 +16,14 @@ type Options struct {
|
|||||||
type Service struct {
|
type Service struct {
|
||||||
fs fileSystem
|
fs fileSystem
|
||||||
cache valkey.Client
|
cache valkey.Client
|
||||||
|
seph *semaphore.Weighted
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(fs fileSystem, cache valkey.Client) *Service {
|
func New(fs fileSystem, cache valkey.Client, maxConcurency int64) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
fs: fs,
|
fs: fs,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
|
seph: semaphore.NewWeighted(maxConcurency),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func (s *Server) registerOther() (h http.Handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ = resthome.New(mux, page.New(fs))
|
_ = resthome.New(mux, page.New(fs))
|
||||||
_ = restimage.New(mux, images.New(fs, valkeyClient))
|
_ = restimage.New(mux, images.New(fs, valkeyClient, 4))
|
||||||
|
|
||||||
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
mux.Handle("/", http.FileServer(http.FS(web.GetStaticFS())))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user