Skip to content

Commit

Permalink
S3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jan 5, 2022
1 parent dc07dde commit 083f5ca
Show file tree
Hide file tree
Showing 27 changed files with 1,002 additions and 627 deletions.
45 changes: 2 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
# Yeet
# Yeet ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/vilsol/yeet/build) ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/vilsol/yeet) ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/vilsol/yeet)

![GitHub Workflow Status](https://img.shields.io/github/workflow/status/vilsol/yeet/build)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/vilsol/yeet)
![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/vilsol/yeet)

```
Usage:
yeet [command]
Available Commands:
completion generate the autocompletion script for the specified shell
help Help about any command
serve Run the webserver
Flags:
--colors Force output with colors
--expiry Use cache expiry
--expiry-interval duration Interval between cache GC's (default 10m0s)
--expiry-time duration Lifetime of a cache entry (default 1h0m0s)
-h, --help help for yeet
--host string Hostname to bind the webserver
--index-file string The directory default index file (default "index.html")
--log string The log level to output (default "info")
--paths strings Paths to serve on the webserver (default [./www])
--port int Port to run the webserver on (default 8080)
--warmup Load all files into memory on startup
--watch Watch filesystem for changes
Use "yeet [command] --help" for more information about a command.
```
CLI Usage: [Docs](./docs/yeet.md)

## Docker

Expand Down Expand Up @@ -63,16 +35,3 @@ BenchmarkServerGet2ReqPerConn10KClientsExpiry 11266317 10
BenchmarkServerGet10ReqPerConn10KClientsExpiry 15184057 776 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet100ReqPerConn10KClientsExpiry 16339011 714 ns/op 0 B/op 0 allocs/op
```

### With file watching

```
BenchmarkServerGet1ReqPerConnWatch 8282121 1430 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet2ReqPerConnWatch 11046026 1080 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet10ReqPerConnWatch 15087688 791 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet10KReqPerConnWatch 16619936 733 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet1ReqPerConn10KClientsWatch 8312827 1460 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet2ReqPerConn10KClientsWatch 10938738 1097 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet10ReqPerConn10KClientsWatch 14515534 811 ns/op 0 B/op 0 allocs/op
BenchmarkServerGet100ReqPerConn10KClientsWatch 16010731 717 ns/op 0 B/op 0 allocs/op
```
21 changes: 19 additions & 2 deletions cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cache

import "time"
import (
"github.com/Vilsol/yeet/source"
"io"
"time"
)

type CachedInstance struct {
RelativePath string
Expand All @@ -10,7 +14,20 @@ type CachedInstance struct {
LoadTime time.Time
}

type commonInstance struct {
Instance *CachedInstance
Get func(instance *commonInstance) (string, io.Reader, int)
}

type KeyValue struct {
Key string
Value *commonInstance
}

type Cache interface {
Index() (int64, error)
Get(path []byte) (string, []byte)
Get(path []byte, host []byte) (string, io.Reader, int)
Source() source.Source
Iter() <-chan KeyValue
Store(path []byte, host []byte, instance *commonInstance)
}
122 changes: 122 additions & 0 deletions cache/hashmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package cache

import (
"github.com/Vilsol/yeet/source"
"github.com/cornelk/hashmap"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"io"
)

var _ Cache = (*HashMapCache)(nil)

type HashMapCache struct {
data *hashmap.HashMap
source source.Source
hosts bool
}

func NewHashMapCache(source source.Source, hosts bool) (*HashMapCache, error) {
data := &hashmap.HashMap{}

c := &HashMapCache{
data: data,
source: source,
hosts: hosts,
}

if viper.GetBool("expiry") {
expiry(c)
}

return c, nil
}

func (c *HashMapCache) Index() (int64, error) {
totalCount, err := indexBase(c)

if viper.GetBool("watch") {
events, err := c.source.Watch()
if err != nil {
return 0, err
}

go func(c *HashMapCache, events <-chan source.WatchEvent) {
for event := range events {
switch event.Op {
case source.WatchRename:
fallthrough
case source.WatchDelete:
c.data.Del(event.CleanPath)
log.Trace().Msgf("Removed from cache: %s", event.CleanPath)
case source.WatchCreate:
instance := &commonInstance{
Instance: &CachedInstance{
RelativePath: event.CleanPath,
AbsolutePath: event.AbsPath,
},
Get: load(c),
}

c.data.Set(event.CleanPath, instance)

if viper.GetBool("warmup") {
instance.Get(instance)
}

log.Trace().Msgf("Added to cache: %s", event.CleanPath)
case source.WatchModify:
instance, ok := c.data.GetStringKey(event.CleanPath)
if ok {
instance.(*commonInstance).Get = load(c)
instance.(*commonInstance).Instance.Data = nil
instance.(*commonInstance).Instance.ContentType = ""
log.Trace().Msgf("Evicted from cache: %s", event.CleanPath)
}
}
}
}(c, events)
}

return totalCount, err
}

func (c *HashMapCache) Store(path []byte, host []byte, instance *commonInstance) {
if !c.hosts {
c.data.Set(path, instance)
} else {
c.data.Set(append(host, path...), instance)
}
}

func (c *HashMapCache) Get(path []byte, host []byte) (string, io.Reader, int) {
if !c.hosts {
if instance, ok := c.data.Get(path); ok {
return instance.(*commonInstance).Get(instance.(*commonInstance))
}
} else {
if instance, ok := c.data.Get(append(host, path...)); ok {
return instance.(*commonInstance).Get(instance.(*commonInstance))
}
}

return "", nil, 0
}

func (c *HashMapCache) Source() source.Source {
return c.source
}

func (c *HashMapCache) Iter() <-chan KeyValue {
ch := make(chan KeyValue)
go func(c *HashMapCache, ch chan KeyValue) {
for keyVal := range c.data.Iter() {
ch <- KeyValue{
Key: keyVal.Key.(string),
Value: keyVal.Value.(*commonInstance),
}
}
close(ch)
}(c, ch)
return ch
}
94 changes: 0 additions & 94 deletions cache/readonly.go

This file was deleted.

Loading

0 comments on commit 083f5ca

Please sign in to comment.