-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 1s cache for lapi.GetStreamByPlaybackID()
It causes the prod failure, because of the cascading failures
- Loading branch information
Showing
5 changed files
with
127 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package mistapiconnector | ||
|
||
import ( | ||
"github.com/livepeer/go-api-client" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const lapiCacheDuration = 1 * time.Second | ||
|
||
type ApiClientCached struct { | ||
lapi *api.Client | ||
streamCache map[string]entry | ||
mu sync.RWMutex | ||
ttl time.Duration | ||
} | ||
|
||
func NewApiClientCached(lapi *api.Client) *ApiClientCached { | ||
return &ApiClientCached{ | ||
lapi: lapi, | ||
streamCache: make(map[string]entry), | ||
ttl: lapiCacheDuration, | ||
} | ||
} | ||
|
||
type entry struct { | ||
stream *api.Stream | ||
err error | ||
updateAt time.Time | ||
} | ||
|
||
func (a *ApiClientCached) GetStreamByPlaybackID(playbackId string) (*api.Stream, error) { | ||
a.mu.RLock() | ||
e, ok := a.streamCache[playbackId] | ||
if ok && e.stream != nil && e.updateAt.Add(a.ttl).After(time.Now()) { | ||
// Use cached value | ||
a.mu.RUnlock() | ||
return e.stream, e.err | ||
} | ||
a.mu.RUnlock() | ||
|
||
// Value not cached or expired | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
// Check again in case another goroutine has updated the cache in the meantime | ||
e, ok = a.streamCache[playbackId] | ||
if ok && e.stream != nil && e.updateAt.Add(a.ttl).After(time.Now()) { | ||
return e.stream, e.err | ||
} | ||
// No value in the cache, fetch from Livepeer API and store the result in a cache | ||
stream, err := a.lapi.GetStreamByPlaybackID(playbackId) | ||
a.streamCache[playbackId] = entry{ | ||
stream: stream, | ||
err: err, | ||
updateAt: time.Now(), | ||
} | ||
return stream, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package mistapiconnector | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/livepeer/go-api-client" | ||
require2 "github.com/stretchr/testify/require" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetStreamByPlaybackID(t *testing.T) { | ||
require := require2.New(t) | ||
|
||
// given | ||
playbackId := "some-playback-id" | ||
stubStream := &api.Stream{ | ||
ID: "123456", | ||
} | ||
var hits int | ||
|
||
lapiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
hits++ | ||
err := json.NewEncoder(w).Encode(stubStream) | ||
if err != nil { | ||
t.Fail() | ||
} | ||
})) | ||
defer lapiServer.Close() | ||
|
||
lapi, _ := api.NewAPIClientGeolocated(api.ClientOptions{ | ||
Server: lapiServer.URL, | ||
}) | ||
cachedClient := NewApiClientCached(lapi) | ||
|
||
// when first call to Livepeer API | ||
s, err := cachedClient.GetStreamByPlaybackID(playbackId) | ||
require.NoError(err) | ||
require.Equal(stubStream, s) | ||
require.Equal(1, hits) | ||
|
||
// when multiple time the same call within 1 second | ||
for i := 0; i < 10; i++ { | ||
s, err = cachedClient.GetStreamByPlaybackID(playbackId) | ||
require.NoError(err) | ||
require.Equal(stubStream, s) | ||
require.Equal(1, hits) | ||
} | ||
|
||
// when ttl is expired | ||
time.Sleep(1 * time.Second) | ||
s, err = cachedClient.GetStreamByPlaybackID(playbackId) | ||
require.NoError(err) | ||
require.Equal(stubStream, s) | ||
require.Equal(2, hits) | ||
} |