Skip to content

Commit

Permalink
Merge pull request #124 from PierreF/retry-http
Browse files Browse the repository at this point in the history
Retry on backend error
  • Loading branch information
PierreF committed Mar 7, 2021
2 parents 19315cf + c619a88 commit 76e798c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ auth_opt_log_file /var/log/mosquitto/mosquitto.log

If `log_dest` or `log_file` are invalid, or if there's an error opening the file (e.g. no permissions), logging will default to `stderr`.

#### Retry

By default, if backend had an error (and no other backend granted access), an error is returned to Mosquitto.

It's possible to enable retry, which will immediately retry all configured backends. This could be useful if the
backend may be behind a load-balancer (like HTTP backend) and one instance may fail:

```
auth_opt_retry_count 2
```

The above example will do up to 2 retries (3 calls in total considering the original one) if the responsible backend had an error or was down while performing the check.

#### Prefixes

Though the plugin may have multiple backends enabled, there's a way to specify which backend must be used for a given user: prefixes. When enabled, `prefixes` allow to check if the username contains a predefined prefix in the form prefix_username and use the configured backend for that prefix. Options to enable and set prefixes are the following:
Expand Down
32 changes: 30 additions & 2 deletions go-auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type AuthPlugin struct {
customPluginHalt func()
useCache bool
checkPrefix bool
retryCount int
prefixes map[string]string
logLevel log.Level
logDest string
Expand Down Expand Up @@ -120,6 +121,15 @@ func AuthPluginInit(keys []string, values []string, authOptsNum int) {
authOpts[keys[i]] = values[i]
}

if retryCount, ok := authOpts["retry_count"]; ok {
retry, err := strconv.ParseInt(retryCount, 10, 64)
if err == nil {
authPlugin.retryCount = int(retry)
} else {
log.Warningf("couldn't parse retryCount (err: %s), defaulting to 0", err)
}
}

//Log and end program if backends are wrong
if !backendsOk {
log.Fatal("backends error")
Expand Down Expand Up @@ -520,7 +530,16 @@ func setCache(authOpts map[string]string) {

//export AuthUnpwdCheck
func AuthUnpwdCheck(username, password, clientid string) uint8 {
ok, err := authUnpwdCheck(username, password, clientid)
var ok bool
var err error

for try := 0; try <= authPlugin.retryCount; try++ {
ok, err = authUnpwdCheck(username, password, clientid)
if err == nil {
break
}
}

if err != nil {
log.Error(err)
return AuthError
Expand Down Expand Up @@ -608,7 +627,16 @@ func authUnpwdCheck(username, password, clientid string) (bool, error) {

//export AuthAclCheck
func AuthAclCheck(clientid, username, topic string, acc int) uint8 {
ok, err := authAclCheck(clientid, username, topic, acc)
var ok bool
var err error

for try := 0; try <= authPlugin.retryCount; try++ {
ok, err = authAclCheck(clientid, username, topic, acc)
if err == nil {
break
}
}

if err != nil {
log.Error(err)
return AuthError
Expand Down

0 comments on commit 76e798c

Please sign in to comment.