Merge pull request #55 from mailgun/thrawn/gha

Remove Travis CI and use GitHub Actions
This commit is contained in:
Derrick J. Wippler 2022-12-20 09:26:46 -06:00 committed by GitHub
commit 2bec0534cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 141 additions and 20 deletions

46
.github/workflows/on-pull-request.yaml vendored Normal file
View File

@ -0,0 +1,46 @@
name: CI
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main
jobs:
on-pull-request:
name: test
strategy:
matrix:
go-version:
- 1.18.x
- 1.19.x
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@master
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- run: go env
- name: Cache deps
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Install deps
run: go mod download
- name: Test
run: go test ./...

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*~
.idea/
.DS_Store

View File

@ -1,20 +0,0 @@
language: go
go_import_path: github.com/mailgun/groupcache
os: linux
dist: xenial
sudo: false
script:
- go test ./...
go:
- 1.13.x
- 1.14.x
- 1.15.x
- 1.17.x
- master
cache:
directories:
- $GOPATH/pkg

94
cmd/server/main.go Normal file
View File

@ -0,0 +1,94 @@
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/mailgun/groupcache/v2"
)
var store = map[string]string{}
var group = groupcache.NewGroup("cache1", 64<<20, groupcache.GetterFunc(
func(ctx context.Context, key string, dest groupcache.Sink) error {
fmt.Printf("Get Called\n")
v, ok := store[key]
if !ok {
return fmt.Errorf("key not set")
} else {
if err := dest.SetBytes([]byte(v), time.Now().Add(10*time.Minute)); err != nil {
log.Printf("Failed to set cache value for key '%s' - %v\n", key, err)
return err
}
}
return nil
},
))
func main() {
addr := flag.String("addr", ":8080", "server address")
addr2 := flag.String("api-addr", ":8081", "api server address")
peers := flag.String("pool", "http://localhost:8080", "server pool list")
flag.Parse()
p := strings.Split(*peers, ",")
pool := groupcache.NewHTTPPoolOpts(fmt.Sprintf("http://%s", *addr), &groupcache.HTTPPoolOptions{})
pool.Set(p...)
http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
key := r.FormValue("key")
value := r.FormValue("value")
fmt.Printf("Set: [%s]%s\n", key, value)
store[key] = value
})
http.HandleFunc("/cache", func(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
fmt.Printf("Fetching value for key '%s'\n", key)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
var b []byte
err := group.Get(ctx, key, groupcache.AllocatingByteSliceSink(&b))
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.Write(b)
w.Write([]byte{'\n'})
})
server := http.Server{
Addr: *addr,
Handler: pool,
}
go func() {
if err := server.ListenAndServe(); err != nil {
log.Fatalf("Failed to start HTTP server - %v", err)
}
}()
go func() {
if err := http.ListenAndServe(*addr2, nil); err != nil {
log.Fatalf("Failed to start API HTTP server - %v", err)
}
}()
fmt.Printf("Running...\n")
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
<-termChan
}