Skip to content

Commit

Permalink
fix: initialize chan operation
Browse files Browse the repository at this point in the history
  • Loading branch information
vie-serendipity committed May 9, 2024
1 parent 82b1fa5 commit d4cb796
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 2 additions & 3 deletions pkg/yurthub/cachemanager/cache_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,16 @@ func NewCacheManager(
listSelectorCollector: make(map[storage.Key]string),
inMemoryCache: make(map[string]runtime.Object),
errorKeys: &errorKeys{
keys: make(map[string]string),
keys: make(map[string]string),
operations: make(chan operation, 100),
},
}
cm.errorKeys.recover()

return cm
}

// CacheResponse cache response of request into backend storage
func (cm *cacheManager) CacheResponse(req *http.Request, prc io.ReadCloser, stopCh <-chan struct{}) error {
go cm.errorKeys.sync(stopCh)
ctx := req.Context()
info, _ := apirequest.RequestInfoFrom(ctx)
if isWatch(ctx) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/yurthub/cachemanager/error_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cachemanager

import (
"bufio"
"context"
"encoding/json"
"os"
"path/filepath"
Expand All @@ -42,6 +43,7 @@ func NewErrorKeys() *errorKeys {
keys: make(map[string]string),
operations: make(chan operation, 10),

Check warning on line 44 in pkg/yurthub/cachemanager/error_keys.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/cachemanager/error_keys.go#L41-L44

Added lines #L41 - L44 were not covered by tests
}
go ek.sync(context.Background())
return ek

Check warning on line 47 in pkg/yurthub/cachemanager/error_keys.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/cachemanager/error_keys.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

Expand Down Expand Up @@ -89,7 +91,7 @@ func (ek *errorKeys) length() int {
return len(ek.keys)
}

func (ek *errorKeys) sync(stopChan <-chan struct{}) {
func (ek *errorKeys) sync(ctx context.Context) {
err := os.MkdirAll(AOFPrefix, 0755)
if err != nil {
klog.Errorf("failed to create dir: %v", err)

Check warning on line 97 in pkg/yurthub/cachemanager/error_keys.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/cachemanager/error_keys.go#L97

Added line #L97 was not covered by tests
Expand All @@ -108,7 +110,7 @@ func (ek *errorKeys) sync(stopChan <-chan struct{}) {
}
file.Write(append(data, '\n'))
file.Sync()

Check warning on line 112 in pkg/yurthub/cachemanager/error_keys.go

View check run for this annotation

Codecov / codecov/patch

pkg/yurthub/cachemanager/error_keys.go#L111-L112

Added lines #L111 - L112 were not covered by tests
case <-stopChan:
case <-ctx.Done():
return
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/yurthub/cachemanager/error_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cachemanager

import (
"context"
"errors"
"os"
"testing"
Expand Down Expand Up @@ -52,8 +53,8 @@ func TestXxx(t *testing.T) {
keys: make(map[string]string),
operations: make(chan operation, 100),
}
stopChan := make(chan struct{})
go ek.sync(stopChan)
ctx, cancel := context.WithCancel(context.TODO())
go ek.sync(ctx)
for i := range tc.keys {
ek.put(tc.keys[i], tc.err[i])
}
Expand All @@ -69,7 +70,7 @@ func TestXxx(t *testing.T) {
if ek.length() != 0 {
t.Errorf("expect length %v, got %v", tc.length, ek.length())
}
close(stopChan)
cancel()
os.RemoveAll(AOFPrefix)
})
}
Expand Down

0 comments on commit d4cb796

Please sign in to comment.