Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]Do we need a lock if we use a cache? #400

Closed
caibirdme opened this issue Oct 4, 2023 · 1 comment
Closed

[Question]Do we need a lock if we use a cache? #400

caibirdme opened this issue Oct 4, 2023 · 1 comment

Comments

@caibirdme
Copy link

Didn't find any docs about the running model of envoy wasm. Does a plugin just like a single thread redis server, that we don't need to take care of the data race, or do we should protect the data by lock?
For example: I want to implement a http plugin that if url matches some regex, add coresponding pre-configured value to its request header, so that VirtualService can leverage this header to do some traffic splitting.

{
"^/a/\\d+": "us_east",
"/x/.*/y/z$": "us_west"
}

Because regex matching consumes too much cpu time, I want to cache the result

// fast path
if head_val, ok := self.cache.get(url); ok {
  // set request header
  return Continue
}
// slow path
head_val, ok :=self.do_regexp_match(url)
if ok {
  self.cache.set(url, head_val)
}
// set request header
return Continue

I don't know if I should add a rwlock to protect the cache object?

type pluginContext struct {
  cache ThreadSafeMap[string,string] // does this map need to be thread safe? Or a builtin map is ok?
}

func (ctx *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
	return &myRegexMatchPlugin{cache: ctx.cache}
}
@mathetake
Copy link
Member

no since Proxy-Wasm is single-threaded (and Wasm is in a general single thread). Please search the GitHub repo before asking questions on the issues #368

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants