Example #1
0
// Store cacheable resources on disk. Naive (and non-conforming) implementation
// of a HTTP cache. Note that this is really not transparent caching:
//
// - Headers are not cached
//
// - As a consequence, encoded data (gzip etc) is served from cache as raw data
//
// - Upstream status code is completely ignored
//
// - Caching directives from client are ignored
//
// - HTTP caching directives from upstream are ignored
//
// - Non-standard upstream "X-Cache" header is used to determine cacheability
//
// - Cache hits are served as raw files, introducing some illegal headers
//
// - and probably more...
//
// All of that notwithstanding, this is a proof of concept worth exploring.
func Cache(h http.Handler) http.Handler {
	return &cacheWrapper{
		Basedir: mustmkdtemp("godspeed-cache-XXXXXXXX") + "/",
		// TODO: Manage maximum size
		idx:     lrucache.New(1 << 20),
		wrapped: h,
	}
}
Example #2
0
func makeServer(password string, writeTimeout time.Duration) *server {
	s := &server{
		ServeMux:     http.NewServeMux(),
		rooms:        lrucache.New(NUM_ROOMS),
		roomNames:    mapset.NewSet(),
		writeTimeout: DEFAULT_WRITE_TIMEOUT,
	}
	s.rooms.OnMiss(func(roomname string) (lrucache.Cacheable, error) {
		go s.roomNames.Add(roomname)
		return newChatroom(s, roomname, BACKLOG_SIZE), nil
	})
	s.HandleFunc("/hub", s.handleHub)
	s.HandleFunc("/rooms", s.handleRooms)
	return s
}
Example #3
0
	"net/http"

	"github.com/gorilla/websocket"
	"github.com/hraban/lrucache"
)

// in num msgs not bytes (TODO: should be in bytes)
const BACKLOG_SIZE = 10

// when maximum is reached, room with least recent activity is purged
const NUM_ROOMS = 1000

// https://soundcloud.com/testa-jp/mask-on-mask-re-edit-free-dl
var verbose bool
var files http.Handler
var rooms = lrucache.New(NUM_ROOMS)

// F**k yeah lrucache
func (cr *chatroom) OnPurge(lrucache.PurgeReason) {
	cr.Close()
}

func handleWebsocket(roomname string, ws *websocket.Conn) {
	obj, err := rooms.Get(roomname)
	if err != nil {
		log.Fatalf("Unexpected error from lrucache.Get(%q): %v", roomname, err)
	}
	cr := obj.(*chatroom)
	c := client(ws)
	id := cr.l_addClient(c)
	for {
Example #4
0
func newChatroom(backlogsize int64) *chatroom {
	cr := &chatroom{}
	cr.backlog = lrucache.New(backlogsize)
	cr.l.clients = map[uint32]client{}
	return cr
}