forked from pierrre/imageserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
38 lines (32 loc) · 851 Bytes
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Package imageserver provides an Image server toolkit.
package imageserver
// Server serves an Image.
type Server interface {
Get(Params) (*Image, error)
}
// ServerFunc is a Server func.
type ServerFunc func(Params) (*Image, error)
// Get implements Server.
func (f ServerFunc) Get(params Params) (*Image, error) {
return f(params)
}
// NewLimitServer creates a new Server that limits the number of concurrent executions.
//
// It uses a buffered channel to limit the number of concurrent executions.
func NewLimitServer(s Server, limit int) Server {
return &limitServer{
Server: s,
limitCh: make(chan struct{}, limit),
}
}
type limitServer struct {
Server
limitCh chan struct{}
}
func (s *limitServer) Get(params Params) (*Image, error) {
s.limitCh <- struct{}{}
defer func() {
<-s.limitCh
}()
return s.Server.Get(params)
}